From b1ad79ed1dc216c87119955a486773fba9fcfdb6 Mon Sep 17 00:00:00 2001
From: Fernando Araiza Gonzalez <faraiza@ifarm1401.jlab.org>
Date: Wed, 6 Jun 2018 11:17:15 -0400
Subject: [PATCH] Modifications to allow per-pmt time window cuts for
 calorimeter.

---
 src/THcShower.cxx      | 15 +++++++++++----
 src/THcShower.h        |  8 ++++----
 src/THcShowerArray.cxx | 17 ++++++++++++-----
 src/THcShowerArray.h   |  4 ++--
 src/THcShowerPlane.cxx |  8 ++++----
 5 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/src/THcShower.cxx b/src/THcShower.cxx
index 00c5f92..f970f67 100644
--- a/src/THcShower.cxx
+++ b/src/THcShower.cxx
@@ -223,8 +223,6 @@ Int_t THcShower::ReadDatabase( const TDatime& date )
       {"cal_fv_test", &fvTest, kInt,0,1},
       {"cal_fv_delta", &fvDelta, kDouble,0,1},
       {"cal_ADCMode", &fADCMode, kInt, 0, 1},
-      {"cal_AdcTimeWindowMin", &fAdcTimeWindowMin, kDouble, 0, 1},
-      {"cal_AdcTimeWindowMax", &fAdcTimeWindowMax, kDouble, 0, 1},
       {"cal_adc_tdc_offset", &fAdcTdcOffset, kDouble, 0, 1},
       {"dbg_raw_cal", &fdbg_raw_cal, kInt, 0, 1},
       {"dbg_decoded_cal", &fdbg_decoded_cal, kInt, 0, 1},
@@ -241,8 +239,6 @@ Int_t THcShower::ReadDatabase( const TDatime& date )
     fdbg_clusters_cal = 0;
     fdbg_tracks_cal = 0;
     fdbg_init_cal = 0;
-    fAdcTimeWindowMin=0;
-    fAdcTimeWindowMax=10000;
     fAdcTdcOffset=0.0;
     fADCMode=kADCDynamicPedestal;
     gHcParms->LoadParmValues((DBRequest*)&list, prefix);
@@ -376,6 +372,9 @@ Int_t THcShower::ReadDatabase( const TDatime& date )
   Double_t hcal_neg_cal_const[fNTotBlocks];
   Double_t hcal_neg_gain_cor[fNTotBlocks];
 
+  fAdcTimeWindowMin = new Double_t [fNTotBlocks];
+  fAdcTimeWindowMax = new Double_t [fNTotBlocks];
+
   DBRequest list[]={
     {"cal_pos_cal_const", hcal_pos_cal_const, kDouble, fNTotBlocks},
     {"cal_pos_ped_limit", fShPosPedLimit, kInt,    fNTotBlocks,1},
@@ -383,10 +382,18 @@ Int_t THcShower::ReadDatabase( const TDatime& date )
     {"cal_neg_cal_const", hcal_neg_cal_const, kDouble, fNTotBlocks},
     {"cal_neg_ped_limit", fShNegPedLimit, kInt,    fNTotBlocks,1},
     {"cal_neg_gain_cor",  hcal_neg_gain_cor,  kDouble, fNTotBlocks},
+    {"cal_AdcTimeWindowMin", fAdcTimeWindowMin, kDouble, static_cast<UInt_t>(fNTotBlocks)},
+    {"cal_AdcTimeWindowMax", fAdcTimeWindowMax, kDouble, static_cast<UInt_t>(fNTotBlocks)},
     {"cal_min_peds", &fShMinPeds, kInt,0,1},
     {0}
   };
   fShMinPeds=0.;
+
+  for(UInt_t ip=0;ip<fNTotBlocks;ip++) {
+    fAdcTimeWindowMin[ip] = -1000.;
+    fAdcTimeWindowMax[ip] = 1000.;
+   }
+
   gHcParms->LoadParmValues((DBRequest*)&list, prefix);
 
   // Debug output.
diff --git a/src/THcShower.h b/src/THcShower.h
index e69556b..28f35ad 100644
--- a/src/THcShower.h
+++ b/src/THcShower.h
@@ -74,10 +74,10 @@ public:
   Int_t GetADCMode() {
     return fADCMode;
   }
