Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
BdataLoc.cxx
Go to the documentation of this file.
1//*-- Author : Bob Michaels, March 2002
2
4//
5// BdataLoc, CrateLoc, WordLoc
6//
7// Utility classes for THaDecData generic raw data decoder
8//
10
11#include "BdataLoc.h"
12#include "THaEvData.h"
13#include "THaGlobals.h"
14#include "THaVarList.h"
15#include "TObjArray.h"
16#include "TObjString.h"
17#include "TError.h"
18#include "TClass.h"
19
20#include <cstring> // for memchr
21#include <cstdlib> // for strtoul
22#include <cerrno>
23#include <utility>
24#include <iostream>
25#include <cassert>
26
27using namespace std;
28
31
32TypeIter_t CrateLoc::fgThisType = DoRegister( BdataLocType( "CrateLoc", "crate", 4 ));
33TypeIter_t CrateLocMulti::fgThisType = DoRegister( BdataLocType( "CrateLocMulti", "multi", 4 ));
34TypeIter_t WordLoc::fgThisType = DoRegister( BdataLocType( "WordLoc", "word", 4 ));
35TypeIter_t RoclenLoc::fgThisType = DoRegister( BdataLocType( "RoclenLoc", "roclen", 2 ));
36
37// Shorthands
38#define kDefine THaAnalysisObject::kDefine
39#define kDelete THaAnalysisObject::kDelete
40#define kOK THaAnalysisObject::kOK
41#define kInitError THaAnalysisObject::kInitError
42
43//_____________________________________________________________________________
45{
46 // Destructor - clean up global variable(s)
47
48 // The following call will always invoke the base class instance
49 // BdataLoc::DefineVariables, even when destroying derived class
50 // objects. But that's OK since the kDelete mode is the same for
51 // the entire class hierarchy; it simply removes the name.
52
54}
55
56//_____________________________________________________________________________
58{
59 // Export this object's data as a global variable
60
61 // Note that the apparatus prefix is already part of this object's name,
62 // e.g. "D.syncroc1", where "D" is the name of the parent THaDecData object
63
64 if( mode == kDefine && TestBit(kIsSetup) ) return kOK;
66
67 Int_t ret = kOK;
68 if( mode == kDefine ) {
69 if( !gHaVars->Define( GetName(), data ) )
70 ret = kInitError;
71 } else
73
74 return ret;
75}
76
77//_____________________________________________________________________________
79{
80 // Check given parameters of call to Configure for obvious errors.
81 // Internal helper function.
82
83 // Invalid call parameter?
84 if( !params || start < 0 || start + GetNparams() > params->GetLast()+1 )
85 return 1;
86
87 // Invalid parameter array data?
88 for( Int_t ip = start; ip < start + GetNparams(); ++ip ) {
89 if( params->At(ip)->IsA() != TObjString::Class() )
90 return 2;
91 auto* os = dynamic_cast<TObjString*>(params->At(ip));
92 if( !os )
93 return 3;
94 if( os->String().IsNull() )
95 return 4;
96 }
97 return 0;
98}
99
100//_____________________________________________________________________________
102{
103 // Initialize this object from the TObjString parameters given in the params
104 // array, starting at index 'start'
105
106 Int_t ret = CheckConfigureParams( params, start );
107 if( ret )
108 return ret;
109
110 SetName( GetString( params, start ) );
111 SetTitle( GetName() );
112
113 crate = GetString( params, start+1 ).Atoi();
114
115 return 0;
116}
117
118//_____________________________________________________________________________
120{
121 // Local storage for all defined BdataLoc types. Initialize here on first use
122 // (cf. http://www.parashift.com/c++-faq/static-init-order-on-first-use-members.html)
123
124 static auto* fgBdataLocTypes = new TypeSet_t;
125
126 return *fgBdataLocTypes;
127}
128
129//_____________________________________________________________________________
131{
132 // Add given info in fgBdataLocTypes
133
134 if( !info.fClassName ||!*info.fClassName ) {
135 ::Error( "BdataLoc::DoRegister", "Attempt to register empty class name. "
136 "Coding error. Call expert." );
137 return fgBdataLocTypes().end();
138 }
139
140 pair< TypeIter_t, bool > ins = fgBdataLocTypes().insert(info);
141
142 if( !ins.second ) {
143 ::Error( "BdataLoc::DoRegister", "Attempt to register duplicate database "
144 "key \"%s\". Coding error. Call expert.", info.fDBkey );
145 return fgBdataLocTypes().end();
146 }
147 // NB: std::set guarantees that iterators remain valid on further insertions,
148 // so this return value will remain good, unlike, e.g., std::vector iterators.
149 return ins.first;
150}
151
152//_____________________________________________________________________________
154{
155 // Print name (and type for "FULL")
156
157 cout << " " << fName << "\t";
158 if( fName.Length() < 7 ) cout << "\t";
159 TString option(opt);
160 if( option.Contains("FULL") ) {
161 cout << "type = " << GetTypeKey();
162 }
163}
164
165//_____________________________________________________________________________
166void BdataLoc::Print( Option_t* opt ) const
167{
168 // Print name and data value
169
170 PrintNameType(opt);
171 TString option(opt);
172 if( option.Contains("FULL") ) {
173 cout << "\t crate = " << crate;
174 }
175 cout << "\t data = " << data << endl;
176}
177
178//_____________________________________________________________________________
180{
181 // Initialize CrateLoc from given parameters
182
183 Int_t ret = BdataLoc::Configure( params, start );
184 if( ret )
185 return ret;
186
187 slot = GetString( params, start+2 ).Atoi();
188 chan = GetString( params, start+3 ).Atoi();
189
190 return 0;
191}
192
193//_____________________________________________________________________________
194void CrateLoc::Load( const THaEvData& evdata )
195{
196 // Load one data word from crate/slot/chan address
197
198 if( evdata.GetNumHits(crate,slot,chan) > 0 ) {
199 data = evdata.GetData(crate,slot,chan,0);
200 }
201}
202
203//_____________________________________________________________________________
205{
206 // Print name and data
207
208 PrintNameType(opt);
209 TString option(opt);
210 if( option.Contains("FULL") ) {
211 cout << "\t cr/sl/ch = " << crate << "/" << slot << "/" << chan;
212 }
213}
214
215//_____________________________________________________________________________
216void CrateLoc::Print( Option_t* opt ) const
217{
218 // Print name and data
219
221 cout << "\t data = " << data << endl;
222}
223
224//_____________________________________________________________________________
226{
227 // For multivalued data (multihit modules), define a variable-sized global
228 // variable on the vector<UInt_t> rdata member.
229
230 if( mode == kDefine && TestBit(kIsSetup) ) return kOK;
232
233 Int_t ret = kOK;
234
235 if( mode == kDefine ) {
236 TString comment = GetName();
237 comment.Append(" multihit data");
238 if( !gHaVars->Define( GetName(), comment, rdata ) )
239 ret = kInitError;
240 } else
242
243 return ret;
244}
245
246//_____________________________________________________________________________
247void CrateLocMulti::Load( const THaEvData& evdata )
248{
249 // Load all decoded hits from crate/slot/chan address
250
251 data = 0;
252 for( UInt_t i = 0; i < evdata.GetNumHits(crate, slot, chan); ++i ) {
253 rdata.push_back( evdata.GetData(crate,slot,chan,i) );
254 }
255}
256
257//_____________________________________________________________________________
259{
260 // Print the data array
261
262 cout << "\t data = ";
263 if( rdata.empty() )
264 cout << "(no hits)";
265 else {
266 for( auto data : rdata ) {
267 cout << " " << data;
268 }
269 }
270}
271
272//_____________________________________________________________________________
274{
275 // Print name and all data values
276
278 PrintMultiData(opt);
279 cout << endl;
280}
281
282//_____________________________________________________________________________
284{
285 // Initialize WordLoc from given parameters
286
287 Int_t ret = BdataLoc::Configure( params, start );
288 if( ret )
289 return ret;
290
291 // Convert header word, given as a hex string, to unsigned int32
292 const char* hs = GetString( params, start+2 ).Data();
293 char* p = nullptr;
294 errno = 0;
295 unsigned long li = strtoul( hs, &p, 16 );
296 if( errno || *p ) return 10;
297 if( li > kMaxUInt ) return 11;
298 header = static_cast<UInt_t>(li);
299
300 ntoskip = GetString( params, start+3 ).Atoi();
301
302 return 0;
303}
304
305//_____________________________________________________________________________
306void WordLoc::Load( const THaEvData& evdata )
307{
308 // Load data at header/notoskip position from crate data buffer
309
310 using rawdata_t = const UInt_t;
311
312 UInt_t roclen = evdata.GetRocLength(crate);
313 if( roclen < ntoskip+1 ) return;
314
315 rawdata_t* cratebuf = evdata.GetRawDataBuffer(crate);
316 assert(cratebuf); // Must exist if roclen > 0
317 rawdata_t* endp = cratebuf+roclen+1;
318
319 // Accelerated search for the header word. Coded explicitly because there
320 // is no "memstr" in the standard library.
321
322 //FIXME: Can this be made faster because each header is followed by the offset
323 // to the next header?
324 //FIXME: What about byte order? Are the raw data always a certain endianness?
325
326 // Get the first byte of the header, regardless of byte order
327 int h = ((UChar_t*)&header)[0];
328 rawdata_t* p = cratebuf+2;
329 while( p < endp &&
330 (p = (rawdata_t*)memchr(p,h,sizeof(rawdata_t)*(endp-p-1)+1)) &&
331 p <= endp-ntoskip-1 ) {
332 // The header must be aligned to a word boundary
333 assert(p > cratebuf);
334 size_t off = ((char*)p-(char*)cratebuf) & (sizeof(rawdata_t)-1); // same as % sizeof()
335 if( off != 0 ) {
336 p = (rawdata_t*)((char*)p + sizeof(rawdata_t)-off);
337 continue;
338 }
339 // Compare all 4 bytes of the header word
340 if( memcmp(p,&header,sizeof(rawdata_t)) == 0 ) {
341 // Fetch the requested word (as UInt_t, i.e. 4 bytes)
342 // BTW, notoskip == 0 makes no sense since it would fetch the header itself
343 data = *(p+ntoskip);
344 break;
345 }
346 ++p; // Next rawdata_t word
347 }
348}
349
350//_____________________________________________________________________________
351void WordLoc::Print( Option_t* opt ) const
352{
353 // Print name and data
354
355 PrintNameType(opt);
356 TString option(opt);
357 if( option.Contains("FULL") ) {
358 cout << "\t cr/hdr/off = " << crate << "/0x"
359 << hex << header << dec << "/" << ntoskip;
360 }
361 cout << "\t data = " << data << endl;
362}
363
364//_____________________________________________________________________________
365void RoclenLoc::Load( const THaEvData& evdata )
366{
367 // Get event length for this crate
368
369 data = evdata.GetRocLength(crate);
370}
371
372//_____________________________________________________________________________
#define kOK
Definition BdataLoc.cxx:40
BdataLoc::TypeSet_t TypeSet_t
Definition BdataLoc.cxx:29
#define kInitError
Definition BdataLoc.cxx:41
#define kDelete
Definition BdataLoc.cxx:39
BdataLoc::TypeIter_t TypeIter_t
Definition BdataLoc.cxx:30
#define kDefine
Definition BdataLoc.cxx:38
int Int_t
unsigned int UInt_t
const UInt_t kMaxUInt
unsigned char UChar_t
const char Option_t
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char mode
R__EXTERN class THaVarList * gHaVars
Definition THaGlobals.h:11
const char * fClassName
Definition BdataLoc.h:33
const char * fDBkey
Definition BdataLoc.h:34
virtual ~BdataLoc()
Definition BdataLoc.cxx:44
TypeSet_t::iterator TypeIter_t
Definition BdataLoc.h:40
virtual const char * GetTypeKey() const =0
virtual void Print(Option_t *opt="") const
Definition BdataLoc.cxx:166
Int_t CheckConfigureParams(const TObjArray *params, Int_t start) const
Definition BdataLoc.cxx:78
static TString & GetString(const TObjArray *params, Int_t pos)
Definition BdataLoc.h:74
static TypeIter_t DoRegister(const BdataLocType &registration_info)
Definition BdataLoc.cxx:130
UInt_t data
Definition BdataLoc.h:83
virtual Int_t GetNparams() const =0
virtual Int_t Configure(const TObjArray *params, Int_t start=0)
Definition BdataLoc.cxx:101
std::set< BdataLocType > TypeSet_t
Definition BdataLoc.h:39
static TypeSet_t & fgBdataLocTypes()
Definition BdataLoc.cxx:119
UInt_t crate
Definition BdataLoc.h:82
void PrintNameType(Option_t *opt="") const
Definition BdataLoc.cxx:153
virtual Int_t DefineVariables(EMode mode=THaAnalysisObject::kDefine)
Definition BdataLoc.cxx:57
@ kIsSetup
Definition BdataLoc.h:91
virtual Int_t DefineVariables(EMode mode=THaAnalysisObject::kDefine)
Definition BdataLoc.cxx:225
void PrintMultiData(Option_t *opt="") const
Definition BdataLoc.cxx:258
virtual void Load(const THaEvData &evt)
Definition BdataLoc.cxx:247
static TypeIter_t fgThisType
Definition BdataLoc.h:151
virtual void Print(Option_t *opt="") const
Definition BdataLoc.cxx:273
std::vector< UInt_t > rdata
Definition BdataLoc.h:146
UInt_t slot
Definition BdataLoc.h:115
virtual void Load(const THaEvData &evt)
Definition BdataLoc.cxx:194
void PrintCrateLocHeader(Option_t *opt="") const
Definition BdataLoc.cxx:204
virtual void Print(Option_t *opt="") const
Definition BdataLoc.cxx:216
static TypeIter_t fgThisType
Definition BdataLoc.h:120
virtual Int_t Configure(const TObjArray *params, Int_t start=0)
Definition BdataLoc.cxx:179
UInt_t chan
Definition BdataLoc.h:115
static TypeIter_t fgThisType
Definition BdataLoc.h:198
virtual void Load(const THaEvData &evt)
Definition BdataLoc.cxx:365
UInt_t GetNumHits(UInt_t crate, UInt_t slot, UInt_t chan) const
Definition THaEvData.h:264
const UInt_t * GetRawDataBuffer() const
Definition THaEvData.h:41
UInt_t GetRocLength(UInt_t crate) const
Definition THaEvData.h:259
UInt_t GetData(UInt_t crate, UInt_t slot, UInt_t chan, UInt_t hit) const
Definition THaEvData.h:273
virtual Int_t RemoveName(const char *name)
THaVar * Define(const char *name, const char *descript, const Double_t &var, const Int_t *count=nullptr)
Definition THaVarList.h:22
virtual void SetTitle(const char *title="")
const char * GetName() const override
TString fName
virtual void SetName(const char *name)
TObject * At(Int_t idx) const override
Int_t GetLast() const override
static TClass * Class()
void SetBit(UInt_t f)
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
virtual void Error(const char *method, const char *msgfmt,...) const
virtual TClass * IsA() const
Ssiz_t Length() const
Int_t Atoi() const
const char * Data() const
TString & Append(char c, Ssiz_t rep=1)
UInt_t ntoskip
Definition BdataLoc.h:177
virtual void Print(Option_t *opt="") const
Definition BdataLoc.cxx:351
virtual Int_t Configure(const TObjArray *params, Int_t start=0)
Definition BdataLoc.cxx:283
static TypeIter_t fgThisType
Definition BdataLoc.h:180
UInt_t header
Definition BdataLoc.h:176
virtual void Load(const THaEvData &evt)
Definition BdataLoc.cxx:306
TH1 * h
start
STL namespace.
ClassImp(TPyArg)