From af6fa62809ca37d9fe14f465c1c0716a2d43358f Mon Sep 17 00:00:00 2001
From: "Stephen A. Wood" <saw@jlab.org>
Date: Wed, 14 Oct 2015 15:58:27 -0400
Subject: [PATCH] FADC compatibility for raw shower hits   A shower counter hit
 can now be multiple samples, with a different   number of samples allowed for
 positive and negative tubes.   GetData(signal) now returns the sum of all the
 samples.  This should   still be compatible with fastbus ADCs as they are a
 single sample   (which is already the integral.)

---
 src/THcRawShowerHit.cxx | 43 +++++++++++++++++++++++++++++++++--------
 src/THcRawShowerHit.h   | 16 ++++++++++-----
 src/THcShowerArray.cxx  |  2 +-
 src/THcShowerPlane.cxx  | 28 +++++++++++++--------------
 4 files changed, 61 insertions(+), 28 deletions(-)

diff --git a/src/THcRawShowerHit.cxx b/src/THcRawShowerHit.cxx
index 8ced5a7..de2642a 100644
--- a/src/THcRawShowerHit.cxx
+++ b/src/THcRawShowerHit.cxx
@@ -9,40 +9,67 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "THcRawShowerHit.h"
+#include <iostream>
 
 using namespace std;
 
 
 void THcRawShowerHit::SetData(Int_t signal, Int_t data) {
   if(signal==0) {
-    fADC_pos = data;
+    fADC_pos[fNPosSamples++] = data;
   } else if (signal==1) {
-    fADC_neg = data;
+    fADC_neg[fNNegSamples++] = data;
   } 
 }
 
+// Return sum of samples
 Int_t THcRawShowerHit::GetData(Int_t signal) {
+  Int_t adcsum=0;
   if(signal==0) {
-    return(fADC_pos);
+    for(UInt_t isample=0;isample<fNPosSamples;isample++) {
+      adcsum += fADC_pos[isample];
+    }
+    return(adcsum);
   } else if (signal==1) {
-    return(fADC_neg);
+    for(UInt_t isample=0;isample<fNNegSamples;isample++) {
+      adcsum += fADC_neg[isample];
+    }
+    return(adcsum);
   } 
-
   return(-1); // Actually should throw exception
 }
 
+// Return a requested sample
+Int_t THcRawShowerHit::GetData(Int_t signal, UInt_t isample) {
+  if(signal==0) {
+    if(isample >=0 && isample< fNPosSamples) {
+      return(fADC_pos[isample]);
+    }
+  } else if (signal==1) {
+    if(isample >=0 && isample< fNNegSamples) {
+      return(fADC_neg[isample]);
+    }
+  }
+  return(-1);
+}
 //_____________________________________________________________________________
 THcRawShowerHit& THcRawShowerHit::operator=( const THcRawShowerHit& rhs )
 {
   // Assignment operator.
 
+  cout << "YES THIS HAPPENS" << endl;
   THcRawHit::operator=(rhs);
   if ( this != &rhs ) {
     fPlane = rhs.fPlane;
     fCounter = rhs.fCounter;
-    fADC_pos = rhs.fADC_pos;
-    fADC_neg = rhs.fADC_neg;
-
+    for(UInt_t isample=0;isample<fNPosSamples;isample++) {
+      fADC_pos[isample] = rhs.fADC_pos[isample];
+    }
+    for(UInt_t isample=0;isample<fNNegSamples;isample++) {
+      fADC_pos[isample] = rhs.fADC_pos[isample];
+    }
+    fNPosSamples = rhs.fNPosSamples;
+    fNNegSamples = rhs.fNNegSamples;
   }
   return *this;
 }
diff --git a/src/THcRawShowerHit.h b/src/THcRawShowerHit.h
index 934dced..2a2ad48 100644
--- a/src/THcRawShowerHit.h
+++ b/src/THcRawShowerHit.h
@@ -3,30 +3,36 @@
 
 #include "THcRawHit.h"
 
