From bd2342c6ae63d8a04efbb88e65257e5410bca329 Mon Sep 17 00:00:00 2001
From: "Stephen A. Wood" <saw@jlab.org>
Date: Thu, 30 Aug 2012 10:12:59 -0400
Subject: [PATCH] Merging in Gabriel's work in progress on better ways of
 retrieving detector parameters from the gHcParms (THcParmList).

---
 examples/PARAM/general.param |   1 +
 src/THcHodoscope.cxx         | 214 ++++++++++++++++++++++++++++++++---
 src/THcHodoscope.h           |  14 ++-
 src/THcHodoscopeHit.cxx      |   5 +
 src/THcParmList.cxx          |  67 +++++++++++
 src/THcParmList.h            |   3 +
 src/THcScintillatorPlane.cxx |   9 +-
 7 files changed, 297 insertions(+), 16 deletions(-)

diff --git a/examples/PARAM/general.param b/examples/PARAM/general.param
index 4a50d14..4dae5e1 100644
--- a/examples/PARAM/general.param
+++ b/examples/PARAM/general.param
@@ -22,3 +22,4 @@ raddeg=3.14159265/180
 #include "PARAM/sdc.pos"
 #include "PARAM/shodo.pos"
 #include "PARAM/scal.pos"
+#include "PARAM/hhodo.param"
diff --git a/src/THcHodoscope.cxx b/src/THcHodoscope.cxx
index d1931a9..5c417dc 100644
--- a/src/THcHodoscope.cxx
+++ b/src/THcHodoscope.cxx
@@ -64,6 +64,7 @@ void THcHodoscope::Setup(const char* name, const char* description)
   // Base class constructor failed?
   if( IsZombie()) return;
 
+  fDebug   = 1;  // Keep this at one while we're working on the code    
   fNPlanes = 4;  // Should get this from parameters
 
   fPlaneNames = new char* [fNPlanes];
@@ -145,6 +146,124 @@ THaAnalysisObject::EStatus THcHodoscope::Init( const TDatime& date )
 
   return fStatus = kOK;
 }