-  Double_t GetAdcTimeWindowMin() {
+  Double_t* GetAdcTimeWindowMin() {
     return fAdcTimeWindowMin;
   }
-  Double_t GetAdcTimeWindowMax() {
+  Double_t* GetAdcTimeWindowMax() {
     return fAdcTimeWindowMax;
   }
   Double_t GetAdcTdcOffset() {
@@ -155,8 +155,8 @@ protected:
   static const Int_t kADCDynamicPedestal=1;
   static const Int_t kADCSampleIntegral=2;
   static const Int_t kADCSampIntDynPed=3;
-  Double_t fAdcTimeWindowMin;
-  Double_t fAdcTimeWindowMax;
+  Double_t* fAdcTimeWindowMin;
+  Double_t* fAdcTimeWindowMax;
   Double_t fAdcTdcOffset;
 
   Int_t fAnalyzePedestals;   // Flag for pedestal analysis.
diff --git a/src/THcShowerArray.cxx b/src/THcShowerArray.cxx
index 80a69bc..88f53cb 100644
--- a/src/THcShowerArray.cxx
+++ b/src/THcShowerArray.cxx
@@ -154,8 +154,6 @@ Int_t THcShowerArray::ReadDatabase( const TDatime& date )
     {"cal_arr_zsize", &fZSize, kDouble},
     {"cal_using_fadc", &fUsingFADC, kInt, 0, 1},
     {"cal_arr_ADCMode", &fADCMode, kInt, 0, 1},
-    {"cal_arr_AdcTimeWindowMin", &fAdcTimeWindowMin, kDouble, 0, 1},
-    {"cal_arr_AdcTimeWindowMax", &fAdcTimeWindowMax, kDouble, 0, 1},
     {"cal_arr_adc_tdc_offset", &fAdcTdcOffset, kDouble, 0, 1},
     {"cal_arr_AdcThreshold", &fAdcThreshold, kDouble, 0, 1},
     {"cal_ped_sample_low", &fPedSampLow, kInt, 0, 1},
@@ -171,8 +169,6 @@ Int_t THcShowerArray::ReadDatabase( const TDatime& date )
 
   fDebugAdc = 0;  // Set ADC debug parameter to false unless set in parameter file
   fADCMode=kADCDynamicPedestal;
-  fAdcTimeWindowMin=0;
-  fAdcTimeWindowMax=10000;
   fAdcTdcOffset=0.0;
   fAdcThreshold=0.;
 
@@ -273,12 +269,23 @@ Int_t THcShowerArray::ReadDatabase( const TDatime& date )
   Double_t cal_arr_cal_const[fNelem];
   Double_t cal_arr_gain_cor[fNelem];
 
+  fAdcTimeWindowMin = new Double_t [fNelem];
+  fAdcTimeWindowMax = new Double_t [fNelem];
+
   DBRequest list1[]={
     {"cal_arr_ped_limit", fPedLimit, kInt, static_cast<UInt_t>(fNelem),1},
     {"cal_arr_cal_const", cal_arr_cal_const, kDouble, static_cast<UInt_t>(fNelem)},
     {"cal_arr_gain_cor",  cal_arr_gain_cor,  kDouble, static_cast<UInt_t>(fNelem)},
+    {"cal_arr_AdcTimeWindowMin", fAdcTimeWindowMin, kDouble, static_cast<UInt_t>(fNelem)},
+    {"cal_arr_AdcTimeWindowMax", fAdcTimeWindowMax, kDouble, static_cast<UInt_t>(fNelem)},
     {0}
   };
+
+   for(UInt_t ip=0;ip<fNelem;ip++) {
+    fAdcTimeWindowMin[ip] = -1000.;
+    fAdcTimeWindowMax[ip] = 1000.;
+   }
+
   gHcParms->LoadParmValues((DBRequest*)&list1, prefix);
 
   // Debug output.
@@ -871,7 +878,7 @@ void THcShowerArray::FillADC_DynamicPedestal()
     Double_t pulseTime   = ((THcSignalHit*) frAdcPulseTime->ConstructedAt(ielem))->GetData();
     Double_t adctdcdiffTime = StartTime-pulseTime;
     Bool_t errorflag     = ((THcSignalHit*) frAdcErrorFlag->ConstructedAt(ielem))->GetData();
-    Bool_t pulseTimeCut  = (adctdcdiffTime > fAdcTimeWindowMin) &&  (adctdcdiffTime < fAdcTimeWindowMax);
+    Bool_t pulseTimeCut  = (adctdcdiffTime > fAdcTimeWindowMin[npad]) &&  (adctdcdiffTime < fAdcTimeWindowMax[npad]);
 
     if (!errorflag && pulseTimeCut) {
       
diff --git a/src/THcShowerArray.h b/src/THcShowerArray.h
index 40edca4..1f05513 100644
--- a/src/THcShowerArray.h
+++ b/src/THcShowerArray.h
@@ -145,8 +145,8 @@ protected:
   static const Int_t kADCDynamicPedestal=1;
   static const Int_t kADCSampleIntegral=2;
   static const Int_t kADCSampIntDynPed=3;
-  Double_t fAdcTimeWindowMin ;
-  Double_t fAdcTimeWindowMax ;
+  Double_t *fAdcTimeWindowMin ;
+  Double_t *fAdcTimeWindowMax ;
   Double_t fAdcThreshold ;
   Double_t fAdcTdcOffset;
 
diff --git a/src/THcShowerPlane.cxx b/src/THcShowerPlane.cxx
index 6f75b54..9dafbaf 100644
--- a/src/THcShowerPlane.cxx
+++ b/src/THcShowerPlane.cxx
@@ -738,8 +738,8 @@ void THcShowerPlane::FillADC_DynamicPedestal()
 {
   Double_t StartTime = 0.0;
   if( fglHod ) StartTime = fglHod->GetStartTime();
-  Double_t AdcTimeWindowMin=static_cast<THcShower*>(fParent)->GetAdcTimeWindowMin();
-  Double_t AdcTimeWindowMax=static_cast<THcShower*>(fParent)->GetAdcTimeWindowMax();
+  Double_t* AdcTimeWindowMin=static_cast<THcShower*>(fParent)->GetAdcTimeWindowMin();
+  Double_t* AdcTimeWindowMax=static_cast<THcShower*>(fParent)->GetAdcTimeWindowMax();
   for (Int_t ielem=0;ielem<frNegAdcPulseInt->GetEntries();ielem++) {
     Int_t    npad         = ((THcSignalHit*) frNegAdcPulseInt->ConstructedAt(ielem))->GetPaddleNumber() - 1;
     Double_t pulseInt     = ((THcSignalHit*) frNegAdcPulseInt->ConstructedAt(ielem))->GetData();
@@ -750,7 +750,7 @@ void THcShowerPlane::FillADC_DynamicPedestal()
     Double_t adctdcdiffTime = StartTime-pulseTime;
     Double_t threshold    = ((THcSignalHit*) frNegAdcThreshold->ConstructedAt(ielem))->GetData();
     Bool_t   errorflag    = ((THcSignalHit*) frNegAdcErrorFlag->ConstructedAt(ielem))->GetData();
-    Bool_t   pulseTimeCut = (adctdcdiffTime > AdcTimeWindowMin) && (adctdcdiffTime < AdcTimeWindowMax);
+    Bool_t   pulseTimeCut = (adctdcdiffTime > AdcTimeWindowMin[npad]) && (adctdcdiffTime < AdcTimeWindowMax[npad]);
     if (!errorflag && pulseTimeCut) {
       fGoodNegAdcPulseIntRaw.at(npad) =pulseIntRaw;
 
@@ -784,7 +784,7 @@ void THcShowerPlane::FillADC_DynamicPedestal()
     Double_t pulseTime    = ((THcSignalHit*) frPosAdcPulseTime->ConstructedAt(ielem))->GetData();
      Double_t adctdcdiffTime = StartTime-pulseTime;
    Bool_t   errorflag    = ((THcSignalHit*) frPosAdcErrorFlag->ConstructedAt(ielem))->GetData();
-    Bool_t   pulseTimeCut = (adctdcdiffTime > AdcTimeWindowMin) &&  (adctdcdiffTime < AdcTimeWindowMax);
+    Bool_t   pulseTimeCut = (adctdcdiffTime > AdcTimeWindowMin[npad]) &&  (adctdcdiffTime < AdcTimeWindowMax[npad]);
     if (!errorflag && pulseTimeCut) {
       fGoodPosAdcPulseIntRaw.at(npad) = pulseIntRaw;
 
-- 
GitLab