Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaRunBase.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 13/05/00
2
4//
5// THaRunBase
6//
7// Base class of a description of a run.
8//
10
11#include "THaRunBase.h"
12#include "THaRunParameters.h"
13#include "THaEvData.h"
14#include "DAQconfig.h"
15#include "THaPrintOption.h"
16#include "TClass.h"
17#include "TError.h"
18#include <iostream>
19#include <algorithm> // std::copy, std::equal
20#include <iterator> // std::begin, std::end
21
22using namespace std;
23
24static const int UNDEFDATE = 19950101;
25static const char* NOTINIT = "uninitialized run";
26static const char* DEFRUNPARAM = "THaRunParameters";
27static constexpr UInt_t DEFEVTRANGE[2]{1, kMaxUInt};
28
29//_____________________________________________________________________________
30THaRunBase::THaRunBase( const char* description )
31 : TNamed(NOTINIT, description )
32 , fNumber(0)
33 , fType(0)
34 , fDate(UNDEFDATE,0)
35 , fEvtRange{DEFEVTRANGE[0], DEFEVTRANGE[1]}
36 , fNumAnalyzed(0)
37 , fDBRead(false)
38 , fIsInit(false)
39 , fOpened(false)
40 , fAssumeDate(false)
41 , fDataSet(0)
42 , fDataRead(0)
43 , fDataRequired(kDate)
44 , fParam(nullptr)
45 , fRunParamClass(DEFRUNPARAM)
46 , fDataVersion(0)
47 , fExtra(nullptr)
48{
49 // Normal & default constructor
50
52 //FIXME: BCI: should be in RunParameters
54}
55
56//_____________________________________________________________________________
58 : TNamed(rhs)
59 , fNumber(rhs.fNumber)
60 , fType(rhs.fType)
61 , fDate(rhs.fDate)
62 , fEvtRange{rhs.fEvtRange[0], rhs.fEvtRange[1]}
63 , fNumAnalyzed(rhs.fNumAnalyzed)
64 , fDBRead(rhs.fDBRead)
65 , fIsInit(rhs.fIsInit)
66 , fOpened(false)
67 , fAssumeDate(rhs.fAssumeDate)
68 , fDataSet(rhs.fDataSet)
69 , fDataRead(rhs.fDataRead)
70 , fDataRequired(rhs.fDataRequired)
71 , fParam(nullptr)
72 , fRunParamClass(rhs.fRunParamClass)
73 , fDataVersion(rhs.fDataVersion)
74 , fExtra(nullptr)
75{
76 // Copy ctor
77
78 // NB: the run parameter object might inherit from THaRunParameters
79 if( rhs.fParam ) {
80 fParam.reset(dynamic_cast<THaRunParameters*>(rhs.fParam->Clone()));
81 }
82 if( rhs.fExtra )
83 fExtra = rhs.fExtra->Clone();
84}
85
86//_____________________________________________________________________________
88{
89 // Assignment operator. NB: this operator is virtual and is expected
90 // to be extended by derived classes.
91
92 if (this != &rhs) {
94 fNumber = rhs.fNumber;
95 fType = rhs.fType;
96 fDate = rhs.fDate;
97 copy(begin(rhs.fEvtRange), end(rhs.fEvtRange), begin(fEvtRange));
99 fDBRead = rhs.fDBRead;
100 fIsInit = rhs.fIsInit;
101 fOpened = false;
103 fDataSet = rhs.fDataSet;
104 fDataRead = rhs.fDataRead;
106 if( rhs.fParam ) {
107 fParam.reset(dynamic_cast<THaRunParameters*>(rhs.fParam->Clone()));
108 } else
109 fParam = nullptr;
112 delete fExtra;
113 if( rhs.fExtra )
114 fExtra = rhs.fExtra->Clone();
115 else
116 fExtra = nullptr;
117 }
118 return *this;
119}
120
121//_____________________________________________________________________________
123{
124 // Destructor
125
126 delete fExtra; fExtra = nullptr;
127}
128
129//_____________________________________________________________________________
131{
132 // Inspect decoded event data 'evdata' for run parameter data (e.g. prescale
133 // factors) and, if any found, extract the parameters and store them here.
134
135 static const char* const here = "THaRunBase::Update";
136
137 if( !evdata )
138 return -1;
139
140 Int_t ret = 0;
141 // Run date & number
142 if( evdata->IsPrestartEvent() ) {
144 if( !fAssumeDate ) {
145 fDate.Set( evdata->GetRunTime() );
146 fDataSet |= kDate;
147 }
148 SetNumber( evdata->GetRunNum() );
149 SetType( evdata->GetRunType() );
151 ret |= (1<<0);
152 }
153 // Prescale factors
154 if( evdata->IsPrescaleEvent() ) {
156 for(int i=0; i<fParam->GetPrescales().GetSize(); i++) {
157 Int_t psfact = evdata->GetPrescaleFactor(i+1);
158 if( psfact == -1 ) {
159 Error( here, "Failed to decode prescale factor for trigger %d. "
160 "Check raw data file for format errors.", i );
161 return -2;
162 }
163 fParam->Prescales()[i] = psfact;
164 }
166 ret |= (1<<1);
167 }
168#define CFGEVT1 Decoder::DAQCONFIG_FILE1
169#define CFGEVT2 Decoder::DAQCONFIG_FILE2
170 if( evdata->GetEvType() == CFGEVT1 || evdata->GetEvType() == CFGEVT2 ) {
172 auto* srcifo = DAQInfoExtra::GetFrom(evdata->GetExtra());
173 if( !srcifo ) {
174 Warning( here, "Failed to decode DAQ config info from event type %u",
175 evdata->GetEvType() );
176 return -3;
177 }
178 auto* ifo = DAQInfoExtra::GetFrom(fExtra);
179 assert(ifo); // else bug in constructor
180 // Copy info to local run parameters. NB: This may be several MB of data.
181 *ifo = *srcifo;
183 ret |= (1<<2);
184 }
185 return ret;
186}
187
188//_____________________________________________________________________________
189bool THaRunBase::operator==( const THaRunBase& rhs ) const
190{
191 return (fNumber == rhs.fNumber);
192}
193
194//_____________________________________________________________________________
195bool THaRunBase::operator!=( const THaRunBase& rhs ) const
196{
197 return (fNumber != rhs.fNumber);
198}
199
200//_____________________________________________________________________________
201bool THaRunBase::operator<( const THaRunBase& rhs ) const
202{
203 return (fNumber < rhs.fNumber);
204}
205
206//_____________________________________________________________________________
207bool THaRunBase::operator>( const THaRunBase& rhs ) const
208{
209 return (fNumber > rhs.fNumber);
210}
211
212//_____________________________________________________________________________
213bool THaRunBase::operator<=( const THaRunBase& rhs ) const
214{
215 return (fNumber <= rhs.fNumber);
216}
217
218//_____________________________________________________________________________
219bool THaRunBase::operator>=( const THaRunBase& rhs ) const
220{
221 return (fNumber >= rhs.fNumber);
222}
223
224//_____________________________________________________________________________
226{
227 // Reset the run object as if freshly constructed.
228 // However, when opt=="INIT", keep an explicitly set event range and run date
229
230 THaPrintOption sopt(opt);
231 sopt.ToUpper();
232 bool doing_init = (sopt.Contains("INIT"));
233
234 Close();
235 fName = NOTINIT;
236 fNumber = -1;
237 fType = 0;
238 fNumAnalyzed = 0;
239 fDBRead = fIsInit = fOpened = false;
240 fDataSet = fDataRead = 0;
241 fParam->Clear(opt);
242
243 // If initializing and the date was explicitly set, keep the date
244 if( doing_init && fAssumeDate )
245 SetDate(fDate);
246 else
247 ClearDate();
248
249 // Likewise, if initializing, keep any explicitly set parameters
250 if( !doing_init ) {
252 fDataVersion = 0;
253 }
254}
255
256//_____________________________________________________________________________
258{
259 // Reset the run date to "undefined"
260
262 fAssumeDate = fIsInit = false;
263 fDataSet &= ~kDate;
264}
265
266//_____________________________________________________________________________
268{
269 // Reset the range of events to analyze
270
271 copy(begin(DEFEVTRANGE), end(DEFEVTRANGE), begin(fEvtRange));
272}
273
274//_____________________________________________________________________________
276{
277 // Compare two THaRunBase objects via run numbers. Returns 0 when equal,
278 // -1 when 'this' is smaller and +1 when bigger (like strcmp).
279
280 if (this == obj) return 0;
281 const auto *rhs = dynamic_cast<const THaRunBase*>(obj);
282 if( !rhs ) return -1;
283 if ( fNumber < rhs->fNumber ) return -1;
284 else if ( fNumber > rhs->fNumber ) return 1;
285 return 0;
286}
287
288//_____________________________________________________________________________
290{
291 // Test if all the bits set in 'bits' are also set in fDataSet.
292 // 'bits' should consist of the bits defined in EInfoType.
293 return ((bits & fDataSet) == bits);
294}
295
296//_____________________________________________________________________________
298{
299 // Test if all the bits set in 'bits' are also set in fDataRead.
300 // 'bits' should consist of the bits defined in EInfoType.
301 return ((bits & fDataRead) == bits);
302}
303
304//_____________________________________________________________________________
306{
307 // Initialize the run. This reads the run database, checks
308 // whether the data source can be opened, and if so, initializes the
309 // run parameters (run number, time, etc.)
310
311 static const char* const here = "THaRunBase::Init";
312
313 // Set up the run parameter object
314 fParam = nullptr;
315 const char* s = fRunParamClass.Data();
316 TClass* cl = TClass::GetClass(s);
317 if( !cl ) {
318 Error( here, "Run parameter class \"%s\" not "
319 "available. Load library or fix configuration.", s?s:"" );
320 return READ_FATAL;
321 }
323 Error( here, "Class \"%s\" is not a run parameter class.", s );
324 return READ_FATAL;
325 }
326
327 fParam.reset(static_cast<THaRunParameters*>( cl->New() ));
328
329 if( !fParam ) {
330 Error( here, "Unexpected error creating run parameter object "
331 "\"%s\". Call expert.", s );
332 return READ_FATAL;
333 }
334
335 // Clear selected parameters (matters for re-init). This calls Close().
336 Clear("INIT");
337
338 // Open the data source.
339 Int_t retval = Open();
340 if( retval )
341 return retval;
342
343 // Get initial information - e.g., prescan the data source for
344 // the required run date
345 retval = ReadInitInfo(0);
346
347 // Close the data source for now to avoid conflicts. It will be opened
348 // again later.
349 Close();
350
351 if( retval != READ_OK && retval != READ_EOF )
352 return retval;
353
354 if( !HasInfo(fDataRequired) ) {
355 vector<TString> errmsg = { "run date", "run number", "run type",
356 "prescale factors", "DAQ info" };
357 TString errtxt("Missing run parameters: ");
358 UInt_t i = 0, n = 0;
359 for( auto& msg : errmsg ) {
360 if( !HasInfo(BIT(i) & fDataRequired) ) {
361 if( n>0 )
362 errtxt += ", ";
363 errtxt += msg;
364 n++;
365 }
366 i++;
367 }
368 errtxt += ". Run not initialized.";
369 Error( here, "%s", errtxt.Data() );
370
371 return READ_FATAL;
372 }
373
374 // Read the database to obtain additional parameters that are not set
375 // by ReadInitInfo
376 retval = ReadDatabase();
377 if( retval )
378 return retval;
379
380 fIsInit = true;
381 return READ_OK;
382}
383
384//_____________________________________________________________________________
386{
387 return fOpened;
388}
389
390//_____________________________________________________________________________
391void THaRunBase::Print( Option_t* opt ) const
392{
393 // Print definition of run
394 THaPrintOption sopt(opt);
395 sopt.ToUpper();
396 if( sopt.Contains("NAMEDESC") ) {
397 cout << "\"" << GetName() << "\"";
398 if( strcmp( GetTitle(), "") != 0 )
399 cout << " \"" << GetTitle() << "\"";
400 return;
401 }
402
403 cout << IsA()->GetName() << " \"" << GetName() << "\"";
404 if( strcmp( GetTitle(), "") != 0 )
405 cout << " \"" << GetTitle() << "\"";
406 cout << endl;
407 cout << "Run number: " << fNumber << endl;
408 cout << "Run date: " << fDate.AsString() << endl;
409 cout << "Data version: " << fDataVersion << endl;
410 cout << "Requested event range: ";
411 if( std::equal(begin(fEvtRange), end(fEvtRange), begin(DEFEVTRANGE)) )
412 cout << "all";
413 else
414 cout << fEvtRange[0] << "-" << fEvtRange[1];
415 cout << endl;
416
417 if( sopt.Contains("STARTINFO") )
418 return;
419
420 cout << "Analyzed events: " << fNumAnalyzed << endl;
421 cout << "Assume Date: " << fAssumeDate << endl;
422 cout << "Database read: " << fDBRead << endl;
423 cout << "Initialized: " << fIsInit << endl;
424 cout << "Opened: " << fOpened << endl;
425 cout << "Date set/read/req: "
426 << HasInfo(kDate) << " " << HasInfoRead(kDate) << " "
427 << (Bool_t)((kDate & fDataRequired) == kDate) << endl;
428 cout << "Run num set/read/req: "
429 << HasInfo(kRunNumber) << " " << HasInfoRead(kRunNumber) << " "
430 << (Bool_t)((kRunNumber & fDataRequired) == kRunNumber) << endl;
431 cout << "Run type set/read/req: "
432 << HasInfo(kRunType) << " " << HasInfoRead(kRunType) << " "
433 << (Bool_t)((kRunType & fDataRequired) == kRunType) << endl;
434 cout << "Prescales set/rd/req: "
435 << HasInfo(kPrescales) << " " << HasInfoRead(kPrescales) << " "
436 << (Bool_t)((kPrescales & fDataRequired) == kPrescales) << endl;
437 cout << "DAQInfo set/rd/req: "
438 << HasInfo(kDAQInfo) << " " << HasInfoRead(kDAQInfo) << " "
439 << (Bool_t)((kDAQInfo & fDataRequired) == kDAQInfo) << endl;
440
441 if( fParam )
442 fParam->Print(opt);
443}
444
445//_____________________________________________________________________________
447{
448 // Query the run database for the parameters of this run. The actual
449 // work is done in the THaRunParameters object. Usually, the beam and target
450 // parameters are read. Internal function called by Init().
451 //
452 // Return 0 if success.
453
454 static const char* const here = "ReadDatabase";
455
456 if( !fParam ) {
457 Error( here, "No run parameter object defined!?! "
458 "Something is very wrong." );
459 return READ_FATAL;
460 }
461 TDatime undef(UNDEFDATE,0);
462 if( fDate == undef ) {
463 Error( here, "Run date undefined. Cannot read database without a date." );
464 return READ_FATAL;
465 }
466
467 Int_t st = fParam->ReadDatabase(fDate);
468 if( st ) {
469 Error( here, "Failed to read run database, error = %d. "
470 "Cannot continue. Check for typos and ill-formed lines.", st);
471 return READ_FATAL;
472 }
473
474 fDBRead = true;
475 return READ_OK;
476}
477
478//_____________________________________________________________________________
480{
481 // Read initial information from the run data source.
482 // Internal function called by Init(). The default version checks
483 // if run date set and prints an error if not.
484
485 // The data source can be assumed to be open at the time this
486 // routine is called.
487
488 if( !fAssumeDate )
489 Error( "ReadInitInfo", "Run date not set." );
490
491 return READ_OK;
492}
493
494//_____________________________________________________________________________
495void THaRunBase::SetDate( const TDatime& date )
496{
497 // Set the timestamp of this run to 'date'.
498
499 if( fDate != date ) {
500 fDate = date;
501 fIsInit = false;
502 }
503 fAssumeDate = true;
504 fDataSet |= kDate;
505}
506
507//_____________________________________________________________________________
509{
510 // Set timestamp of this run to 'tloc' which is in Unix time
511 // format (number of seconds since 01 Jan 1970).
512
513 TDatime date( tloc );
514 SetDate( date );
515}
516
517//_____________________________________________________________________________
519{
520 // Set bitmask of information items that must be extracted from the run
521 // for Init() to succeed. Can be used to override the default value set by
522 // the class for special cases. Use with caution! The default values are
523 // usually sensible, and further analysis will usually fail if certain
524 // info is not present.
525 //
526 // The 'mask' argument should contain only the bits defined in EInfoType.
527 // Additional bits are ignored with a warning.
528 //
529 // Example: Only require the run date:
530 //
531 // run->SetDataRequired( THaRunBase::kDate );
532 //
533
535 if( (mask & all_info) != mask ) {
536 Warning( "THaRunBase::SetDataRequired", "Illegal bit(s) 0x%x in bitmask "
537 "argument ignored. See EInfoType.", (mask & ~all_info) );
538 }
539 fDataRequired = (mask & all_info);
540}
541
542//_____________________________________________________________________________
544{
545 // Set data format version (meaning is dependent on the underlying
546 // data source). THaRunBase does not use this variable.
547
548 return (fDataVersion = version);
549}
550//_____________________________________________________________________________
552{
553 // Set range of event numbers to analyze. The interpretation of
554 // the event range depends on the analyzer. Usually, it refers
555 // to the range of physics event numbers.
556
557 fEvtRange[0] = first;
558 fEvtRange[1] = last;
559}
560
561//_____________________________________________________________________________
563{
564 // Set number of first event to analyze
565
566 fEvtRange[0] = n;
567}
568
569//_____________________________________________________________________________
571{
572 // Set number of last event to analyze
573
574 fEvtRange[1] = n;
575}
576
577//_____________________________________________________________________________
579{
580 // Change/set the number of the Run.
581
582 fNumber = number;
583 SetName( Form("RUN_%u", number) );
585}
586
587//_____________________________________________________________________________
589{
590 // Set run type
591 fType = type;
593}
594
595//_____________________________________________________________________________
596void THaRunBase::SetRunParamClass( const char* classname )
597{
598 // Set class of run parameters to use
599
600 fRunParamClass = classname;
601}
602
603//_____________________________________________________________________________
605{
606 auto* ifo = DAQInfoExtra::GetFrom(fExtra);
607 if( !ifo )
608 return 0;
609 return ifo->strings.size();
610}
611
612//_____________________________________________________________________________
613const string& THaRunBase::GetDAQConfig( size_t i ) const
614{
615 static const string nullstr;
616 auto* ifo = DAQInfoExtra::GetFrom(fExtra);
617 if( !ifo || i >= ifo->strings.size() )
618 return nullstr;
619 return ifo->strings[i];
620}
621
622//_____________________________________________________________________________
623const string& THaRunBase::GetDAQInfo( const std::string& key ) const
624{
625 static const string nullstr;
626 auto* ifo = DAQInfoExtra::GetFrom(fExtra);
627 if( !ifo )
628 return nullstr;
629 const auto& keyval = ifo->keyval;
630 auto it = keyval.find(key);
631 if( it == keyval.end() )
632 return nullstr;
633 return it->second;
634}
635
636//_____________________________________________________________________________
int Int_t
unsigned int UInt_t
std::map< std::string, std::string > keyval
Definition DAQconfig.h:1
bool Bool_t
const UInt_t kMaxUInt
const char Option_t
#define BIT(n)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
#define CFGEVT1
static const char * NOTINIT
static const int UNDEFDATE
#define CFGEVT2
static const char * DEFRUNPARAM
static constexpr UInt_t DEFEVTRANGE[2]
static const char *const here
Definition THaVar.cxx:64
char * Form(const char *fmt,...)
static DAQconfig * GetFrom(TObject *p)
static void AddTo(TObject *&p, TObject *obj=nullptr)
Definition DAQconfig.cxx:61
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Bool_t InheritsFrom(const char *cl) const override
void Set()
const char * AsString() const
UInt_t GetEvType() const
Definition THaEvData.h:53
Bool_t IsPrescaleEvent() const
Definition THaEvData.h:372
virtual UInt_t GetPrescaleFactor(UInt_t) const
Definition THaEvData.h:105
Bool_t IsPrestartEvent() const
Definition THaEvData.h:362
TObject * GetExtra() const
Definition THaEvData.h:147
ULong64_t GetRunTime() const
Definition THaEvData.h:60
UInt_t GetRunNum() const
Definition THaEvData.h:57
UInt_t GetRunType() const
Definition THaEvData.h:61
Bool_t Contains(const std::string &token) const
virtual Int_t Update(const THaEvData *evdata)
THaRunBase(const char *description="")
virtual Int_t ReadInitInfo(Int_t level=0)
virtual Int_t ReadDatabase()
UInt_t fEvtRange[2]
Definition THaRunBase.h:95
const std::string & GetDAQConfig(size_t i) const
void SetDataRequired(UInt_t mask)
TDatime fDate
Definition THaRunBase.h:93
virtual Bool_t HasInfoRead(UInt_t bits) const
Bool_t fIsInit
Definition THaRunBase.h:98
UInt_t fDataRequired
Definition THaRunBase.h:103
UInt_t fDataRead
Definition THaRunBase.h:102
virtual bool operator>(const THaRunBase &) const
virtual void ClearDate()
virtual void SetType(UInt_t type)
virtual Int_t Init()
UInt_t fNumber
Definition THaRunBase.h:91
size_t GetNConfig() const
TString fRunParamClass
Definition THaRunBase.h:105
virtual void Print(Option_t *opt="") const
virtual bool operator!=(const THaRunBase &) const
Bool_t fOpened
Definition THaRunBase.h:99
UInt_t fType
Definition THaRunBase.h:92
virtual bool operator==(const THaRunBase &) const
virtual void SetNumber(UInt_t number)
std::unique_ptr< THaRunParameters > fParam
Definition THaRunBase.h:104
void SetLastEvent(UInt_t n)
UInt_t fDataSet
Definition THaRunBase.h:101
void SetRunParamClass(const char *classname)
virtual bool operator<(const THaRunBase &) const
Bool_t fAssumeDate
Definition THaRunBase.h:100
virtual Bool_t IsOpen() const
void SetFirstEvent(UInt_t n)
Bool_t fDBRead
Definition THaRunBase.h:97
const std::string & GetDAQInfo(const std::string &key) const
virtual THaRunBase & operator=(const THaRunBase &rhs)
virtual Int_t SetDataVersion(Int_t version)
void SetEventRange(UInt_t first, UInt_t last)
virtual void Clear(Option_t *opt="")
UInt_t fNumAnalyzed
Definition THaRunBase.h:96
virtual Int_t Close()=0
virtual Int_t Compare(const TObject *obj) const
virtual bool operator<=(const THaRunBase &) const
virtual bool operator>=(const THaRunBase &) const
virtual Int_t Open()=0
virtual void SetDate(const TDatime &date)
TObject * fExtra
Definition THaRunBase.h:107
virtual Bool_t HasInfo(UInt_t bits) const
void ClearEventRange()
Int_t fDataVersion
Definition THaRunBase.h:106
virtual ~THaRunBase()
const char * GetName() const override
const char * GetTitle() const override
TString fName
virtual void SetName(const char *name)
TClass * IsA() const override
TNamed & operator=(const TNamed &rhs)
virtual TObject * Clone(const char *newname="") const
virtual void Warning(const char *method, const char *msgfmt,...) const
static TClass * Class()
virtual void Error(const char *method, const char *msgfmt,...) const
void Clear()
const char * Data() const
const Int_t n
end
STL namespace.
ClassImp(TPyArg)