From e77389120fa52c58c9b4b33f241c62fcbf136ec8 Mon Sep 17 00:00:00 2001
From: "Stephen A. Wood" <saw@jlab.org>
Date: Wed, 2 Dec 2015 16:08:09 -0500
Subject: [PATCH] Add reference time to hitlist   Base class THcRawHit has null
 GetReference method.  Returns   zero.   THcRawDCHit will get the reference
 time if it was specified in the map file.   It is up to the detector class to
 use the reference time to subtract off   pipeline TDC jitter.

---
 src/THcDriftChamberPlane.cxx |  7 ++++---
 src/THcHitList.cxx           |  7 +++++++
 src/THcRawDCHit.cxx          | 14 ++++++++++++++
 src/THcRawDCHit.h            |  4 ++++
 src/THcRawHit.h              |  8 ++++++--
 5 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/src/THcDriftChamberPlane.cxx b/src/THcDriftChamberPlane.cxx
index 3df5a7b..b83cae3 100644
--- a/src/THcDriftChamberPlane.cxx
+++ b/src/THcDriftChamberPlane.cxx
@@ -313,13 +313,14 @@ Int_t THcDriftChamberPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
     Int_t wireNum = hit->fCounter;
     THcDCWire* wire = GetWire(wireNum);
     Int_t wire_last = -1;
+    Int_t reftime = hit->GetReference(0);
     for(UInt_t mhit=0; mhit<hit->fNHits; mhit++) {
       fNRawhits++;
       /* Sort into early, late and ontime */
       Int_t rawtdc = hit->fTDC[mhit];
-      if(rawtdc < fTdcWinMin) {
+      if((rawtdc-reftime) < fTdcWinMin) {
 	// Increment early counter  (Actually late because TDC is backward)
-      } else if (rawtdc > fTdcWinMax) {
+      } else if ((rawtdc-reftime) > fTdcWinMax) {
 	// Increment late count 
       } else {
 	// A good hit
@@ -330,7 +331,7 @@ Int_t THcDriftChamberPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
 	  // cout << "Extra hit " << fPlaneNum << " " << wireNum << " " << rawtdc << endl;
 	} else {
 	  Double_t time = -StartTime   // (comes from h_trans_scin
-	    - rawtdc*fNSperChan + fPlaneTimeZero;
+	    - (rawtdc-reftime)*fNSperChan + fPlaneTimeZero;
 	  // How do we get this start time from the hodoscope to here
 	  // (or at least have it ready by coarse process)
 	  new( (*fHits)[nextHit++] ) THcDCHit(wire, rawtdc, time, this);
diff --git a/src/THcHitList.cxx b/src/THcHitList.cxx
index 4d8a396..bbac904 100644
--- a/src/THcHitList.cxx
+++ b/src/THcHitList.cxx
@@ -103,6 +103,13 @@ Int_t THcHitList::DecodeToHitList( const THaEvData& evdata ) {
 	// cout << "Signal " << signal << "=" << data << endl;
 	rawhit->SetData(signal,data);
       }
+      // Get the reference time.  Only take the first hit
+      if(d->refchan >= 0) {
+	if( evdata.GetNumHits(d->crate,d->slot,d->refchan) > 0) {
+	  Int_t reftime = evdata.GetData(d->crate, d->slot, d->refchan, 0);
+	  rawhit->SetReference(signal, reftime);
+	}
+      }
     }
   }
   fRawHitList->Sort(fNRawHits);
diff --git a/src/THcRawDCHit.cxx b/src/THcRawDCHit.cxx
index 46ce22a..6032b15 100644
--- a/src/THcRawDCHit.cxx
+++ b/src/THcRawDCHit.cxx
@@ -34,6 +34,20 @@ Int_t THcRawDCHit::GetData(Int_t signal, UInt_t ihit) {
   }
 }
 
+// Set the reference time
+void THcRawDCHit::SetReference(Int_t signal, Int_t reference) {
+  fReferenceTime = reference;
+  fHasRef = kTRUE;
+}
+
+// Get the reference time
+Int_t THcRawDCHit::GetReference(Int_t signal) {
+  if(fHasRef) {
+    return(fReferenceTime);
+  } else {
+    return(0);
+  }
+}
 
 Int_t THcRawDCHit::Compare(const TObject* obj) const
 {
diff --git a/src/THcRawDCHit.h b/src/THcRawDCHit.h
index 18668d6..305a89b 100644
--- a/src/THcRawDCHit.h
+++ b/src/THcRawDCHit.h
@@ -20,8 +20,11 @@ public:
   virtual void Clear( Option_t* opt="" ) { fNHits=0; }
 
   void SetData(Int_t signal, Int_t data);
+  void SetReference(Int_t signal, Int_t reference);
   Int_t GetData(Int_t signal);
   Int_t GetData(Int_t signal, UInt_t ihit);
+  Int_t GetReference(Int_t signal);
+  
 
   virtual Bool_t  IsSortable () const {return kTRUE; }
   virtual Int_t   Compare(const TObject* obj) const;
@@ -30,6 +33,7 @@ public:
 protected:
   UInt_t fNHits;
   Int_t fTDC[MAXHITS];
+  Int_t fReferenceTime;
 
 private:
 
diff --git a/src/THcRawHit.h b/src/THcRawHit.h
index 1ee7d16..dc0cbd3 100644
--- a/src/THcRawHit.h
+++ b/src/THcRawHit.h
@@ -11,8 +11,8 @@
 class THcRawHit : public TObject {
 
 public:
- THcRawHit(Int_t plane=0, Int_t counter=0) :
-  fPlane(plane), fCounter(counter) {};
+  THcRawHit(Int_t plane=0, Int_t counter=0) :
+    fPlane(plane), fCounter(counter), fHasRef(kFALSE) {};
  THcRawHit( const THcRawHit& rhs ) : TObject(rhs) {}
   THcRawHit& operator=( const THcRawHit& rhs )
     { TObject::operator=(rhs); return *this; };
@@ -27,6 +27,9 @@ public:
 
   virtual void SetData(Int_t signal, Int_t data) {};
   virtual Int_t GetData(Int_t signal) {return 0;};
+  virtual void SetReference(Int_t signal, Int_t reference) {};
+  virtual Bool_t HasReference(Int_t signal) {return fHasRef;};
+  virtual Int_t GetReference(Int_t signal) {return 0;};
 
   // Derived objects must be sortable and supply Compare method
   //  virtual Bool_t  IsSortable () const {return kFALSE; }
@@ -36,6 +39,7 @@ public:
 
   Int_t fPlane;
   Int_t fCounter;
+  Bool_t fHasRef;
 
  private:
 
-- 
GitLab