Hall C ROOT/C++ Analyzer (hcana)
THcAnalyzer.cxx
Go to the documentation of this file.
1 
15 #include "THcAnalyzer.h"
16 #include "THaRunBase.h"
17 #include "THaBenchmark.h"
18 #include "TList.h"
19 #include "THcParmList.h"
20 #include "THcFormula.h"
21 #include "THcGlobals.h"
22 #include "TMath.h"
23 
24 #include <fstream>
25 #include <algorithm>
26 #include <iomanip>
27 #include <cstring>
28 #include <iostream>
29 
30 using namespace std;
31 
32 
33 // Pointer to single instance of this object
34 //THcAnalyzer* THcAnalyzer::fgAnalyzer = 0;
35 
36 //FIXME:
37 // do we need to "close" scalers/EPICS analysis if we reach the event limit?
38 
39 //_____________________________________________________________________________
41 {
42 
43 }
44 
45 //_____________________________________________________________________________
47 {
48  // Destructor.
49 
50 }
51 
52 //_____________________________________________________________________________
53 void THcAnalyzer::PrintReport(const char* templatefile, const char* ofile)
54 {
59  ifstream ifile;
60  ifile.open(templatefile);
61 
62  if(!ifile.is_open()) {
63  cout << "Error opening template file " << templatefile << endl;
64  return;
65  }
66 
67  ofstream ostr(ofile);
68 
69  if(!ostr.is_open()) {
70  cout << "Error opening report output file " << ofile << endl;
71  return;
72  }
73 
74  LoadInfo(); // Load some run information into gHcParms
75 
76  // In principle, we should allow braces to be escaped. But for
77  // now we won't. Existing template files don't seem to output
78  // any braces
79  for(string line; getline(ifile, line);) {
80  // cout << line << endl;
81  string::size_type start;
82  while((start = line.find('{',0)) != string::npos) {
83  // cout << start << endl;
84  string::size_type end = line.find('}',start);
85  if(end==string::npos) break; // No more expressions on the line
86  string expression=line.substr(start+1,end-start-1);
87  string::size_type formatpos = expression.find(':',0);
88  string format;
89  if(formatpos != string::npos) {
90  format=expression.substr(formatpos+1);
91  expression=expression.substr(0,formatpos);
92  // cout << "|" << expression << "|" << format << "|" << endl;
93  }
94  // Should we first check if the expression can be simply a variable
95  // or index into variable?
96  // For now, first check if it is a string.
97  // If not, then evaluate as an expression.
98  string replacement;
99  if(const char *textstring=gHcParms->GetString(expression)) {
100  // cout << expression << " is a string with value " << textstring << endl;
101  if(format.empty()) format = "%s";
102  replacement=Form(format.c_str(),textstring);
103  } else {
104  THcFormula* formula = new THcFormula("temp",expression.c_str(),gHcParms,gHaVars,gHaCuts);
105  Double_t value=formula->Eval();
106  delete formula; formula = 0;
107  // If the value is close to integer and no format is defined
108  // use "%.0f" to print out integer
109  if(format.empty()) {
110  if(TMath::Abs(value-TMath::Nint(value)) < 0.0000001) {
111  format = "%.0f";
112  } else {
113  format = "%f";
114  }
115  }
116  if(format[format.length()-1] == 'd') {
117  replacement=Form(format.c_str(),TMath::Nint(value));
118  } else {
119  replacement=Form(format.c_str(),value);
120  }
121  }
122  // cout << "Replacement:" << replacement << endl;
123  line.replace(start,end-start+1,replacement);
124  }
125  ostr << line << endl;
126  }
127  ostr.close();
128  ifile.close();
129 
130  return;
131 }
132 
133 //_____________________________________________________________________________
135 {
138  // run number, first event analyzed, number of events, etc.
142  Int_t* runnum;
143  Int_t* firstevent;
144  Int_t* lastevent;
145 
146  THaVar* varptr;
147  varptr = gHcParms->Find("gen_run_number");
148  if(varptr) {
149  runnum = (Int_t*) varptr->GetValuePointer(); // Assume correct type
150  } else {
151  runnum = new Int_t[1];
152  gHcParms->Define("gen_run_number","Run Number", *runnum);
153  }
154  *runnum = fRun->GetNumber();
155 
156  varptr = gHcParms->Find("gen_run_starting_event");
157  if(varptr) {
158  firstevent = (Int_t*) varptr->GetValuePointer(); // Assume correct type
159  } else {
160  firstevent = new Int_t[1];
161  gHcParms->Define("gen_run_starting_event","First event analyzed", *firstevent);
162  }
163  // May not agree with engine event definintions
164  *firstevent = fRun->GetFirstEvent();
165 
166  varptr = gHcParms->Find("gen_event_id_number");
167  if(varptr) {
168  lastevent = (Int_t*)varptr->GetValuePointer(); // Assume correct type
169  } else {
170  lastevent = new Int_t[1];
171  gHcParms->Define("gen_event_id_number","Last event analyzed", *lastevent);
172  }
173  // Not accurate
174  *lastevent = fRun->GetFirstEvent()+fRun->GetNumAnalyzed();
175 }
176 
177 //_____________________________________________________________________________
178 
180 
TLine * line
Hall C analyzer class.
Definition: THcAnalyzer.h:12
int Int_t
virtual ~THcAnalyzer()
Definition: THcAnalyzer.cxx:46
STL namespace.
Short_t Abs(Short_t d)
char * Form(const char *fmt,...)
void PrintReport(const char *templatefile, const char *ofile)
Definition: THcAnalyzer.cxx:53
Enhanced THaFormula for use in report files.
Definition: THcFormula.h:15
double Double_t
ClassImp(THcDCLookupTTDConv) THcDCLookupTTDConv
void LoadInfo()
const char * GetString(const std::string &name) const
Definition: THcParmList.h:38
R__EXTERN class THcParmList * gHcParms
Definition: THcGlobals.h:11
Int_t Nint(T x)