+#define MAXSAMPLES 126
+
 class THcRawShowerHit : public THcRawHit {
 
  public:
   friend class THcShowerPlane;
   friend class THcShowerArray;
 
-  THcRawShowerHit(Int_t plane=0, Int_t counter=0) : THcRawHit(plane, counter), 
-    fADC_pos(-1), fADC_neg(-1){
+  THcRawShowerHit(Int_t plane=0, Int_t counter=0) : 
+    THcRawHit(plane, counter), fNPosSamples(0), fNNegSamples(0) {
   }
   THcRawShowerHit& operator=( const THcRawShowerHit& );
   virtual ~THcRawShowerHit() {}
 
   virtual void Clear( Option_t* opt="" )
-    { fADC_pos = -1; fADC_neg = -1; }
+  { fNPosSamples=0; fNNegSamples=0;}
 
   void SetData(Int_t signal, Int_t data);
   Int_t GetData(Int_t signal);
+  Int_t GetData(Int_t signal, UInt_t isample);
 
   //  virtual Bool_t  IsSortable () const {return kTRUE; }
   //  virtual Int_t   Compare(const TObject* obj) const;
 
  protected:
-  Int_t fADC_pos;
-  Int_t fADC_neg;
+  UInt_t fNPosSamples;
+  UInt_t fNNegSamples;
+  // Is there a way we could pass sample size from the detector initialization
+  Int_t fADC_pos[MAXSAMPLES];
+  Int_t fADC_neg[MAXSAMPLES];
 
  private:
 
diff --git a/src/THcShowerArray.cxx b/src/THcShowerArray.cxx
index 7b684e6..1d46585 100644
--- a/src/THcShowerArray.cxx
+++ b/src/THcShowerArray.cxx
@@ -173,7 +173,7 @@ Int_t THcShowerArray::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
     THcRawShowerHit* hit = (THcRawShowerHit *) rawhits->At(ihit);
 
     // Should probably check that counter # is in range
-    fA[hit->fCounter-1] = hit->fADC_pos;
+    fA[hit->fCounter-1] = hit->GetData(0);
 
     // Do other stuff like comparison to thresholds, signal hits, energy sums
 
diff --git a/src/THcShowerPlane.cxx b/src/THcShowerPlane.cxx
index 81c9f67..208834e 100644
--- a/src/THcShowerPlane.cxx
+++ b/src/THcShowerPlane.cxx
@@ -301,20 +301,20 @@ Int_t THcShowerPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
     }
     
     // Should probably check that counter # is in range
-    fA_Pos[hit->fCounter-1] = hit->fADC_pos;
-    fA_Neg[hit->fCounter-1] = hit->fADC_neg;
+    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.
 
     Double_t thresh_pos = fPosThresh[hit->fCounter -1];
-    if(hit->fADC_pos >  thresh_pos) {
+    if(hit->GetData(0) >  thresh_pos) {
 
       THcSignalHit *sighit =
 	(THcSignalHit*) fPosADCHits->ConstructedAt(nPosADCHits++);
-      sighit->Set(hit->fCounter, hit->fADC_pos);
+      sighit->Set(hit->fCounter, hit->GetData(0));
 
-      fA_Pos_p[hit->fCounter-1] = hit->fADC_pos - fPosPed[hit->fCounter -1];
+      fA_Pos_p[hit->fCounter-1] = hit->GetData(0) - fPosPed[hit->fCounter -1];
 
       fEpos[hit->fCounter-1] += fA_Pos_p[hit->fCounter-1]*
 	fParent->GetGain(hit->fCounter-1,fLayerNum-1,0);
@@ -324,13 +324,13 @@ Int_t THcShowerPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
     // energy depostion from negative side for the counter.
 
     Double_t thresh_neg = fNegThresh[hit->fCounter -1];
-    if(hit->fADC_neg >  thresh_neg) {
+    if(hit->GetData(1) >  thresh_neg) {
 
       THcSignalHit *sighit = 
 	(THcSignalHit*) fNegADCHits->ConstructedAt(nNegADCHits++);
-      sighit->Set(hit->fCounter, hit->fADC_neg);
+      sighit->Set(hit->fCounter, hit->GetData(1));
 
-      fA_Neg_p[hit->fCounter-1] = hit->fADC_neg - fNegPed[hit->fCounter -1];
+      fA_Neg_p[hit->fCounter-1] = hit->GetData(1) - fNegPed[hit->fCounter -1];
 
       fEneg[hit->fCounter-1] += fA_Neg_p[hit->fCounter-1]*
 	fParent->GetGain(hit->fCounter-1,fLayerNum-1,1);
@@ -369,8 +369,8 @@ Int_t THcShowerPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
 	break;
       }
 
-      if(hit->fADC_pos > fPosThresh[hit->fCounter -1] ||
-	 hit->fADC_neg > fNegThresh[hit->fCounter -1]) {
+      if(hit->GetData(0) > fPosThresh[hit->fCounter -1] ||
+	 hit->GetData(1) > fNegThresh[hit->fCounter -1]) {
 	cout << "  plane =  " << hit->fPlane
 	     << "  counter =  " << hit->fCounter
 	     << "  Emean = " << fEmean[hit->fCounter-1]
@@ -411,8 +411,8 @@ Int_t THcShowerPlane::AccumulatePedestals(TClonesArray* rawhits, Int_t nexthit)
       break;
     }
     Int_t element = hit->fCounter - 1; // Should check if in range
-    Int_t adcpos = hit->fADC_pos;
-    Int_t adcneg = hit->fADC_neg;
+    Int_t adcpos = hit->GetData(0);
+    Int_t adcneg = hit->GetData(1);
 
     if(adcpos <= fPosPedLimit[element]) {
       fPosPedSum[element] += adcpos;
@@ -457,8 +457,8 @@ Int_t THcShowerPlane::AccumulatePedestals(TClonesArray* rawhits, Int_t nexthit)
       cout << "  hit " << ih << ":"
 	   << "  plane =  " << hit->fPlane
 	   << "  counter = " << hit->fCounter
-	   << "  ADCpos = " << hit->fADC_pos
-	   << "  ADCneg = " << hit->fADC_neg
+	   << "  ADCpos = " << hit->GetData(0)
+	   << "  ADCneg = " << hit->GetData(1)
 	   << endl;
     }
 
-- 
GitLab