+//_____________________________________________________________________________
+Double_t THcHodoscope::DefineDoubleVariable(const char* fName)
+{
+  // Define a variale of type double by looking it up in the THcParmList
+  char prefix[2];
+  char parname[100];
+  Double_t tmpvar=-1e6;
+  prefix[0]=tolower(GetApparatus()->GetName()[0]);
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,fName);
+  if (gHcParms->Find(parname)) {
+    tmpvar=*(Double_t *)gHcParms->Find(parname)->GetValuePointer();
+    if (fDebug>=1)  cout << parname << " "<< tmpvar << endl;
+  } else {
+    cout << "*** ERROR!!! Could not find " << parname << " in the list of variables! ***" << endl;
+  }
+  return tmpvar;
+}
+
+//_____________________________________________________________________________
+Int_t THcHodoscope::DefineIntVariable(const char* fName)
+{
+  // Define a variale of type int by looking it up in the THcParmList
+  char prefix[2];
+  char parname[100];
+  Int_t tmpvar=-100000;
+  prefix[0]=tolower(GetApparatus()->GetName()[0]);
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,fName);
+  if (gHcParms->Find(parname)) {
+    tmpvar=*(Int_t *)gHcParms->Find(parname)->GetValuePointer();
+    if (fDebug>=1)  cout << parname << " "<< tmpvar << endl;
+  } else {
+    cout << "*** ERROR!!! Could not find " << parname << " in the list of variables! ***" << endl;
+  }
+  return tmpvar;
+}
+
+//_____________________________________________________________________________
+void THcHodoscope::DefineArray(const char* fName, const Int_t index, Double_t *myArray)
+{
+  char prefix[2];
+  char parname[100];
+  //  Int_t tmpvar=-100000;
+   prefix[0]=tolower(GetApparatus()->GetName()[0]);
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,fName);
+  if (gHcParms->Find(parname)) {
+    if (fDebug >=1) cout <<parname;
+    Double_t* p = (Double_t *)gHcParms->Find(parname)->GetValuePointer();
+    for(Int_t i=0;i<index;i++) {
+      myArray[i] = p[i];
+      if (fDebug>=1)    cout << " " << myArray[i];
+    }
+    if (fDebug>=1)  cout << endl;
+
+  }
+  else {
+    cout <<" Could not find "<<parname<<" in the DataBase!!!\n";
+  }
+}
+
+//_____________________________________________________________________________
+void THcHodoscope::DefineArray(const char* fName, char** Suffix, const Int_t index, Double_t *myArray)
+{
+  // Try to read an array made up of what used to be (in the f77 days) a number of variables
+  // example: hscin_1x_center, hscin_1y_center, hscin_2x_center, hscin_2y_center will become scin_center
+  //
+  char prefix[2];
+  char parname[100],parname2[100];
+  //  
+  prefix[0]=tolower(GetApparatus()->GetName()[0]);
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,fName);
+  for(Int_t i=0;i<index;i++) {
+    strcpy(parname2,Form(parname,Suffix[i]));
+    if (gHcParms->Find(parname2)) {
+      if (fDebug >=1) cout <<parname2;
+      myArray[i] = *(Double_t *)gHcParms->Find(parname2)->GetValuePointer();
+      if (fDebug>=1)    cout << " " << myArray[i];
+    }
+    if (fDebug>=1)  cout << endl;
+    else {
+      cout <<" Could not find "<<parname2<<" in the DataBase!!!\n";
+    }
+  }
+}
+
+//_____________________________________________________________________________
+void THcHodoscope::DefineArray(const char* fName, char** Suffix, const Int_t index, Int_t *myArray)
+{
+  // Try to read an array made up of what used to be (in the f77 days) a number of variables
+  // example: hscin_1x_center, hscin_1y_center, hscin_2x_center, hscin_2y_center will become scin_center
+  //
+  char prefix[2];
+  char parname[100],parname2[100];
+  //  
+  prefix[0]=tolower(GetApparatus()->GetName()[0]);
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,fName);
+  for(Int_t i=0;i<index;i++) {
+    strcpy(parname2,Form(parname,Suffix[i]));
+    if (gHcParms->Find(parname2)) {
+      if (fDebug >=1) cout <<parname2;
+      myArray[i] = *(Int_t *)gHcParms->Find(parname2)->GetValuePointer();
+      if (fDebug>=1)    cout << " " << myArray[i];
+    }
+    if (fDebug>=1)  cout << endl;
+    else {
+      cout <<" Could not find "<<parname2<<" in the DataBase!!!\n";
+    }
+  }
+}
 
 //_____________________________________________________________________________
 Int_t THcHodoscope::ReadDatabase( const TDatime& date )
@@ -166,30 +285,28 @@ Int_t THcHodoscope::ReadDatabase( const TDatime& date )
   // the parameter names (e.g. hscin_1x_nr vs. sscin_1x_nr)
 
   prefix[0]=tolower(GetApparatus()->GetName()[0]);
-
+  //
   prefix[1]='\0';
-
   strcpy(parname,prefix);
   strcat(parname,"scin_");
   Int_t plen=strlen(parname);
 
   fNPaddle = new Int_t [fNPlanes];
+  DefineArray("scin_%s_nr",fPlaneNames,fNPlanes,&fNPaddle[0]);
 
+  if (fDebug>=1) cout <<"Testing scin_nr ";
   for(Int_t i=0;i<fNPlanes;i++) {
-    parname[plen] = '\0';
-    strcat(parname,fPlaneNames[i]);
-    strcat(parname,"_nr");
-    fNPaddle[i] = *(Int_t *)gHcParms->Find(parname)->GetValuePointer();
-    cout << parname << " " << fNPaddle[i] << endl;
+    if (fDebug>=1)  cout << " "<<fNPaddle[i];
   }
+  if (fDebug>=1) cout <<endl;
 
   fSpacing = new Double_t [fNPlanes];
