39#if defined(__linux__) && (defined(_GNU_SOURCE) || !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE > 600))
40# define GNU_STRERROR_R
49 const size_t BUFLEN = 128;
53 const char* s = strerror_r(errno, buf, BUFLEN);
54 string ret = s ? string(s) : string(
"unknown error ") + to_string(errno);
56 strerror_r(errno, buf, BUFLEN);
79 if( !db_filename || !*db_filename ) {
80 ::Warning(
"THaCrateMap",
"Undefined database file name, "
81 "using default \"db_cratemap.dat\"" );
82 db_filename =
"cratemap";
90 for(
UInt_t crate = 0; crate <
crdat.size(); crate++ ) {
91 const auto& cr =
crdat[crate];
92 if( !cr.crate_used )
continue;
94 UInt_t headtry = word & 0xfff00000;
95 UInt_t zero = word & 0x0000ff00;
97 (headtry == cr.sltdat[1].header) )
108 if( crate >=
crdat.size() )
110 crdat[crate] = empty_crateinfo;
118 assert(crate <
crdat.size());
122 if( stype ==
"fastbus" )
124 else if( stype ==
"vme" )
126 else if( stype ==
"scaler" )
128 else if( stype ==
"camac" )
145 auto& slt =
crdat[crate].sltdat[slot];
146 if( slt.model != 0 && mod == 0 ) {
161 assert( crate <
crdat.size() && slot <
crdat[crate].sltdat.size() );
163 static const array<ModelPar_t, 18> modelpar = {{
184 find_if(ALL(modelpar), [
model](
const ModelPar_t& modelParam ) {
185 return model == modelParam.model;
187 if( item != modelpar.end() ) {
188 crdat[crate].sltdat[slot].nchan = item->nchan;
189 crdat[crate].sltdat[slot].ndata = item->ndata;
198 assert( crate <
crdat.size() && slot <
crdat[crate].sltdat.size() );
199 auto& cr =
crdat[crate];
200 cr.crate_used =
true;
201 cr.sltdat[slot].used =
true;
204 if( std::find(ALL(cr.used_slots), slot) == cr.used_slots.end() ) {
205 cr.used_slots.push_back(slot);
215 crdat[crate].crate_used =
false;
221 assert( crate <
crdat.size() && slot <
crdat[crate].sltdat.size() );
222 auto& cr =
crdat[crate];
223 cr.sltdat[slot].used =
false;
224 auto it = std::find(ALL(cr.used_slots), slot);
225 if( it != cr.used_slots.end() ) {
226 cr.used_slots.erase(it);
228 if( cr.used_slots.empty() )
240 if( fseek(fi, 0, SEEK_END) == 0 ) {
241 long size = ftell(fi);
244 unique_ptr<char[]> fbuf{
new char[
size]};
245 size_t nread = fread(fbuf.get(),
sizeof(
char),
size, fi);
246 if( nread ==
static_cast<size_t>(
size) ) {
248 text.assign(fbuf.get(), fbuf.get()+nread);
263 const char*
const here =
"THaCrateMap::init(file)";
266 ::Error(
here,
"Error opening crate map database file %s: %s",
273 ::Error(
here,
"Error reading crate map database file %s: %s",
290 const char*
const here =
"THaCrateMap::init(tloc)";
301 const auto& cr =
crdat[roc];
302 if( !cr.crate_used || cr.used_slots.empty() )
continue;
303 os <<
"==== Crate " << roc <<
" type " << cr.crate_type_name;
304 if( !cr.scalerloc.empty() ) os <<
" \"" << cr.scalerloc <<
"\"";
306 os <<
"#slot\tmodel\tclear\t header\t mask \tnchan\tndata\n";
307 for(
UInt_t slot = 0; slot < cr.sltdat.size(); slot++ ) {
308 const auto& slt = cr.sltdat[slot];
309 if( !slt.used )
continue;
310 os <<
" " << slot <<
"\t" << slt.model <<
"\t" << slt.clear;
311 ios::fmtflags oldf = os.setf(ios::right, ios::adjustfield);
312 os <<
"\t0x" << hex << setfill(
'0') << setw(8) << slt.header
313 <<
"\t0x" << hex << setfill(
'0') << setw(8) << slt.headmask
314 << dec << setfill(
' ') << setw(0)
328 const char*
const here =
"THaCrateMap::loadConfig";
329 auto pos1 =
line.find(
"cfg:");
330 auto pos2 =
line.find(
"dbfile:");
331 bool have_cfg = (pos1 != string::npos);
332 bool have_dbf = (pos2 != string::npos);
333 if( have_cfg or have_dbf ) {
334 if( have_cfg and have_dbf ) {
335 ::Error(
here,
"Cannot specify both database file and options, "
336 "line = \n%s",
line.c_str());
340 cfgstr =
line.substr(pos1 + 4);
343 string fname =
line.substr(pos2 + 7);
348 ::Error(
here,
"Error opening decoder module database file "
349 "\"db_%s.dat\": %s\n line = %s",
354 ::Error(
here,
"Error reading decoder module database file "
355 "\"db_%s.dat\": %s", fname.c_str(),
StrError().c_str());
370 static const char*
const here =
"THaCrateMap::ParseCrateInfo";
373 if( sscanf(
line.c_str(),
"Crate %u type %20s", &crate, ctype) == 2 ) {
375 cerr <<
"THaCrateMap:: fatal ERROR 2 setCrateType " << endl;
379 auto& cr =
crdat[crate];
380 if( cr.crate_code ==
kScaler ) {
381 if( sscanf(
line.c_str(),
"Crate %*u type %*s %20s", ctype) != 1 ) {
382 cerr <<
"THaCrateMap:: fatal ERROR 3 " << endl;
387 cr.scalerloc = scaler_name;
393 Error(
here,
"Invalid/incomplete Crate definition: \"%s\"",
424 Int_t cword = 1, imodel = 0;
428 Int_t nread = sscanf(
line.c_str(),
"%u %d %d %x %x %u %u",
429 &slot, &imodel, &cword, &iheader, &
mask, &ichan, &idata);
432 cerr <<
"THaCrateMap:: fatal ERROR 4 " << endl <<
"Bad line " << endl
434 <<
" Warning: a bad line could cause wrong decoding !" << endl;
439 crmap->
setModel(crate,slot,imodel,ichan,idata);
446 else if( nread > 3 ) {
452 slt.
cfgstr = std::move(cfgstr);
461 auto& cr =
crdat[iused];
462 assert(!cr.used_slots.empty());
463 sort(ALL(cr.used_slots));
464 auto slot_is_bank = [&](
UInt_t idx ) {
return cr.sltdat[idx].bank >= 0; };
465 cr.bank_structure = any_of(ALL(cr.used_slots), slot_is_bank);
466 cr.all_banks = all_of(ALL(cr.used_slots), slot_is_bank);
477 const char*
const here =
"THaCrateMap::init(string)";
479 if ( the_map.empty() ) {
481 ::Warning(
here,
"Empty crate map definition. Decoder will not be usable. "
488 crdat.resize(ncrates);
494 istringstream s(the_map);
495 Int_t found_tscrate=0;
496 TDatime keydate(950101, 0), prevdate(950101, 0);
497 bool do_ignore =
false, in_crate =
false;
500 while( getline(s,
line) ) {
503 auto pos =
line.find_first_of(
"!#");
504 if( pos != string::npos )
507 if(
line.find_first_not_of(
" \t") == string::npos )
516 if( Podd::IsDBtimestamp(
line, keydate) ) {
517 do_ignore = (keydate >
fInitTime || keydate < prevdate);
520 }
else if( do_ignore )
525 if( !in_crate &&
line.find(
"TSROC") != string::npos ) {
533 pos =
line.find(
"Crate");
534 if( pos != string::npos ) {
542 in_crate =
crdat[crate].crate_used;
545 if( crate ==
kMaxUInt || !in_crate ) {
546 Error(
here,
"db_%s.dat:%d: Expected Crate definition.\n"
547 "For example: \"==== Crate 5 type vme\". "
548 "Found instead: \"%s\"",
553 if( (ret =
crdat[crate].ParseSlotInfo(
this, crate,
line)) !=
CM_OK )
559 if ( !found_tscrate ) {
560 cout <<
"THaCrateMap::WARNING: Did not find TSROC. Using default " <<
fTSROC << endl;
569 crate_code(
kUnknown), crate_type_name(
"unknown"),
570 crate_used(false), bank_structure(false), all_banks(false)
size_t size(const MatrixT &matrix)
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
Option_t Option_t TPoint TPoint const char text
static const char *const here
std::string crate_type_name
std::array< SlotInfo_t, MAXSLOT > sltdat
std::vector< UInt_t > used_slots
Int_t ParseSlotInfo(THaCrateMap *crmap, UInt_t crate, std::string &line)
void setUsed(UInt_t crate, UInt_t slot)
static const UInt_t MAXDATA
static const UInt_t MAXCHAN
void print(std::ostream &os=std::cout) const
Int_t loadConfig(std::string &line, std::string &cfgstr)
std::vector< UInt_t > used_crates
UInt_t getScalerCrate(UInt_t word) const
Int_t ParseCrateInfo(const std::string &line, UInt_t &crate)
Int_t resetCrate(UInt_t crate)
Int_t setModel(UInt_t crate, UInt_t slot, Int_t mod, UInt_t nchan=MAXCHAN, UInt_t ndata=MAXDATA)
int init(const std::string &the_map)
static const Int_t CM_ERR
static Int_t readFile(FILE *fi, std::string &text)
static const UInt_t DEFAULT_TSROC
THaCrateMap(const char *db="cratemap")
Int_t SetModelSize(UInt_t crate, UInt_t slot, UInt_t model)
void setUnused(UInt_t crate, UInt_t slot)
std::vector< CrateInfo_t > crdat
bool isScalerCrate(UInt_t crate) const
Int_t setCrateType(UInt_t crate, const char *stype)
TString & ReplaceAll(const char *s1, const char *s2)
unsigned long long ULong64_t
static const UInt_t MAXROC