Hall C ROOT/C++ Analyzer (hcana)
THcDetectorMap.cxx
Go to the documentation of this file.
1 
11 #include "THcDetectorMap.h"
12 
13 #include "TObjArray.h"
14 #include "TObjString.h"
15 
16 #include <iostream>
17 #include <fstream>
18 #include <cstdlib>
19 
20 using namespace std;
21 
23 
24 inline static bool IsComment( const string& s, string::size_type pos )
25 {
26  return ( pos != string::npos && pos < s.length() &&
27  (s[pos] == '!') );
28 }
29 
30 //_____________________________________________________________________________
31 THcDetectorMap::THcDetectorMap() : fNchans(0), fNIDs(0)
32 {
33 }
34 
35 //_____________________________________________________________________________
37 {
38 }
39 
40 bool THcDetectorMap::compare(const ChaninMod *first, const ChaninMod *second) {
41  // This one not used, but we get a link error if we don't include
42  // a compare method
43  return((first->channel < second->channel)? true: false);
44 }
45 struct Functor
46 {
48  { return((first.channel < second.channel)? true: false);}
49 };
50 //_____________________________________________________________________________
51 Int_t THcDetectorMap::FillMap(THaDetMap *detmap, const char *detectorname)
52 {
61  list<ModChanList>::iterator imod;
62  list<ChaninMod>::iterator ichan;
63  ChaninMod Achan;
64  ModChanList Amod;
65 
66  // Translate detector name into and ID
67  // For now just long if then else. Could get it from the comments
68  // at the beginning of the map file.
69  Int_t did=-1;
70  for(Int_t i=0; i < fNIDs; i++) {
71  if(strcasecmp(detectorname,fIDMap[i].name) == 0) {
72  did = fIDMap[i].id;
73  break;
74  }
75  }
76  if(did < 0) {
77  cout << "FillMap Error: No detector ID registered for " << detectorname << endl;
78  cout << " Using detector id of 0" << endl;
79  did = 0;
80  }
81 
82  mlist.clear();
83  // cout << "fNchans=" << fNchans << endl;
84  for(Int_t ich=0;ich<fNchans;ich++) {
85  if(fTable[ich].did == did) {
86  Int_t roc=fTable[ich].roc;
87  Int_t slot=fTable[ich].slot;
88  Int_t model=fTable[ich].model;
89  // cout << "ROC=" << fTable[ich].roc << " SLOT=" << fTable[ich].slot
90  // << " CHANNEL=" << fTable[ich].channel << endl;
91  Achan.channel = fTable[ich].channel;
92  Achan.plane = fTable[ich].plane;
93  Achan.counter = fTable[ich].counter;
94  Achan.signal = fTable[ich].signal;
95  Achan.refchan = fTable[ich].refchan;
96  Achan.refindex = fTable[ich].refindex;
97  for(imod=mlist.begin(); imod!= mlist.end(); ++imod) {
98  if((*imod).roc == roc && (*imod).slot == slot) {
99  // cout << "Pushing chan " << Achan.channel << " to " << roc
100  // << " " << slot << endl;
101  (*imod).clist.push_back(Achan);
102  break;
103  }
104  }
105  if(imod == mlist.end()) {
106  Amod.roc = roc;
107  Amod.slot = slot;
108  Amod.model = model;
109  Amod.clist.clear();
110  Amod.clist.push_back(Achan);
111  mlist.push_back(Amod);
112  // cout << "New module " << Achan.channel << " to " << roc
113  // << " " << slot << endl;
114  }
115  }
116  }
117 // if(mlist.size() <= 0) {
118  if(mlist.empty()) {
119  return(-1);
120  }
121  Functor f;
122  for(imod=mlist.begin(); imod!= mlist.end(); ++imod) {
123  // cout << "Slot " << (*imod).slot << endl;
124  list<ChaninMod> *clistp = &((*imod).clist);
125  clistp->sort(f);//Sort by channel
126  }
127  // Copy the information to the Hall A style detector map
128  // grouping consecutive channels that are all the same plane
129  // and signal type
130  for(imod=mlist.begin(); imod!= mlist.end(); ++imod) {
131  UShort_t roc = (*imod).roc;
132  UShort_t slot = (*imod).slot;
133  UInt_t model=(*imod).model;
134  // cout << "Slot " << slot << endl;
135  list<ChaninMod> *clistp = &((*imod).clist);
136  Int_t first_chan = -1;
137  Int_t last_chan = -1;
138  Int_t last_plane = -1;
139  Int_t last_signal = -1;
140  Int_t first_counter = -1;
141  Int_t last_counter = -1;
142  Int_t last_refchan = -1;
143  Int_t last_refindex = -1;
144  for(ichan=clistp->begin(); ichan!=clistp->end(); ++ichan) {
145  Int_t this_chan = (*ichan).channel;
146  Int_t this_counter = (*ichan).counter;
147  Int_t this_signal = (*ichan).signal;
148  Int_t this_plane = (*ichan).plane;
149  Int_t this_refchan = (*ichan).refchan;
150  Int_t this_refindex = (*ichan).refindex;
151  if(last_chan+1!=this_chan || last_counter+1 != this_counter
152  || last_plane != this_plane || last_signal!=this_signal
153  || last_refchan != this_refchan || last_refindex != this_refindex) {
154  if(last_chan >= 0) {
155  if(ichan != clistp->begin()) {
156  // cout << "AddModule " << slot << " " << first_chan <<
157  // " " << last_chan << " " << first_counter << endl;
158  detmap->AddModule((UShort_t)roc, (UShort_t)slot,
159  (UShort_t)first_chan, (UShort_t)last_chan,
160  (UInt_t) first_counter, model, (Int_t) last_refindex,
161  (Int_t) last_refchan, (UInt_t)last_plane, (UInt_t)last_signal);
162  }
163  }
164  first_chan = this_chan;
165  first_counter = this_counter;
166  }
167  last_chan = this_chan;
168  last_refchan = this_refchan;
169  last_refindex = this_refindex;
170  last_counter = this_counter;
171  last_plane = this_plane;
172  last_signal = this_signal;
173  // cout << " Channel " << (*ichan).channel << " " <<
174  // (*ichan).plane << " " << (*ichan).counter << endl;
175  }
176  detmap->AddModule((UShort_t)roc, (UShort_t)slot,
177  (UShort_t)first_chan, (UShort_t)last_chan,
178  (UInt_t) first_counter, model, (Int_t) last_refindex,
179  (Int_t) last_refchan, (UInt_t)last_plane, (UInt_t)last_signal);
180  }
181 
182  return(0);
183 }
184 
185 //_____________________________________________________________________________
186 void THcDetectorMap::Load(const char *fname)
187 {
247  static const char* const whtspc = " \t";
248 
249  ifstream ifile;
250 
251  ifile.open(fname);
252  if(!ifile.is_open()) {
253  static const char* const here = "THcDetectorMap::Load";
254  Error(here, "error opening detector map file %s",fname);
255  return; // Need a success/failure argument?
256  }
257  string line;
258 
259  Int_t roc=0;
260  Int_t nsubadd=0;
261  // Int_t mask=0;
262  Int_t bsub=0;
263  Int_t detector=0;
264  Int_t slot=0;
265  Int_t refchan=-1;
266  Int_t refindex=-1;
267  Int_t model=0;
268 
269  fNchans = 0;
270 
271  string::size_type start, pos=0;
272 
273  char varname[100];
274 
275  while(getline(ifile,line)) {
276  // BLank line or comment
277  if(line.empty()) continue;
278  if((start = line.find_first_not_of( whtspc )) == string::npos) continue;
279  if(IsComment(line, start)) { // Check for ID assignments
280  // Get rid of all white space
281  while((pos=line.find_first_of(whtspc)) != string::npos) {
282  line.erase(pos,1);
283  }
284  line.erase(0,1); // Erase "!"
285  if(! ((pos=line.find("_ID=")) == string::npos)) {
286  string::size_type llen = line.length();
287  fIDMap[fNIDs].name = new char [pos+1];
288  strncpy(fIDMap[fNIDs].name,line.c_str(),pos);
289  fIDMap[fNIDs].name[pos] = '\0';
290  start = (pos += 4); // Move to after "="
291  while(pos < llen) {
292  if(isdigit(line.at(pos))) {
293  pos++;
294  } else {
295  break;
296  }
297  }
298  fIDMap[fNIDs++].id = atoi(line.substr(start,pos-start).c_str());
299  }
300  continue;
301  }
302 
303  // cout << "MAPA: " << line << endl;
304 
305  // Remove comment from line
306  while ((pos = line.find_first_of("!", pos+1)) != string::npos) {
307  if(IsComment(line, pos)) {
308  line.erase(pos);
309  break;
310  }
311  }
312 
313  // Get rid of all white space
314  while((pos=line.find_first_of(whtspc)) != string::npos) {
315  line.erase(pos,1);
316  }
317 
318  // cout << "MAPB: " << line << endl;
319 
320  // Decide if line is ROC/NSUBADD/MASK/BSUB/DETECTOR/SLOT = something
321  // or chan, plane, counter[, signal]
322 
323 
324  if((pos=line.find_first_of("=")) != string::npos) { // Setting parameter
325  strcpy(varname, (line.substr(0,pos)).c_str());
326  size_t valuestartpos = pos+1;
327  size_t commapos = line.find_first_of(",");
328  Int_t value;
329  Int_t value2 = -1;
330  if(commapos != string::npos) {
331  // cout << line.substr(valuestartpos,commapos-valuestartpos) << "|"
332  // << line.substr(commapos+1) << "|" << endl;
333  value = atoi(line.substr(valuestartpos,commapos-valuestartpos).c_str());
334  value2 = atoi(line.substr(commapos+1).c_str());
335  } else {
336  value = atoi(line.substr(valuestartpos).c_str());
337  }
338  // Some if statements
339  if(strcasecmp(varname,"detector")==0) {
340  detector = value;
341  refindex = -1; // New detector resets ref time info
342  refchan = -1;
343  } else if (strcasecmp(varname,"roc")==0) {
344  roc = value;
345  refindex = -1; // New roc resets ref time info
346  refchan = -1;
347  } else if (strcasecmp(varname,"nsubadd")==0) {
348  nsubadd = value;
349  } else if (strcasecmp(varname,"mask")==0) { // mask not used here
350  //mask = value;
351  } else if (strcasecmp(varname,"bsub")==0) {
352  bsub = value;
353  } else if (strcasecmp(varname,"slot")==0) {
354  slot = value;
355  refchan = value2; // Deprecating this
356  refindex = -1;
357  } else if (strcasecmp(varname,"refchan")==0) {
358  refchan = value; // Applies to just current slot
359  } else if (strcasecmp(varname,"refindex")==0) {
360  refindex = value; // Applies to just current slot
361  }
362  if(nsubadd == 96) {
363  model = 1877;
364  } else if (nsubadd == 64) {
365  if(bsub == 16) {
366  model = 1872;
367  } else if(bsub == 17) {
368  model = 1881;
369  } else {
370  model = 0;
371  }
372  } else {
373  model = 0;
374  }
375  } else { // Assume channel definition
376  TString values(line.c_str());
377  TObjArray *vararr = values.Tokenize(",");
378  Int_t nvals = vararr->GetLast()+1;
379  if(nvals<3 || nvals>4) {
380  if(nvals > 1) { // Silent for help, noecho, nodebug, override
381  cout << "Map file: Invalid value count: " << line << endl;
382  }
383  continue;
384  }
385  Int_t channel = ((TObjString*)vararr->At(0))->GetString().Atoi();
386  Int_t plane = ((TObjString*)vararr->At(1))->GetString().Atoi();
387  Int_t counter = ((TObjString*)vararr->At(2))->GetString().Atoi();
388  Int_t signal = 0;
389  if(nvals==4) {
390  signal= ((TObjString*)vararr->At(3))->GetString().Atoi();
391  }
392  delete vararr; // Discard result of Tokenize
393 
394  fTable[fNchans].roc=roc;
395  fTable[fNchans].slot=slot;
396  fTable[fNchans].refchan=refchan;
397  fTable[fNchans].refindex=refindex;
398  fTable[fNchans].channel=channel;
399  fTable[fNchans].did=detector;
400  fTable[fNchans].plane=plane;
401  fTable[fNchans].counter=counter;
402  fTable[fNchans].signal=signal;
403  fTable[fNchans].model=model;
404 
405  fNchans++;
406  }
407  }
408  cout << endl << " Detector ID Map" << endl << endl;
409  for(Int_t i=0; i < fNIDs; i++) {
410  cout << " ";
411  cout << fIDMap[i].name << " " << fIDMap[i].id << endl;
412  }
413  cout << endl;
414 
415 }
virtual Int_t FillMap(THaDetMap *detmap, const char *detectorname)
std::list< ChaninMod > clist
IDMap fIDMap[50]
TLine * line
Int_t GetLast() const
bool operator()(const THcDetectorMap::ChaninMod &first, const THcDetectorMap::ChaninMod &second)
unsigned short UShort_t
Channel fTable[10000]
int Int_t
STL namespace.
Class to read and hold a Hall C style detector map.
tuple signal
virtual void Error(const char *method, const char *msgfmt,...) const
std::list< ModChanList > mlist
unsigned int UInt_t
virtual ~THcDetectorMap()
static bool IsComment(const string &s, string::size_type pos)
Definition: THcParmList.cxx:78
virtual void Load(const char *fname)
ClassImp(THcDetectorMap) inline static bool IsComment(const string &s
double f(double x)
tuple model
string::size_type pos
bool compare(const ChaninMod *first, const ChaninMod *second)
char name[80]