+  DefineArray("scin_%s_spacing",fPlaneNames,fNPlanes,&fSpacing[0]);
+  if (fDebug>=1) cout <<"Testing scin_spacing ";
   for(Int_t i=0;i<fNPlanes;i++) {
-    parname[plen] = '\0';
-    strcat(parname,fPlaneNames[i]);
-    strcat(parname,"_spacing");
-    fSpacing[i] = *(Int_t *)gHcParms->Find(parname)->GetValuePointer();
+    if (fDebug>=1)    cout << " " << fSpacing[i];
   }
+  if (fDebug>=1)  cout << endl;
 
   fCenter = new Double_t* [fNPlanes];
   for(Int_t i=0;i<fNPlanes;i++) {
@@ -201,9 +318,80 @@ Int_t THcHodoscope::ReadDatabase( const TDatime& date )
     cout << parname;
     for(Int_t ipad=0;ipad<fNPaddle[i];ipad++) {
       fCenter[i][ipad] = p[ipad];
-      cout << " " << fCenter[i][ipad];
+      if (fDebug>=1)    cout << " " << fCenter[i][ipad];
     }
-    cout << endl;
+    if (fDebug>=1)  cout << endl;
+  }
+
+
+  // GN added
+  // reading variables from *hodo.param
+  prefix[1]='\0';
+
+
+  DBRequest list[]={
+    {Form("%sstart_time_center",prefix),&fStartTimeCenter, kDouble},
+    {Form("%sstart_time_slop",prefix) , &fStartTimeSlop, kDouble},
+    {Form("%sscin_tdc_to_time",prefix), &fScinTdcToTime, kDouble},
+    {Form("%sscin_tdc_min",prefix), &fScinTdcMin, kDouble},
+    {Form("%sscin_tdc_max",prefix), &fScinTdcMax, kDouble},
+    {Form("%stof_tolerance",prefix), &fTofTolerance, kDouble},
+    {0}
+  };
+  gHcParms->LoadParmValues((DBRequest*)&list);
+  cout <<"******* Testing *** "<<fStartTimeCenter<<" "<<fStartTimeSlop;
+  cout <<" "<<fScinTdcToTime;
+  cout <<" "<<fScinTdcMin;
+  cout <<" "<<fScinTdcMax;
+  cout <<" "<<fTofTolerance;
+  cout <<endl<<endl;
+  //  fStartTimeCenter=DefineDoubleVariable("start_time_center");
+  //  fStartTimeSlop=DefineDoubleVariable("start_time_slop");
+  //fScinTdcToTime=DefineDoubleVariable("scin_tdc_to_time");
+  //fScinTdcMin=DefineIntVariable("scin_tdc_min");
+  //fScinTdcMax=DefineIntVariable("scin_tdc_max");
+  //fTofTolerance=DefineDoubleVariable("tof_tolerance");
+  fHodoSlop = new Double_t [fNPlanes];
+  DefineArray("hodo_slop",fNPlanes, &fHodoSlop[0]);
+  /*  if (fDebug>=1) {
+    cout <<"testing hodo_slop ";
+    for(Int_t i=0;i<fNPlanes;i++) {
+      if (fDebug>=1)    cout << " " << fHodoSlop[i];
+    }
+    if (fDebug>=1)  cout << endl;
+   }
+  */
+ 
+  /* try to read *hodo_vel_light
+     NOTE: for HMS these were assigned as a 4x16 array (so we need to pad the planes 
+     that have less than 16 paddles per
+  */
+  Int_t max_paddles=fNPaddle[0];
+  for (Int_t i=1;i<fNPlanes;i++) {
+    max_paddles=(max_paddles > fNPaddle[i])? max_paddles : fNPaddle[i];
+  }
+  cout <<"maxpaddles = "<<max_paddles<<endl;
+  fHodoVelLight = new Double_t* [max_paddles];
+  prefix[1]='\0';
+  strcpy(parname,prefix);
+  strcat(parname,"hodo_vel_light");
+  Int_t k=0;
+  if (gHcParms->Find(parname)) {
+    Double_t* p = (Double_t *)gHcParms->Find(parname)->GetValuePointer();
+    cout <<parname<<" ";
+    for(Int_t i=0;i<max_paddles;i++) {
+      fHodoVelLight[i] = new Double_t [fNPlanes];
+      for(Int_t j=0;j<fNPlanes;j++) {
+	//	cout <<"i = "<<i<<" j = "<<j;
+	fHodoVelLight[i][j] = p[k];
+	k++;
+	if (fDebug>=1)    cout << " " << fHodoVelLight[i][j]<<endl;
+      }
+      if (fDebug>=1)  cout << endl;
+    }
+  }
+  else {
+    cout <<"Could not find "<<parname<<" in the DataBase!!!\n";
   }
 
   fIsInit = true;
