Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
Module.cxx
Go to the documentation of this file.
1
2//
3// Module
4//
5// Abstract class to help decode the module.
6// No module data is stored here; instead it is in THaSlotData
7// arrays, as it was traditionally.
8//
10
11#include "Module.h"
12#include "Textvars.h" // for Podd::Tokenize
13#include "Helper.h"
14#include "TError.h"
15#include <algorithm>
16#include <stdexcept>
17#include <sstream>
18#include <cassert>
19
20using namespace std;
21
22namespace Decoder {
23
24//_____________________________________________________________________________
26 : fCrate{crate}
27 , fSlot{slot}
28 , fHeader{0}
29 , fHeaderMask{0xffffffff}
30 , fBank{-1}
31 , fWordsExpect{0}
32 , fWordsSeen{0}
33 , fWdcntMask{0}
34 , fWdcntShift{0}
35 , fModelNum{-1}
36 , fNumChan{0}
37 , fMode{0}
38 , block_size{1}
39 , IsInit{false}
40 , fMultiBlockMode{false}
41 , fBlockIsDone{false}
42 , fFirmwareVers{0}
43 , fDebug{0}
44 , fDebugFile{nullptr}
45 , fExtra{nullptr}
46{
47 // Warning: see comments at Init()
48}
49
50//_____________________________________________________________________________
52{
53// Suggestion: call this Init() before calling the inheriting class's Init.
54// Otherwise some variables may be undefined. The "factory" method
55// using TClass::New does NOT call the c'tor of this base class !!
56// Make sure all your variables are defined.
57 fHeader=0;
58 fHeaderMask=0xffffffff;
59 fBank=-1;
60 fWordsSeen = 0;
61 fWordsExpect = 0;
62 fWdcntMask=0;
64 fDebug = 0;
65 fDebugFile=nullptr;
66 fModelNum = -1;
67 fName = "";
68 fNumChan = 0;
70
71//_____________________________________________________________________________
73{
74 // Clear event-by-event data.
75 // This is called for each event block read from file, but not for each
76 // individual event in a multi-event block.
77
78 fWordsSeen = 0;
79 block_size = 1;
80 fMultiBlockMode = false;
81 fBlockIsDone = false;
82}
83
84//_____________________________________________________________________________
85static void StoreValue( const string& item, UInt_t& data )
86{
87 // Convert 'item' to unsigned int and store the result in 'data'
88 size_t len = 0;
89 unsigned long val = stoul(item, &len, 0);
90 if( val > kMaxUInt ) {
91 ostringstream ostr;
92 ostr << "Value " << val << " out of range. Must be <= " << kMaxUInt << ". "
93 << "Fix the cratemap.";
94 throw std::out_of_range(ostr.str());
95 }
96 data = val;
97}
98
99//_____________________________________________________________________________
100void Module::ParseConfigStr( const char* configstr,
101 const vector<ConfigStrReq>& req )
102{
103 // Helper function for parsing a module configuration string
104
105 if( !configstr || !*configstr || req.empty() )
106 // Nothing to do
107 return;
108
109 size_t idx = 0;
110 vector<string> items;
111 // Use commas and/or whitespace to separate fields in the configuration string
112 Podd::Tokenize(configstr, ", \t\n\v\f\r", items);
113 for( auto item : items ) {
114 // Too many parameters?
115 if( idx >= req.size() ) {
116 ostringstream ostr;
117 ostr << "Too many configuration parameters in string \"" << configstr
118 << "\". Maximum number is " << req.size() << ". Fix the cratemap.";
119 throw invalid_argument(ostr.str());
120 }
121 // Do we have a named parameter?
122 string name;
123 auto pos = item.find('=');
124 if( pos != string::npos ) {
125 if( pos > 0 )
126 name = item.substr(0,pos);
127 item.erase(0,pos+1);
128 }
129 if( name.empty() ) {
130 // Unnamed: use item position to assign to corresponding destination
131 StoreValue( item, req[idx].data );
132 } else {
133 // Named: find and assign to corresponding destination
134 auto it = find_if(ALL(req), [&name]( const ConfigStrReq& r ) { return name == r.name; });
135 if( it != req.end() ) {
136 StoreValue( item, it->data );
137 } else {
138 // Unknown parameter name in configstr. Probably some confusion.
139 // Make the user think about it.
140 ostringstream ostr;
141 ostr << "Unsupported parameter name \"" << name << "\". "
142 << "Fix the cratemap.";
143 throw invalid_argument(ostr.str());
144 }
145 }
146 ++idx;
147 }
148}
149
150//_____________________________________________________________________________
151void Module::Init( const char* /*configstr*/ )
152{
153 // Initialize using optional configuration string defined in the crate map.
154 // This is the method actually called from THaSlotData::loadModule. By
155 // default, it simply calls the standard Init() method, which is kept around
156 // for backward compatibility. Derived classes wanting to use configuration
157 // strings to set extra parameters must override this method.
158
159 Init();
160}
161
162
163//_____________________________________________________________________________
165// Simplest version of IsSlot relies on a unique header.
166 if ((rdata & fHeaderMask)==fHeader) {
168 return true;
169 }
170 return false;
171}
172
173//_____________________________________________________________________________
174void Module::DoPrint() const {
175 if (fDebugFile) {
176 *fDebugFile << "Module name = "<<fName<<endl;
177 *fDebugFile << "Module:: Crate "<<fCrate<<" slot "<<fSlot<<endl;
178 *fDebugFile << "Module:: fWdcntMask "<<hex<<fWdcntMask<<dec<<endl;
179 *fDebugFile << "Module:: fHeader "<<hex<<fHeader<<endl;
180 *fDebugFile << "Module:: fHeaderMask "<<hex<<fHeaderMask<<dec<<endl;
181 *fDebugFile << "Module:: num chan "<<fNumChan<<endl;
182 }
183}
184
185
186//_____________________________________________________________________________
188{
189 // Local storage for all defined Module types. Initialize here on first use
190 // (cf. http://www.parashift.com/c++-faq/static-init-order-on-first-use-members.html)
191
192 static auto *fgModuleTypes = new TypeSet_t;
193
194 return *fgModuleTypes;
195}
196
197//_____________________________________________________________________________
199{
200 // Add given info in fgModuleTypes
201
202 if( !info.fClassName ||!*info.fClassName ) {
203 ::Error( "Module::DoRegister", "Attempt to register empty class name. "
204 "Coding error. Call expert." );
205 return fgModuleTypes().end();
206 }
207
208 pair< TypeIter_t, bool > ins = fgModuleTypes().insert(info);
209
210 if( !ins.second ) {
211 ::Error( "Module::DoRegister", "Attempt to register duplicate decoder module "
212 "class \"%s\". Coding error. Call expert.", info.fClassName );
213 return fgModuleTypes().end();
214 }
215 // NB: std::set guarantees that iterators remain valid on further insertions,
216 // so this return value will remain good, unlike, e.g., std::vector iterators.
217 return ins.first;
218}
219
220//_____________________________________________________________________________
221UInt_t Module::LoadSlot( THaSlotData *sldat, const UInt_t* evbuffer,
222 UInt_t pos, UInt_t len)
223{
224 // Load slot from [pos, pos+len). pos+len-1 is the last word of data
225
226 // Basic example of how this could be done
227 // if (fDebugFile) {
228 // *fDebugFile << "Module:: Loadslot "<<endl;
229 // *fDebugFile << "pos"<<dec<<pos<<" len "<<len<<endl;
230 // }
231 // fWordsSeen=0;
232 // while ( fWordsSeen<len ) {
233 // if (fDebugFile) *fDebugFile <<endl;
234 // for (size_t ichan = 0, nchan = GetNumChan(); ichan < nchan; ichan++) {
235 // Int_t mdata,rdata;
236 // rdata = evbuffer[pos+fWordsSeen];
237 // mdata = rdata;
238 // sldat->loadData(ichan, mdata, rdata);
239 // if (ichan < fData.size()) fData[ichan]=rdata;
240 // fWordsSeen++;
241 // }
242 // }
243 // return fWordsSeen;
244
245 // By default, this is just a wrapper for the 3-parameter version of LoadSlot,
246 // which every module is required to implement
247
248 assert(len > 0);
249 if( len == 0 ) return 0;
250 return LoadSlot(sldat, evbuffer+pos, evbuffer+pos+len-1);
251}
252
253} // namespace Decoder
254
unsigned int UInt_t
ROOT::R::TRInterface & r
bool Bool_t
const UInt_t kMaxUInt
const char Option_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
char name[80]
const char * fClassName
Definition Module.h:35
UInt_t fCrate
Definition Module.h:142
UInt_t fNumChan
Definition Module.h:148
static TypeIter_t DoRegister(const ModuleType &registration_info)
Definition Module.cxx:198
virtual void Clear(Option_t *="")
Definition Module.cxx:72
virtual void Init()
Definition Module.cxx:51
Bool_t fMultiBlockMode
Definition Module.h:152
std::set< ModuleType > TypeSet_t
Definition Module.h:39
std::ofstream * fDebugFile
Definition Module.h:156
UInt_t fWdcntMask
Definition Module.h:146
virtual UInt_t LoadSlot(THaSlotData *sldat, const UInt_t *evbuffer, const UInt_t *pstop)=0
Int_t fModelNum
Definition Module.h:147
UInt_t fWdcntShift
Definition Module.h:146
UInt_t fWordsExpect
Definition Module.h:145
UInt_t block_size
Definition Module.h:150
Bool_t fBlockIsDone
Definition Module.h:152
static TypeSet_t & fgModuleTypes()
Definition Module.cxx:187
virtual Bool_t IsSlot(UInt_t rdata)
Definition Module.cxx:164
static void ParseConfigStr(const char *configstr, const std::vector< ConfigStrReq > &req)
Definition Module.cxx:100
virtual void DoPrint() const
Definition Module.cxx:174
UInt_t fHeader
Definition Module.h:143
UInt_t fSlot
Definition Module.h:142
UInt_t fHeaderMask
Definition Module.h:143
UInt_t fWordsSeen
Definition Module.h:145
TypeSet_t::iterator TypeIter_t
Definition Module.h:40
TString fName
virtual void Error(const char *method, const char *msgfmt,...) const
static void StoreValue(const string &item, UInt_t &data)
Definition Module.cxx:85
STL namespace.
ClassImp(TPyArg)