From 27a187302f5a3dc1ea398571cc0e81a782c7ee6c Mon Sep 17 00:00:00 2001
From: Jure Bericic <bericic@jlab.org>
Date: Thu, 15 Dec 2016 18:12:40 -0500
Subject: [PATCH] Added new methods to THcRawAdcHit.

---
 src/THcRawAdcHit.cxx   | 46 ++++++++++++++++++++++++++++++++++++++++++
 src/THcRawAdcHit.h     |  6 ++++++
 src/THcShowerArray.cxx | 38 ++++++++++++++++++++++++++++------
 src/THcShowerPlane.cxx | 15 +++++++++++---
 4 files changed, 96 insertions(+), 9 deletions(-)

diff --git a/src/THcRawAdcHit.cxx b/src/THcRawAdcHit.cxx
index b104b17..3fc88ac 100644
--- a/src/THcRawAdcHit.cxx
+++ b/src/THcRawAdcHit.cxx
@@ -179,12 +179,58 @@ Int_t THcRawAdcHit::GetSample(UInt_t iSample) {
     );
     throw std::out_of_range(msg.Data());
   }
+  else if (iSample >= fNSamples && iSample == 0) {
+    return 0;
+  }
   else {
     return fAdcSample[iSample];
   }
 }
 
 
+Double_t THcRawAdcHit::GetAverage(UInt_t iSampleLow, UInt_t iSampleHigh) {
+  if (iSampleHigh >= fNSamples || iSampleLow >= fNSamples) {
+    TString msg = TString::Format(
+      "`THcRawAdcHit::GetAverage`: not this many samples available!"
+    );
+    throw std::out_of_range(msg.Data());
+  }
+  else {
+    Double_t average = 0.0;
+    for (UInt_t i=iSampleLow; i<=iSampleHigh; ++i) {
+      average += fAdcSample[i];
+    }
+    return average / (iSampleHigh - iSampleLow + 1);
+  }
+}
+
+
+Int_t THcRawAdcHit::GetIntegral(UInt_t iSampleLow, UInt_t iSampleHigh) {
+  if (iSampleHigh >= fNSamples || iSampleLow >= fNSamples) {
+    TString msg = TString::Format(
+      "`THcRawAdcHit::GetAverage`: not this many samples available!"
+    );
+    throw std::out_of_range(msg.Data());
+  }
+  else {
+    Int_t integral = 0;
+    for (UInt_t i=iSampleLow; i<=iSampleHigh; ++i) {
+      integral += fAdcSample[i];
+    }
+    return integral;
+  }
+}
+
+
+Double_t THcRawAdcHit::GetData(
+  UInt_t iPedLow, UInt_t iPedHigh, UInt_t iIntLow, UInt_t iIntHigh
+) {
+  return
+    GetIntegral(iIntLow, iIntHigh)
+    - GetAverage(iPedHigh, iPedLow) * (iIntHigh - iIntLow + 1);
+}
+
+
 UInt_t THcRawAdcHit::GetNPulses() {
   return fNPulses;
 }
diff --git a/src/THcRawAdcHit.h b/src/THcRawAdcHit.h
index 09d77bf..5cf9012 100644
--- a/src/THcRawAdcHit.h
+++ b/src/THcRawAdcHit.h
@@ -24,6 +24,12 @@ class THcRawAdcHit : public TObject {
     Int_t GetAdcPeak(UInt_t iPulse=0);
     Int_t GetSample(UInt_t iSample);
 
+    Double_t GetAverage(UInt_t iSampleLow, UInt_t iSampleHigh);
+    Int_t GetIntegral(UInt_t iSampleLow, UInt_t iSampleHigh);
+    Double_t GetData(
+      UInt_t iPedLow, UInt_t iPedHigh, UInt_t iIntLow, UInt_t iIntHigh
+    );
+
     UInt_t GetNPulses();
     UInt_t GetNSamples();
 
diff --git a/src/THcShowerArray.cxx b/src/THcShowerArray.cxx
index da9008d..5bc7c41 100644
--- a/src/THcShowerArray.cxx
+++ b/src/THcShowerArray.cxx
@@ -641,9 +641,19 @@ Int_t THcShowerArray::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
       break;
     }
 
-    // Should probably check that counter # is in range
-		// TODO: Need to include rich FADC data if available.
-    fA[hit->fCounter-1] = hit->GetData(0);
+
+		// Should check that counter # is in range
+		if (fUsingFADC) {
+			fA[hit->fCounter-1] = hit->GetRawAdcHitPos().GetData(
+				fPedSampLow, fPedSampHigh, fDataSampLow, fDataSampHigh
+			);
+			fP[hit->fCounter-1] = hit->GetRawAdcHitPos().GetAverage(
+				fPedSampLow, fPedSampHigh
+			);
+		}
+		else {
+			fA[hit->fCounter-1] = hit->GetData(0);
+		}
 
     if(fA[hit->fCounter-1] > threshold) {
       ngood++;
@@ -782,8 +792,16 @@ Int_t THcShowerArray::AccumulatePedestals(TClonesArray* rawhits, Int_t nexthit)
 
     Int_t element = hit->fCounter - 1; // Should check if in range
 
-		// TODO: Need to include rich FADC data if available.
-    Int_t adc = hit->GetData(0);
+		// Should check that counter # is in range
+		Int_t adc = 0;
+		if (fUsingFADC) {
+			adc = hit->GetRawAdcHitPos().GetData(
+				fPedSampLow, fPedSampHigh, fDataSampLow, fDataSampHigh
+			);
+		}
+		else {
+			adc = hit->GetData(0);
+		}
 
     if(adc <= fPedLimit[element]) {
       fPedSum[element] += adc;
@@ -815,7 +833,15 @@ Int_t THcShowerArray::AccumulatePedestals(TClonesArray* rawhits, Int_t nexthit)
 	break;
       }
 
-      Int_t adc = hit->GetData(0);
+			Int_t adc = 0;
+			if (fUsingFADC) {
+				adc = hit->GetRawAdcHitPos().GetData(
+					fPedSampLow, fPedSampHigh, fDataSampLow, fDataSampHigh
+				);
+			}
+			else {
+				adc = hit->GetData(0);
+			}
 
       cout << "  hit " << ih << ":"
 	   << "  plane =  " << hit->fPlane
diff --git a/src/THcShowerPlane.cxx b/src/THcShowerPlane.cxx
index f673f6a..a0246ce 100644
--- a/src/THcShowerPlane.cxx
+++ b/src/THcShowerPlane.cxx
@@ -322,9 +322,18 @@ Int_t THcShowerPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
     }
 
     // Should probably check that counter # is in range
-		// TODO: Need to include rich FADC data if available.
-    fA_Pos[hit->fCounter-1] = hit->GetData(0);
-    fA_Neg[hit->fCounter-1] = hit->GetData(1);
+		if (fUsingFADC) {
+			fA_Pos[hit->fCounter-1] = hit->GetRawAdcHitPos().GetData(
+				fPedSampLow, fPedSampHigh, fDataSampLow, fDataSampHigh
+			);
+			fA_Neg[hit->fCounter-1] = hit->GetRawAdcHitNeg().GetData(
+				fPedSampLow, fPedSampHigh, fDataSampLow, fDataSampHigh
+			);
+		}
+		else {
+			fA_Pos[hit->fCounter-1] = hit->GetData(0);
+			fA_Neg[hit->fCounter-1] = hit->GetData(1);
+		}
 
     // Sparsify positive side hits, fill the hit list, compute the
     // energy depostion from positive side for the counter.
-- 
GitLab