Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaEvData.cxx
Go to the documentation of this file.
1
2//
3// THaEvData
4// Hall A Event Data from One "Event"
5//
6// This is a pure virtual base class. You should not
7// instantiate this directly (and actually CAN not), but
8// rather use THaCodaDecoder or (less likely) a sim class like
9// THaVDCSimDecoder.
10//
11// This class is intended to provide a crate/slot structure
12// for derived classes to use. All derived class must define and
13// implement LoadEvent(const int*). See the header.
14//
15// original author Robert Michaels (rom@jlab.org)
16//
17// modified for abstraction by Ken Rossato (rossato@jlab.org)
18//
19//
21
22#include "THaEvData.h"
23#include "Module.h"
24#include "Helper.h"
25#include "THaSlotData.h"
26#include "THaCrateMap.h"
27#include "THaBenchmark.h"
28#include "TError.h"
29#include <cctype>
30#include <iostream>
31#include <iomanip>
32#include <ctime>
33#include <algorithm>
34#include <cassert>
35#include <utility>
36#include <stdexcept>
37#include <sstream>
38
39using namespace std;
40using namespace Decoder;
41
42// Instances of this object
44
45const Double_t THaEvData::kBig = 1e38;
46static constexpr auto MAXROCSLOT = MAXROC * MAXSLOT;
47
48// If false, signal attempted use of unimplemented features
49#ifndef NDEBUG
51#else
53#endif
54
56
57//_____________________________________________________________________________
59 fMap{nullptr},
60 // FIXME: allocate dynamically?
61 crateslot(MAXROCSLOT),
62 first_decode{true},
63 fTrigSupPS{true},
64 fDataVersion{0},
65 fEpicsEvtType{Decoder::EPICS_EVTYPE}, // default for Hall A
66 buffer{nullptr},
67 fDebugFile{nullptr},
68 event_type{0},
69 event_length{0},
70 event_num{0},
71 run_num{0},
72 run_type{0},
73 data_type{0},
74 trigger_bits{0},
75 evscaler{0},
76 fRunTime(time(nullptr)), // default fRunTime is NOW
77 evt_time{0},
78 fDoBench{false},
79 fInstance{fgInstances.FirstNullBit()},
80 fNeedInit{true},
81 fDebug{0},
82 fExtra{nullptr}
83{
84 fSlotUsed.reserve(MAXROCSLOT/4); // Generous space for a typical setup
85 fSlotClear.reserve(MAXROCSLOT/4);
87 fInstance++;
88}
89
90//_____________________________________________________________________________
92 if( fDoBench ) {
93 Float_t a,b;
94 fBench->Summary(a,b);
95 }
96 delete fExtra;
97 fInstance--;
99}
100
101//_____________________________________________________________________________
102const char* THaEvData::DevType( UInt_t crate, UInt_t slot) const {
103// Device type in crate, slot
104 return ( GoodIndex(crate,slot) ) ?
105 crateslot[idx(crate,slot)]->devType() : " ";
106}
107
108//_____________________________________________________________________________
110{
111 Int_t ret = init_cmap();
112 if( ret != HED_OK ) {
113 ::Error("THaEvData::init", "Error initializing crate map. "
114 "Cannot continue. Fix database.");
115 return ret;
116 }
117 if( fDebugFile ) {
118 *fDebugFile << endl << " THaEvData:: Print of Crate Map" << endl;
119 fMap->print(*fDebugFile);
120 }
121 // if (fMap) fMap->print();
122 ret = init_slotdata();
123 if( ret != HED_OK ) return ret;
124 first_decode = false;
125 fNeedInit = false;
127 return ret;
128}
129
130//_____________________________________________________________________________
132{
133 // Set run time and re-initialize crate map (and possibly other
134 // database parameters for the new time.
135 if( fRunTime == tloc )
136 return;
137 fRunTime = tloc;
138
139 init_cmap();
140}
141
142//_____________________________________________________________________________
144{
145 // Enable/disable run time reporting
146 fDoBench = enable;
147 if( fDoBench ) {
148 if( !fBench ) {
149#if __cplusplus >= 201402L
150 fBench = make_unique<THaBenchmark>();
151#else
152 fBench.reset(new THaBenchmark);
153#endif
154 }
155 } else {
156 fBench = nullptr;
157 }
158}
159
160//_____________________________________________________________________________
162{
163 // Enable/disable helicity decoding
164 SetBit(kHelicityEnabled, enable);
165}
166
167//_____________________________________________________________________________
169{
170 // Enable/disable scaler decoding
171 SetBit(kScalersEnabled, enable);
172}
173
174//_____________________________________________________________________________
176{
177 // Enable/disable prescan mode
178 SetBit(kPrescanMode, enable);
179}
180
181//_____________________________________________________________________________
183{
184 // Set verbosity level. Identical to SetDebug(). Kept for compatibility.
185
186 SetDebug(level);
187}
188
189//_____________________________________________________________________________
191{
192 // Set debug level
193
194 fDebug = level;
195}
196
197//_____________________________________________________________________________
199{
200 switch(evtyp) {
201 case TS_PRESCALE_EVTYPE: // default after Nov 2003
202 fTrigSupPS = true;
203 break;
204 case PRESCALE_EVTYPE:
205 fTrigSupPS = false;
206 break;
207 default:
208 cerr << "SetOrigPS::Warn: PS factors";
209 cerr << " originate only from evtype ";
210 cerr << PRESCALE_EVTYPE << " or ";
211 cerr << TS_PRESCALE_EVTYPE << endl;
212 break;
213 }
214}
215
216//_____________________________________________________________________________
218{
219 TString answer = "PS from ";
220 if (fTrigSupPS) {
221 answer += " Trig Sup evtype ";
222 answer.Append(Form("%d",TS_PRESCALE_EVTYPE));
223 } else {
224 answer += " PS evtype ";
225 answer.Append(Form("%d",PRESCALE_EVTYPE));
226 }
227 return answer;
228}
229
230//_____________________________________________________________________________
231void THaEvData::hexdump(const char* cbuff, size_t nlen)
232{
233 // Hexdump buffer 'cbuff' of length 'nlen'
234 const int NW = 16; const char* p = cbuff;
235 while( p<cbuff+nlen ) {
236 cout << dec << setw(4) << setfill('0') << (size_t)(p-cbuff) << " ";
237 Long_t nelem = TMath::Min((Long_t)NW,(Long_t)(cbuff+nlen-p));
238 for(int i=0; i<NW; i++) {
239 UInt_t c = (i<nelem) ? *(const unsigned char*)(p+i) : 0;
240 cout << " " << hex << setfill('0') << setw(2) << c << dec;
241 } cout << setfill(' ') << " ";
242 for(int i=0; i<NW; i++) {
243 char c = (i<nelem) ? *(p+i) : (char)0;
244 if(isgraph(c)||c==' ') cout << c; else cout << ".";
245 } cout << endl;
246 p += NW;
247 }
248}
249
250//_____________________________________________________________________________
251void THaEvData::SetDefaultCrateMapName( const char* name )
252{
253 // Static function to set fgDefaultCrateMapName. Call this function to set a
254 // global default name for all decoder instances before initialization. This
255 // is usually what you want to do for a given replay.
256
257 if( name && *name ) {
259 }
260 else {
261 ::Error( "THaEvData::SetDefaultCrateMapName", "Default crate map name "
262 "must not be empty" );
263 }
264}
265
266//_____________________________________________________________________________
267void THaEvData::SetCrateMapName( const char* name )
268{
269 // Set fCrateMapName for this decoder instance only
270
271 if( name && *name ) {
272 if( fCrateMapName != name ) {
274 fNeedInit = true;
275 }
276 } else if( fCrateMapName != fgDefaultCrateMapName ) {
278 fNeedInit = true;
279 }
280}
281
282//_____________________________________________________________________________
283// Set up and initialize the crate map
285 if( fCrateMapName.IsNull() )
287 // Make a new crate map object unless we already have one
288 if( !fMap || fCrateMapName != fMap->GetName() ) {
289#if __cplusplus >= 201402L
290 fMap = make_unique<THaCrateMap>(fCrateMapName);
291#else
292 fMap.reset(new THaCrateMap(fCrateMapName));
293#endif
294 }
295 // Initialize the newly created crate map
296 if( fDebug>0 )
297 cout << "Initializing crate map " << endl;
298 if( fMap->init(GetRunTime()) != THaCrateMap::CM_OK )
299 return HED_FATAL; // Can't continue w/o cratemap
300 fNeedInit = false;
301 return HED_OK;
302}
303
304//_____________________________________________________________________________
306{
307 // Activate crate/slot
308 UShort_t idx = slot + MAXSLOT*crate;
309 if( !crateslot[idx] ) {
310#if __cplusplus >= 201402L
311 crateslot[idx] = make_unique<THaSlotData>(crate, slot);
312#else
313 crateslot[idx].reset(new THaSlotData(crate, slot));
314#endif
315 }
316#ifndef NDEBUG
317 else {
318 // Slot already defined
319 assert(crateslot[idx]->getCrate() == crate &&
320 crateslot[idx]->getSlot() == slot);
321 }
322#endif
323 if (fDebugFile) crateslot[idx]->SetDebugFile(fDebugFile);
324 if( !fMap ) return;
325 if( fMap->crateUsed(crate) && fMap->slotUsed(crate,slot)) {
327 ->define( crate, slot, fMap->getNchan(crate,slot) );
328// ndata no longer used
329// ->define( crate, slot, fMap->getNchan(crate,slot),
330// fMap->getNdata(crate,slot) );
331 // Prevent duplicates in fSlotUsed and fSlotClear
332 if( find(ALL(fSlotUsed), idx) == fSlotUsed.end() )
333 fSlotUsed.push_back(idx);
334 if( fMap->slotClear(crate,slot) &&
335 find(ALL(fSlotClear), idx) == fSlotClear.end() )
336 fSlotClear.push_back(idx);
337 if( crateslot[idx]->loadModule(fMap.get()) != SD_OK ) {
338 ostringstream ostr;
339 ostr << "Failed to initialize decoder for crate " << crate << " "
340 << "slot " << slot << ". Fix database or call expert.";
341 throw runtime_error(ostr.str());
342 }
343 }
344}
345
346//_____________________________________________________________________________
348 //TODO
349 cout << "THaEvData::PrintOut() called" << endl;
350}
351
352//_____________________________________________________________________________
353void THaEvData::PrintSlotData( UInt_t crate, UInt_t slot) const {
354 // Print the contents of (crate, slot).
355 if( GoodIndex(crate,slot)) {
356 crateslot[idx(crate,slot)]->print();
357 } else {
358 cout << "THaEvData: Warning: Crate, slot combination";
359 cout << "\nexceeds limits. Cannot print"<<endl;
360 }
361}
362
363//_____________________________________________________________________________
364// To initialize the THaSlotData member on first call to decoder
366{
367 // Update lists of used/clearable slots in case crate map changed
368 if(!fMap) return HED_ERR;
369 for( auto it = fSlotUsed.begin(); it != fSlotUsed.end(); ) {
370 THaSlotData* pSlotData = crateslot[*it].get();
371 UInt_t crate = pSlotData->getCrate();
372 UInt_t slot = pSlotData->getSlot();
373 if( !fMap->crateUsed(crate) || !fMap->slotUsed(crate, slot) ||
374 !fMap->slotClear(crate, slot) ) {
375 auto jt = find(ALL(fSlotClear), *it);
376 if( jt != fSlotClear.end() )
377 fSlotClear.erase(jt);
378 }
379 if( !fMap->crateUsed(crate) || !fMap->slotUsed(crate, slot) ) {
380 assert( find(ALL(fSlotClear), *it) == fSlotClear.end() );
381 crateslot[*it].reset(); // Release unused resources
382 it = fSlotUsed.erase(it);
383 } else
384 ++it;
385 }
386 return HED_OK;
387}
388
389//_____________________________________________________________________________
391 // Disable slots for which no module is defined.
392 // This speeds up the decoder.
393 vector< pair<UInt_t,UInt_t> > to_unset;
394 for( auto roc : fMap->GetUsedCrates() ) {
395 for( auto slot : fMap->GetUsedSlots(roc) ) {
396 assert( fMap->slotUsed(roc,slot) );
397 if ( !crateslot[idx(roc,slot)]->GetModule() ) {
398 cout << "WARNING: No module defined for crate "<<roc<<" slot "<<slot<<endl;
399 cout << "Check db_cratemap.dat for module that is undefined"<<endl;
400 cout << "This crate, slot will be ignored"<<endl;
401 // To avoid undefined behavior, save the roc/slot numbers here so we
402 // can call setUnused outside of these loops. setUnused modifies the
403 // vectors underlying the loops.
404 to_unset.emplace_back(roc, slot);
405 }
406 }
407 }
408 for( const auto& cs : to_unset )
409 fMap->setUnused(cs.first, cs.second);
410}
411
412//_____________________________________________________________________________
414{
415 if( crateslot[idx(roc,slot)] )
416 return crateslot[idx(roc,slot)]->GetModule();
417 return nullptr;
418}
419
420//_____________________________________________________________________________
422{
423 return (fDataVersion = version);
424}
425
426//_____________________________________________________________________________
428{
429 run_num = num;
430 run_type = type;
431 SetRunTime(tloc);
432}
433
434//_____________________________________________________________________________
int Int_t
unsigned int UInt_t
long Long_t
uint32_t time
#define c(i)
bool Bool_t
unsigned short UShort_t
float Float_t
double Double_t
winID h TVirtualViewer3D TVirtualGLPainter p
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 b
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
char name[80]
static constexpr auto MAXROCSLOT
Definition THaEvData.cxx:46
const int SD_OK
Definition THaSlotData.h:33
char * Form(const char *fmt,...)
static const Int_t CM_OK
Definition THaCrateMap.h:75
UInt_t getSlot() const
Definition THaSlotData.h:58
UInt_t getCrate() const
Definition THaSlotData.h:57
void ResetBitNumber(UInt_t bitnumber)
void ResetAllBits(Bool_t value=kFALSE)
void SetBitNumber(UInt_t bitnumber, Bool_t value=kTRUE)
Bool_t fDoBench
Definition THaEvData.h:215
TBits fMsgPrinted
Definition THaEvData.h:230
UInt_t run_num
Definition THaEvData.h:206
virtual Int_t Init()
std::ofstream * fDebugFile
Definition THaEvData.h:204
virtual Int_t init_slotdata()
Bool_t fNeedInit
Definition THaEvData.h:226
UInt_t run_type
Definition THaEvData.h:207
void SetDebug(Int_t level)
std::vector< UShort_t > fSlotClear
Definition THaEvData.h:213
void SetVerbose(Int_t level)
UInt_t idx(UInt_t crate, UInt_t slot) const
Definition THaEvData.h:241
void EnableScalers(Bool_t enable=true)
std::vector< std::unique_ptr< Decoder::THaSlotData > > crateslot
Definition THaEvData.h:195
virtual void PrintOut() const
std::unique_ptr< Decoder::THaCrateMap > fMap
Definition THaEvData.h:183
virtual void FindUsedSlots()
std::unique_ptr< THaBenchmark > fBench
Definition THaEvData.h:216
static void hexdump(const char *cbuff, size_t len)
ULong64_t fRunTime
Definition THaEvData.h:209
Int_t fDataVersion
Definition THaEvData.h:199
Bool_t first_decode
Definition THaEvData.h:197
virtual void SetRunTime(ULong64_t tloc)
void EnablePrescanMode(Bool_t enable=true)
@ kHelicityEnabled
Definition THaEvData.h:165
@ kScalersEnabled
Definition THaEvData.h:166
void SetRunInfo(UInt_t num, UInt_t type, ULong64_t tloc)
Int_t fDebug
Definition THaEvData.h:228
std::vector< UShort_t > fSlotUsed
Definition THaEvData.h:212
TObject * fExtra
Definition THaEvData.h:232
static const Double_t kBig
Definition THaEvData.h:221
static void SetDefaultCrateMapName(const char *name)
virtual Int_t SetDataVersion(Int_t version)
virtual ~THaEvData()
Definition THaEvData.cxx:91
void SetCrateMapName(const char *name)
virtual void PrintSlotData(UInt_t crate, UInt_t slot) const
static TBits fgInstances
Definition THaEvData.h:219
void EnableHelicity(Bool_t enable=true)
void SetOrigPS(Int_t event_type)
void EnableBenchmarks(Bool_t enable=true)
virtual void makeidx(UInt_t crate, UInt_t slot)
TString fCrateMapName
Definition THaEvData.h:225
static Bool_t fgAllowUnimpl
Definition THaEvData.h:222
static TString fgDefaultCrateMapName
Definition THaEvData.h:224
TString GetOrigPS() const
Bool_t fTrigSupPS
Definition THaEvData.h:198
ULong64_t GetRunTime() const
Definition THaEvData.h:60
virtual Decoder::Module * GetModule(UInt_t roc, UInt_t slot) const
virtual Int_t init_cmap()
UInt_t fInstance
Definition THaEvData.h:218
Bool_t GoodIndex(UInt_t crate, UInt_t slot) const
Definition THaEvData.h:255
const char * DevType(UInt_t crate, UInt_t slot) const
void SetBit(UInt_t f)
virtual void Error(const char *method, const char *msgfmt,...) const
TString & Append(char c, Ssiz_t rep=1)
Bool_t IsNull() const
unsigned long long ULong64_t
static const UInt_t PRESCALE_EVTYPE
Definition Decoder.h:47
static const UInt_t MAXSLOT
Definition Decoder.h:35
static const UInt_t EPICS_EVTYPE
Definition Decoder.h:46
static const UInt_t TS_PRESCALE_EVTYPE
Definition Decoder.h:44
static const UInt_t MAXROC
Definition Decoder.h:33
Double_t Min(Double_t a, Double_t b)
STL namespace.
TArc a
ClassImp(TPyArg)