diff --git a/src/THcHodoscope.h b/src/THcHodoscope.h
index 23540d1..2b8c8d8 100644
--- a/src/THcHodoscope.h
+++ b/src/THcHodoscope.h
@@ -50,6 +50,14 @@ protected:
 
   // Potential Hall C parameters.  Mostly here for demonstration
   Int_t fNPlanes;
+  Double_t fStartTimeCenter, fStartTimeSlop, fScinTdcToTime;
+  Double_t fTofTolerance;
+  Int_t fScinTdcMin, fScinTdcMax; // min and max TDC values
+  Double_t* fHodoSlop;
+  Double_t** fHodoVelLight;
+  Double_t** fHodoPosSigma;
+  Double_t** fHodoNegSigma;
+
   char** fPlaneNames;
   Int_t* fNPaddle;		// Number of paddles per plane
   Double_t* fSpacing;		// Paddle spacing in cm
@@ -80,7 +88,11 @@ protected:
   void           DeleteArrays();
   virtual Int_t  ReadDatabase( const TDatime& date );
   virtual Int_t  DefineVariables( EMode mode = kDefine );
-
+  Double_t DefineDoubleVariable(const char* fName);
+  Int_t    DefineIntVariable(const char* fName);
+  void DefineArray(const char* fName, const Int_t index, Double_t *myArray);
+  void DefineArray(const char* fName, char** Suffix, const Int_t index, Double_t *myArray);
+  void DefineArray(const char* fName, char** Suffix, const Int_t index, Int_t *myArray);
   enum ESide { kLeft = 0, kRight = 1 };
   
   virtual  Double_t TimeWalkCorrection(const Int_t& paddle,
diff --git a/src/THcHodoscopeHit.cxx b/src/THcHodoscopeHit.cxx
index 9e3ea84..82f1c84 100644
--- a/src/THcHodoscopeHit.cxx
+++ b/src/THcHodoscopeHit.cxx
@@ -8,6 +8,11 @@
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <cstring>
+#include <cstdio>
+#include <cstdlib>
+#include <iostream>
+
 #include "THcHodoscopeHit.h"
 
 using namespace std;
diff --git a/src/THcParmList.cxx b/src/THcParmList.cxx
index 85e0af4..624a673 100644
--- a/src/THcParmList.cxx
+++ b/src/THcParmList.cxx
@@ -8,6 +8,7 @@
 
 #define INCLUDESTR "#include"
 
+#include "THaDB.h"
 #include "TObjArray.h"
 #include "TObjString.h"
 
@@ -23,6 +24,7 @@
 #include <cstdlib>
 
 using namespace std;
+Int_t  fDebug   = 1;  // Keep this at one while we're working on the code    
 
 ClassImp(THcParmList)
 
@@ -340,6 +342,71 @@ void THcParmList::Load( const char* fname, Int_t RunNumber )
   return;
 
 }
