From 339d3535cf3bf744c4912c892889e73486a1f936 Mon Sep 17 00:00:00 2001
From: "Stephen A. Wood" <saw@jlab.org>
Date: Thu, 9 Aug 2012 15:54:44 -0400
Subject: [PATCH] Add code and example of interpreting ENGINE style database
 files which use the run number to determine which parameter and map files to
 use.

---
 examples/DBASE/test.database |  7 +++++++
 examples/hodtest.C           | 32 +++++++++++++++++++++++---------
 src/THcParmList.cxx          | 34 ++++++++++++++++++++++++++++++++--
 src/THcParmList.h            |  2 +-
 4 files changed, 63 insertions(+), 12 deletions(-)
 create mode 100644 examples/DBASE/test.database

diff --git a/examples/DBASE/test.database b/examples/DBASE/test.database
new file mode 100644
index 0000000..c9d3760
--- /dev/null
+++ b/examples/DBASE/test.database
@@ -0,0 +1,7 @@
+# ENGINE style parameter vs. run number database
+50017
+g_ctp_parm_filename="PARAM/general.param"
+g_decode_map_filename="jan03.map"
+47000-48000
+g_ctp_parm_filename="PARAM/general.param"
+g_decode_map_filename="jan03.map"
diff --git a/examples/hodtest.C b/examples/hodtest.C
index 240160b..6a2ce5f 100644
--- a/examples/hodtest.C
+++ b/examples/hodtest.C
@@ -1,17 +1,27 @@
 
 {
 
-  // Load the Hall C style detector map
-  gHcDetectorMap=new THcDetectorMap();
- // gHcDetectorMap->Load("july04.map");
-gHcDetectorMap->Load("jan03.map");
-//gHcDetectorMap->Load("MAPS/jan03_dg_update.map");
-
-  gHcParms->Load("PARAM/general.param");
   //
   //  Steering script to test hodoscope decoding
   //
   
+  Int_t RunNumber=50017;
+  char* RunFileNamePattern="daq04_%d.log.0";
+  
+  gHcParms->Define("gen_run_number", "Run Number", RunNumber);
+  gHcParms->AddString("g_ctp_database_filename", "DBASE/test.database");
+  
+  gHcParms->Load(gHcParms->GetString("g_ctp_database_filename"), RunNumber);
+
+  // g_ctp_parm_filename and g_decode_map_filename should now be defined
+
+  gHcParms->Load(gHcParms->GetString("g_ctp_parm_filename"));
+
+  // Load the Hall C style detector map
+
+  gHcDetectorMap=new THcDetectorMap();
+  gHcDetectorMap->Load(gHcParms->GetString("g_decode_map_filename"));
+
   // Set up the equipment to be analyzed.
   
   THaApparatus* HMS = new THcHallCSpectrometer("H","HMS");
@@ -37,8 +47,12 @@ gHcDetectorMap->Load("jan03.map");
   
   // Define the run(s) that we want to analyze.
   // We just set up one, but this could be many.
-  THaRun* run = new THaRun( "daq04_50017.log.0" );
-//THaRun* run = new THaRun( "daq03_47851.log.0" );
+  char RunFileName[100];
+  sprintf(RunFileName,RunFileNamePattern,RunNumber);
+  THaRun* run = new THaRun(RunFileName);
+
+  // Eventually need to learn to skip over, or properly analyze
+  // the pedestal events
   run->SetEventRange(1054,100000);
 
   // Define the analysis parameters
diff --git a/src/THcParmList.cxx b/src/THcParmList.cxx
index 52a75b1..85e0af4 100644
--- a/src/THcParmList.cxx
+++ b/src/THcParmList.cxx
@@ -37,7 +37,7 @@ inline static bool IsComment( const string& s, string::size_type pos )
 	   (s[pos] == '#' || s[pos] == ';' || s.substr(pos,2) == "//") );
 }
 
-void THcParmList::Load( const char* fname )
+void THcParmList::Load( const char* fname, Int_t RunNumber )
 {
 
   static const char* const here   = "THcParmList::LoadFromFile";
@@ -60,9 +60,17 @@ void THcParmList::Load( const char* fname )
   string line;
   Int_t nlines_read = 0, nparameters_read = 0;
   char varname[100];
+  Int_t InRunRange;
 
   varname[0] = '\0';
 
+  if(RunNumber > 0) {
+    InRunRange = 0;		// Wait until run number range matching RunNumber is found
+    cout << "Reading Parameters for run " << RunNumber << endl;
+  } else {
+    InRunRange = 1;		// Interpret all lines
+  }
+
   while(nfiles) {
     string current_comment("");
     string existing_comment("");
@@ -98,7 +106,6 @@ void THcParmList::Load( const char* fname )
       continue;
     }
 
-
     // Blank line or comment?
     if( line.empty()
 	|| (start = line.find_first_not_of( whtspc )) == string::npos
@@ -170,6 +177,29 @@ void THcParmList::Load( const char* fname )
 
     // Need to do something to bug out if line is empty
 
+    // If in Engine database mode, check if line is a number range AAAA-BBBB
+    if(RunNumber>0) {
+      if(line.find_first_not_of("0123456789-")==string::npos) { // Interpret as runnum range
+	if( (pos=line.find_first_of("-")) != string::npos) {
+	  Int_t RangeStart=atoi(line.substr(0,pos).c_str());
+	  Int_t RangeEnd=atoi(line.substr(pos+1,string::npos).c_str());
+	  if(RunNumber >= RangeStart && RunNumber <= RangeEnd) {
+	    InRunRange = 1;
+	  } else {
+	    InRunRange = 0;
+	  }
+	} else {		// A single number.  Run 
+	  if(atoi(line.c_str()) == RunNumber) {
+	    InRunRange = 1;
+	  } else {
+	    InRunRange = 0;
+	  }
+	}
+      }
+    }
+
+    if(!InRunRange) continue;
+
     // Interpret left of = as var name
     Int_t valuestartpos=0;  // Stays zero if no = found
     Int_t existinglength=0;
diff --git a/src/THcParmList.h b/src/THcParmList.h
index b0c62b1..1e632e0 100644
--- a/src/THcParmList.h
+++ b/src/THcParmList.h
@@ -19,7 +19,7 @@ public:
   THcParmList();
   virtual ~THcParmList() { Clear(); delete TextList; }
 
-  virtual void Load( const char *fname);
+  virtual void Load( const char *fname, Int_t RunNumber=0);
 
   virtual void PrintFull(Option_t *opt="") const;
 
-- 
GitLab