Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaSlotData.cxx
Go to the documentation of this file.
1
2//
3// THaSlotData
4// Data in one slot of one crate from DAQ.
5//
6// THaSlotData contains data from one slot of one crate
7// from a CODA event. Public methods allow to obtain
8// raw data for this crate & slot, or to obtain TDC, ADC,
9// or scaler data for each channel in the slot. Methods
10// clearEvent() and loadData() should only be used by
11// the decoder. WARNING: For efficiency, only the
12// hit counters are zero'd each event, not the data
13// arrays, see below.
14//
15// author Robert Michaels (rom@jlab.org)
16//
18
19#include "Decoder.h"
20#include "Module.h"
21#include "THaSlotData.h"
22#include "THaCrateMap.h"
23#include "TClass.h"
24#include <iostream>
25#include <stdexcept>
26#include <sstream>
27
28using namespace std;
29
30namespace Decoder {
31
32static const bool VERBOSE = true;
33const UInt_t THaSlotData::DEFNCHAN = 128; // Default number of channels
34const UInt_t THaSlotData::DEFNDATA = 1024; // Default number of data words
35const UInt_t THaSlotData::DEFNHITCHAN = 1; // Default number of hits per channel
36
37//_____________________________________________________________________________
39 crate(-1), slot(-1), fModule(nullptr), numhitperchan(0), numraw(0), numchanhit(0),
40 firstfreedataidx(0), numholesdataidx(0), fDebugFile(nullptr),
41 didini(false), fNchan(0) {}
42
43//_____________________________________________________________________________
45 crate(cra), slot(slo), fModule(nullptr), numhitperchan(0), numraw(0), numchanhit(0),
46 firstfreedataidx(0), numholesdataidx(0), fDebugFile(nullptr),
47 didini(false), fNchan(0)
48{
49}
50
51//_____________________________________________________________________________
53
54//_____________________________________________________________________________
56 UInt_t /*ndata*/, // legacy parameter, no longer needed
57 // since data arrays grow as needed
58 UInt_t nhitperchan ) {
59 // Must call define once if you are really going to use this slot.
60 // Otherwise its an empty slot which does not use much memory.
61 crate = cra;
62 slot = slo;
63 didini = true;
64 fNchan = nchan;
65 numhitperchan=nhitperchan;
66 numHits.resize(fNchan);
67 chanlist.resize(fNchan);
68 idxlist.resize(fNchan);
69 chanindex.resize(fNchan);
70 rawData.resize(fNchan);
71 data.resize(fNchan);
72 dataindex.resize(fNchan);
73 numMaxHits.resize(fNchan);
75 numHits.assign(numHits.size(),0);
76}
77
78//_____________________________________________________________________________
80
81 UInt_t modelnum = map->getModel(crate, slot);
82
83 // Search for the model number, which is the sorting key for the std::set
84 auto found = Module::fgModuleTypes().find(Module::ModuleType(nullptr, modelnum));
85
86 if( found != Module::fgModuleTypes().end() ) {
87 const auto& loctype = *found;
88 assert(modelnum == loctype.fMapNum); // else set::find lied
89
90 if (fDebugFile) {
91 *fDebugFile << "THaSlotData:: loctype.fClassName "<< loctype.fClassName<<endl;
92 *fDebugFile << "THaSlotData:: loctype.fMapNum "<< loctype.fMapNum<<endl;
93 *fDebugFile << "THaSlotData:: fTClass ptr = "<<loctype.fTClass<<endl;
94 }
95
96 // Get the ROOT class for this type
97 if( !loctype.fTClass ) {
98 loctype.fTClass = TClass::GetClass( loctype.fClassName );
99 if (fDebugFile) {
100 *fDebugFile << "defining fTClass ptr = "<<loctype.fTClass<<endl;
101 }
102 if (!loctype.fTClass) {
103 if (fDebugFile) {
104 *fDebugFile << "THaSlotData:: SERIOUS problem : fTClass still zero " << endl;
105 }
106 return SD_OK;
107 }
108 }
109
110 if (fDebugFile) *fDebugFile << "THaSlotData:: Found Module !!!! "<<dec<<modelnum<<endl;
111
112 if (fDebugFile) *fDebugFile << "THaSlotData:: Creating fModule"<<endl;
113 if( !fModule || fModule->IsA() != loctype.fTClass ) {
114 fModule.reset(static_cast<Module*>( loctype.fTClass->New() ));
115 } else if (fDebugFile) {
116 *fDebugFile << "THaSlotData:: Reusing existing fModule" << endl;
117 }
118
119 if (!fModule) {
120 cerr << "ERROR: Failure to make module on crate "<<dec<<crate<<" slot "<<slot<<endl;
121 cerr << "usually because the module class is abstract; make sure base class methods are defined"<<endl;
122 if (fDebugFile)
123 *fDebugFile << "failure to make module on crate "<<dec<<crate<<" slot "<<slot<<endl;
124 return SD_ERR;
125 }
126
127 if (fDebugFile) {
128 *fDebugFile << "THaSlotData: fModule successfully created" << endl;
129 *fDebugFile << "THaSlotData:: about to init module "
130 << crate << " " << slot
131 << " header " << hex << map->getHeader(crate, slot)
132 << " model num " << dec << map->getModel(crate, slot)
133 << " bank = " << map->getBank(crate, slot)
134 << endl;
135 }
136
137 // Init first, then SetSlot
138 try {
139 fModule->Init(map->getConfigStr(crate, slot));
140 }
141 catch( const exception& e ) {
142 ostringstream ostr;
143 ostr << "ERROR initializing module for crate " << dec << crate
144 << " slot " << slot << ": " << e.what() << endl;
145 cerr << ostr.str();
146 if( fDebugFile )
147 *fDebugFile << ostr.str();
148 return SD_ERR;
149 }
150 fModule->SetSlot(crate, slot,
151 map->getHeader(crate, slot),
152 map->getMask(crate, slot),
153 map->getModel(crate, slot));
154 fModule->SetBank(map->getBank(crate, slot));
155 if (fDebugFile) {
156 fModule->SetDebugFile(fDebugFile);
157 fModule->DoPrint();
158 }
159 }
160 return SD_OK;
161}
162
163//_____________________________________________________________________________
164UInt_t THaSlotData::LoadIfSlot( const UInt_t* evbuffer, const UInt_t *pstop) {
165 // returns how many words seen.
166 if ( !fModule ) {
167// This is bad and should not happen; it means you didn't define a module
168// for this slot. Check db_cratemap.dat, e.g. erase things that dont exist.
169 cerr << "THaSlotData::ERROR: No module defined for slot. "<<crate<<" "<<slot<<endl;
170 return 0;
171 }
172 if (fDebugFile)
173 *fDebugFile << "THaSlotData::LoadIfSlot: "
174 << dec << crate << " " << slot
175 << " p " << hex << evbuffer << " " << *evbuffer
176 << " " << dec << ((UInt_t(*evbuffer)) >> 27)
177 << hex << " " << pstop << " " << fModule.get()
178 << dec << endl;
179 if ( !fModule->IsSlot( *evbuffer ) ) {
180 if(fDebugFile) *fDebugFile << "THaSlotData:: Not slot ... return ... "<<endl;
181 return 0;
182 }
183 if (fDebugFile) fModule->DoPrint();
184 fModule->Clear();
185 UInt_t wordseen = fModule->LoadBlock(this, evbuffer, pstop);
186 if (fDebugFile)
187 *fDebugFile << "THaSlotData:: after LoadIfSlot: wordseen = "
188 << dec << " " << wordseen << endl;
189 return wordseen;
190}
191
192//_____________________________________________________________________________
194 // returns how many words seen.
195 if ( !fModule ) {
196// This is bad and should not happen; it means you didn't define a module
197// for this slot. Check db_cratemap.dat, e.g. erase things that dont exist.
198 cerr << "THaSlotData::ERROR: No module defined for slot. "<<crate<<" "<<slot<<endl;
199 return 0;
200 }
201 if (fDebugFile)
202 *fDebugFile << "THaSlotData::LoadBank: " << dec << crate << " "<<slot
203 << " pos " << pos << " len " << len << " start word "
204 << hex << *p << " module ptr " << fModule.get() << dec << endl;
205 if (fDebugFile) fModule->DoPrint();
206 fModule->Clear();
207 UInt_t wordseen = fModule->LoadBank(this, p, pos, len);
208 if (fDebugFile) *fDebugFile << "THaSlotData:: after LoadBank: wordseen = "<<dec<<" "<<wordseen<<endl;
209 return wordseen;
210}
211
212//_____________________________________________________________________________
214// for modules that are in multiblock mode, load the next event in the block
215 if ( !fModule ) {
216 cerr << "THaSlotData::ERROR: No module defined for slot. "<<crate<<" "<<slot<<endl;
217 return 0;
218 }
219 return fModule->LoadNextEvBuffer(this);
220}
221
222//_____________________________________________________________________________
223Int_t THaSlotData::loadData(const char* type, UInt_t chan, UInt_t dat, UInt_t raw) {
224
225 const int very_verb=1;
226
227 if( !didini ) {
228 if (very_verb) { // this might be your problem.
229 cout << "THaSlotData: ERROR: Did not init slot."<<endl;
230 cout << " Fix your cratemap."<<endl;
231 }
232 return SD_ERR;
233 }
234 if ( chan >= fNchan) {
235 if (VERBOSE) {
236 cout << "THaSlotData: Warning in loadData: channel ";
237 cout <<chan<<" out of bounds, ignored,"
238 << " on crate " << crate << " slot "<< slot << endl;
239 }
240 return SD_WARN;
241 }
242 if( numchanhit > fNchan) {
243 if (VERBOSE) {
244 cout << "THaSlotData: Warning in loadData: too many "
245 << "channels for crate/slot = " << crate << " " << slot;
246 cout << ": " << numchanhit << " seen."
247 << endl;
248 }
249 return SD_WARN;
250 }
251 if( device.empty() && type ) device = type;
252
253 if (( numchanhit == 0 )||(numHits[chan]==0)) {
261 } else {
262 if (numHits[chan]<numMaxHits[chan]) {
264 } else {
270 } else {
273 for (UInt_t i=0; i<numHits[chan]; i++ ) {
275 }
280 }
281 }
282 }
283
284 // Grow data arrays if necessary
285 if( numraw >= data.size() ) {
286 size_t allocd = 2*data.size();
287 rawData.resize(allocd);
288 data.resize(allocd);
289 }
290 rawData[numraw] = raw;
291 data[numraw++] = dat;
292 if( numHits[chan] == kMaxUInt ) {
293 cout << "THaSlotData: numchanhit, numraw = "<<numchanhit<<" "<<numraw<<endl;
294 if( VERBOSE )
295 cout << "THaSlotData: Warning in loadData: too many hits "
296 << "for module " << device << " in crate/slot = "
297 << dec << crate << " " << slot
298 << " chan = " << chan << endl;
299 return SD_WARN;
300 }
301 numHits[chan]++;
302 return SD_OK;
303}
304
305//_____________________________________________________________________________
307 // NEW (6/2014).
308 return loadData(nullptr, chan, dat, raw);
309}
310
311//_____________________________________________________________________________
313{
314 if (fDebugFile) {
316 return;
317 }
318 cout << "\n THaSlotData contents : " << endl;
319 cout << "This is crate "<<dec<<crate<<" and slot "<<slot<<endl;
320 cout << "Total Amount of Data : " << dec << getNumRaw() << endl;
321 if (getNumRaw() > 0) {
322 cout << "Raw Data Dump: " << hex << endl;
323 } else {
324 // if(getNumRaw() == SD_ERR) cout << "getNumRaw ERROR"<<endl;
325 return;
326 }
327 UInt_t k = 0;
328 for (UInt_t i=0; i<getNumRaw()/5; i++) {
329 for(UInt_t j=0; j<5; j++) cout << getRawData(k++) << " ";
330 cout << endl;
331 }
332 for (UInt_t i=k; i<getNumRaw(); i++) cout << getRawData(i) << " ";
333 bool first = true;
334 ios_base::fmtflags fmt = cout.flags();
335 for ( UInt_t chan=0; chan < fNchan; chan++) {
336 if (getNumHits(chan) > 0) {
337 if (first) {
338 cout << "\nThis is "<<devType()<<" Data : "<<endl;
339 first = false;
340 }
341 cout << dec << "Channel " << chan << " ";
342 cout << "numHits : " << getNumHits(chan) << endl;
343 for (UInt_t hit=0; hit<getNumHits(chan); hit++) {
344 cout << "Hit # "<<dec<<hit;
345 // if(getData(chan,hit) == SD_ERR) cout<<"ERROR: getData"<<endl;
346 cout << " Data = (hex) "<<hex<<getData(chan,hit);
347 cout << " or (decimal) = "<<dec<<getData(chan,hit)<<endl;
348 }
349 }
350 }
351 cout.flags(fmt);
352}
353
354//_____________________________________________________________________________
356 if (!fDebugFile) return;
357 *fDebugFile << "\n THaSlotData contents : " << endl;
358 *fDebugFile << "This is crate "<<dec<<crate<<" and slot "<<slot<<endl;
359 *fDebugFile << "Total Amount of Data : " << dec << getNumRaw() << endl;
360 if (getNumRaw() > 0) {
361 *fDebugFile << "Raw Data Dump: " << hex << endl;
362 } else {
363 // if(getNumRaw() == SD_ERR) *fDebugFile << "getNumRaw ERROR"<<endl;
364 return;
365 }
366 UInt_t k = 0;
367 for (UInt_t i=0; i<getNumRaw()/5; i++) {
368 for(UInt_t j=0; j<5; j++) *fDebugFile << getRawData(k++) << " ";
369 *fDebugFile << endl;
370 }
371 for (UInt_t i=k; i<getNumRaw(); i++) *fDebugFile << getRawData(i) << " ";
372 bool first = true;
373 for ( UInt_t chan=0; chan < fNchan; chan++) {
374 if (getNumHits(chan) > 0) {
375 if (first) {
376 *fDebugFile << "\nThis is "<<devType()<<" Data : "<<endl;
377 first = false;
378 }
379 *fDebugFile << dec << "Channel " << chan << " ";
380 *fDebugFile << "numHits : " << getNumHits(chan) << endl;
381 for (UInt_t hit=0; hit<getNumHits(chan); hit++) {
382 *fDebugFile << "Hit # "<<dec<<hit;
383 // if(getData(chan,hit) == SD_ERR) *fDebugFile<<"ERROR: getData"<<endl;
384 *fDebugFile << " Data = (hex) "<<hex<<getData(chan,hit);
385 *fDebugFile << " or (decimal) = "<<dec<<getData(chan,hit)<<endl;
386 }
387 }
388 }
389}
390
391//_____________________________________________________________________________
393{
394 // first check if it is more favourable to expand it, or to reshuffle
395 auto alloci = dataindex.size();
396 if( numholesdataidx/static_cast<double>(alloci) > 0.5 &&
397 numholesdataidx > numidx ) {
398 // Maybe reshuffle. But how many active dataindex entries would we need?
399 UInt_t nidx = numidx;
400 for (UInt_t i=0; i<numchanhit; i++) {
401 nidx += numMaxHits[ chanlist[i] ];
402 }
403 if( nidx <= alloci ) {
404 // reshuffle, lots of holes
405 VectorUIntNI tmp(alloci);
407 for (UInt_t i=0; i<numchanhit; i++) {
409 for (UInt_t j=0; j<numHits[chan]; j++) {
411 }
414 }
415 dataindex = std::move(tmp);
416 return;
417 }
418 }
419 // If we didn't reshuffle, grow the array instead
420 alloci *= 2;
421 if( firstfreedataidx+numidx > alloci )
422 // Still too small?
423 alloci = 2*(firstfreedataidx+numidx);
424 // FIXME one should check that it doesnt grow too much
425 dataindex.resize(alloci);
426}
427
428} // namespace Decoder
429
430//_____________________________________________________________________________
int Int_t
unsigned int UInt_t
uint32_t chan
#define e(i)
const UInt_t kMaxUInt
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 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 UChar_t len
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
const int SD_WARN
Definition THaSlotData.h:31
const int SD_OK
Definition THaSlotData.h:33
const int SD_ERR
Definition THaSlotData.h:32
static TypeSet_t & fgModuleTypes()
Definition Module.cxx:187
UInt_t getHeader(UInt_t crate, UInt_t slot) const
const char * getConfigStr(UInt_t crate, UInt_t slot) const
Int_t getModel(UInt_t crate, UInt_t slot) const
UInt_t getMask(UInt_t crate, UInt_t slot) const
Int_t getBank(UInt_t crate, UInt_t slot) const
UInt_t LoadBank(const UInt_t *p, UInt_t pos, UInt_t len)
UInt_t getNumRaw() const
Definition THaSlotData.h:50
std::unique_ptr< Module > fModule
Definition THaSlotData.h:86
VectorUIntNI dataindex
Definition THaSlotData.h:96
Int_t loadData(const char *type, UInt_t chan, UInt_t dat, UInt_t raw)
UInt_t getNumHits(UInt_t chan) const
void print_to_file() const
VectorUIntNI chanlist
Definition THaSlotData.h:93
static const UInt_t DEFNHITCHAN
Definition THaSlotData.h:43
VectorUIntNI numMaxHits
Definition THaSlotData.h:97
VectorUIntNI data
Definition THaSlotData.h:99
VectorUIntNI idxlist
Definition THaSlotData.h:94
UInt_t getRawData(UInt_t ihit) const
void compressdataindex(UInt_t numidx)
void define(UInt_t crate, UInt_t slot, UInt_t nchan=DEFNCHAN, UInt_t ndata=DEFNDATA, UInt_t nhitperchan=DEFNHITCHAN)
std::ofstream * fDebugFile
VectorUIntNI chanindex
Definition THaSlotData.h:95
static const UInt_t DEFNDATA
Definition THaSlotData.h:42
UInt_t getData(UInt_t chan, UInt_t hit) const
Int_t loadModule(const THaCrateMap *map)
void compressdataindexImpl(UInt_t numidx)
VectorUIntNI rawData
Definition THaSlotData.h:98
const char * devType() const
UInt_t LoadIfSlot(const UInt_t *evbuffer, const UInt_t *pstop)
static const UInt_t DEFNCHAN
Definition THaSlotData.h:41
virtual void Clear(Option_t *="")
static const bool VERBOSE
std::vector< UInt_t, default_init_allocator< UInt_t > > VectorUIntNI
Definition CustomAlloc.h:38
end
STL namespace.
ClassImp(TPyArg)