+//_____________________________________________________________________________
+Int_t THcParmList::LoadParmValues(const DBRequest* list)
+{
+  // Load a number of entries from the database.
+  // For array entries, the number of elements to be read in
+  // must be given, and the memory already allocated
+  // NOTE: initial code taken wholesale from THaDBFile. 
+  // GN 2012
+  
+  const DBRequest *ti = list;
+  Int_t cnt=0;
+  Int_t this_cnt=0;
+
+  while ( ti && ti->name ) {
+    ///    cout <<"Now at "<<ti->name<<endl;
+    if (ti->nelem>1) {
+      // it is an array, use the appropriate interface
+      switch (ti->type) {
+      case (kDouble) :
+	//	this_cnt = GetArray(system,ti->name,static_cast<Double_t*>(ti->var),
+	//		    ti->expected,date);
+	break;
+      case (kInt) :
+	//	this_cnt = GetArray(system,ti->name,static_cast<Int_t*>(ti->var),
+	//ti->expected,date);
+      break;
+    default:
+	Error("THcParmList","Invalid type to read %s",ti->name);
+	break;
+      }
+
+    } else {
+      switch (ti->type) {
+      case (kDouble) :
+	if (this->Find(ti->name)) {
+	  *static_cast<Double_t*>(ti->var)=*(Double_t *)this->Find(ti->name)->GetValuePointer();
+	} else {
+	  cout << "*** ERROR!!! Could not find " << ti->name << " in the list of variables! ***" << endl;
+	}
+	this_cnt=1;
+
+	break;
+      case (kInt) :
+	if (this->Find(ti->name)) {
+	  *static_cast<Int_t*>(ti->var)=*(Int_t *)this->Find(ti->name)->GetValuePointer();
+	} else {
+	  cout << "*** ERROR!!! Could not find " << ti->name << " in the list of variables! ***" << endl;
+	}
+	this_cnt=1;
+	break;
+      default:
+	Error("THcParmList","Invalid type to read %s",ti->name);
+	break;
+      }
+    }
+    if (this_cnt<=0) {
+      if ( !ti->optional ) {
+	Fatal("THcParmList","Could not find %s in database!",ti->name);
+      }
+    }
+    cnt += this_cnt;
+    ti++;
+  }
+  return cnt;
+}
 
 //_____________________________________________________________________________
 void THcParmList::PrintFull( Option_t* option ) const
diff --git a/src/THcParmList.h b/src/THcParmList.h
index 1e632e0..9cd2c73 100644
--- a/src/THcParmList.h
+++ b/src/THcParmList.h
@@ -7,6 +7,7 @@
 //
 //////////////////////////////////////////////////////////////////////////
 
+#include "THaDB.h"
 #include "THaVarList.h"
 #include "THaTextvars.h"
 
@@ -34,6 +35,8 @@ public:
     TextList->Remove(name);
   }
 
+  Int_t LoadParmValues(const DBRequest* list); // assign values to the variables in list
+
 private:
 
   THaTextvars* TextList;
diff --git a/src/THcScintillatorPlane.cxx b/src/THcScintillatorPlane.cxx
index 350c3de..f355e13 100644
--- a/src/THcScintillatorPlane.cxx
+++ b/src/THcScintillatorPlane.cxx
@@ -214,7 +214,7 @@ Int_t THcScintillatorPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
       break;
     }
 
-
+    // TDC positive hit
     if(hit->fTDC_pos >  0) {
 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,32,0)
       THcSignalHit *sighit = (THcSignalHit*) fPosTDCHits->ConstructedAt(nPosTDCHits++);
@@ -228,6 +228,7 @@ Int_t THcScintillatorPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
       sighit->Set(hit->fCounter, hit->fTDC_pos);
     }
 
+    // TDC negative hit
     if(hit->fTDC_neg >  0) {
 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,32,0)
       THcSignalHit *sighit = (THcSignalHit*) fNegTDCHits->ConstructedAt(nNegTDCHits++);
@@ -242,7 +243,9 @@ Int_t THcScintillatorPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
       sighit->Set(hit->fCounter, hit->fTDC_neg);
     }
 
+    // ADC positive hit
     if(hit->fADC_pos >  0) {
+      //     cout <<"adc pos hit!!\n";
 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,32,0)
       THcSignalHit *sighit = (THcSignalHit*) fPosADCHits->ConstructedAt(nPosADCHits++);
 #else
@@ -255,7 +258,9 @@ Int_t THcScintillatorPlane::ProcessHits(TClonesArray* rawhits, Int_t nexthit)
       sighit->Set(hit->fCounter, hit->fADC_pos);
     }
 
-    if(hit->fADC_neg >  0) {
+    // ADC negative hit
+    if(hit->fADC_neg >  0) {   
+      // cout <<"adc neg hit!!\n";
 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,32,0)
       THcSignalHit *sighit = (THcSignalHit*) fNegADCHits->ConstructedAt(nNegADCHits++);
 #else
-- 
GitLab