File indexing completed on 2025-01-18 10:17:32
0001
0002
0003
0004
0005
0006 #pragma once
0007 #include <JANA/JException.h>
0008
0009 #include <typeinfo>
0010 #include <stdint.h>
0011 #include <pthread.h>
0012 #include <iostream>
0013 #include <map>
0014 #include <string>
0015 #include <sstream>
0016 #include <vector>
0017 using std::map;
0018 using std::string;
0019 using std::stringstream;
0020 using std::vector;
0021 using std::pair;
0022
0023
0024 class JCalibration{
0025 public:
0026
0027 enum containerType_t{
0028 kUnknownType,
0029 kVector,
0030 kMap,
0031 kVectorVector,
0032 kVectorMap
0033 };
0034
0035 JCalibration(string url, int run, string context="default");
0036 virtual ~JCalibration();
0037 virtual const char* className(void){return static_className();}
0038 static const char* static_className(void){return "JCalibration";}
0039
0040
0041 virtual bool GetCalib(string namepath, map<string, string> &svals, uint64_t event_number=0)=0;
0042 virtual bool GetCalib(string namepath, vector<string> &svals, uint64_t event_number=0)=0;
0043 virtual bool GetCalib(string namepath, vector< map<string, string> > &svals, uint64_t event_number=0)=0;
0044 virtual bool GetCalib(string namepath, vector< vector<string> > &svals, uint64_t event_number=0)=0;
0045 virtual bool PutCalib(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, map<string, string> &svals, string comment="");
0046 virtual bool PutCalib(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector< map<string, string> > &svals, string comment="");
0047 virtual void GetListOfNamepaths(vector<string> &namepaths)=0;
0048 virtual void GetEventBoundaries(vector<uint64_t> &event_boundaries);
0049
0050 template<class T> bool Get(string namepath, map<string,T> &vals, uint64_t event_number=0);
0051 template<class T> bool Get(string namepath, vector<T> &vals, uint64_t event_number=0);
0052 template<class T> bool Get(string namepath, vector< map<string,T> > &vals, uint64_t event_number=0);
0053 template<class T> bool Get(string namepath, vector< vector<T> > &vals, uint64_t event_number=0);
0054 template<class T> bool Get(string namepath, T &val);
0055
0056 template<class T> bool Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, map<string,T> &vals, const string &comment="");
0057 template<class T> bool Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector<T> &vals, const string &comment="");
0058 template<class T> bool Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector< map<string,T> > &vals, const string &comment="");
0059 template<class T> bool Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector< vector<T> > &vals, const string &comment="");
0060
0061 template<class T> bool Get(string namepath, const T* &vals, uint64_t event_number=0);
0062
0063 const int32_t& GetRun(void) const {return run_number;}
0064 const string& GetContext(void) const {return context;}
0065 const string& GetURL(void) const {return url;}
0066 void GetAccesses(map<string, vector<string> > &accesses){accesses = this->accesses;}
0067 string GetVariation(void);
0068
0069 containerType_t GetContainerType(string typeid_name);
0070 void DumpCalibrationsToFiles(string basedir="./");
0071 void WriteCalibFileVector(string dir, string fname, string pathname);
0072 void WriteCalibFileMap(string dir, string fname, string pathname);
0073 void WriteCalibFileVectorVector(string dir, string fname, string pathname);
0074 void WriteCalibFileVectorMap(string dir, string fname, string pathname);
0075
0076 protected:
0077 int32_t run_number;
0078
0079 pthread_mutex_t accesses_mutex;
0080 pthread_mutex_t stored_mutex;
0081 pthread_mutex_t boundaries_mutex;
0082
0083 template<typename T> containerType_t TrycontainerType(string typeid_name);
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 virtual void RetrieveEventBoundaries(void){}
0094
0095 private:
0096 JCalibration(){}
0097
0098 string context;
0099 string url;
0100
0101
0102
0103
0104 vector<uint64_t> event_boundaries;
0105 bool retrieved_event_boundaries;
0106
0107
0108
0109
0110 map<pair<string,string>, void*> stored;
0111
0112
0113
0114 template<typename T> bool TryDelete(map<pair<string,string>, void*>::iterator iter);
0115
0116
0117
0118
0119
0120
0121 map<string, vector<string> > accesses;
0122
0123
0124 void RecordRequest(string namepath, string type_name);
0125 };
0126
0127
0128
0129
0130 template<class T>
0131 bool JCalibration::Get(string namepath, map<string,T> &vals, uint64_t event_number)
0132 {
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 map<string, string> svals;
0150 bool res = GetCalib(namepath, svals, event_number);
0151 RecordRequest(namepath, typeid(map<string,T>).name());
0152
0153
0154
0155 vals.clear();
0156 map<string,string>::const_iterator iter;
0157 for(iter=svals.begin(); iter!=svals.end(); ++iter){
0158
0159 T v;
0160 stringstream ss(iter->second);
0161 ss >> v;
0162 vals[iter->first] = v;
0163 }
0164
0165 return res;
0166 }
0167
0168
0169 template<>
0170 inline bool JCalibration::Get(string namepath, map<string,string> &vals, uint64_t event_number)
0171 {
0172 bool res = GetCalib(namepath, vals, event_number);
0173 RecordRequest(namepath, typeid(map<string,string>).name());
0174 return res;
0175 }
0176
0177
0178
0179
0180 template<class T>
0181 bool JCalibration::Get(string namepath, vector<T> &vals, uint64_t event_number)
0182 {
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 vector<string> svals;
0200 bool res = GetCalib(namepath, svals, event_number);
0201 RecordRequest(namepath, typeid(vector<T>).name());
0202
0203
0204
0205 vals.clear();
0206 vector<string>::const_iterator iter;
0207 for(iter=svals.begin(); iter!=svals.end(); ++iter){
0208
0209 T v;
0210 stringstream ss(*iter);
0211 ss >> v;
0212 vals.push_back(v);
0213 }
0214
0215 return res;
0216 }
0217
0218 template<>
0219 inline bool JCalibration::Get(string namepath, vector<string> &vals, uint64_t event_number) {
0220 bool res = GetCalib(namepath, vals, event_number);
0221 RecordRequest(namepath, typeid(vector<string>).name());
0222 return res;
0223 }
0224
0225
0226
0227
0228
0229 template<class T>
0230 bool JCalibration::Get(string namepath, vector< map<string,T> > &vals, uint64_t event_number)
0231 {
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265 vector< map<string, string> >svals;
0266 bool res = GetCalib(namepath, svals, event_number);
0267 RecordRequest(namepath, typeid(vector< map<string,T> >).name());
0268
0269
0270
0271 vals.clear();
0272 for(unsigned int i=0; i<svals.size(); i++){
0273 map<string,string>::const_iterator iter;
0274 map<string,T> mvals;
0275 for(iter=svals[i].begin(); iter!=svals[i].end(); ++iter){
0276
0277 T v;
0278 stringstream ss(iter->second);
0279 ss >> v;
0280 mvals[iter->first] = v;
0281 }
0282 vals.push_back(mvals);
0283 }
0284
0285 return res;
0286 }
0287 template<>
0288 inline bool JCalibration::Get(string namepath, vector< map<string,string> > &vals, uint64_t event_number) {
0289 bool res = GetCalib(namepath, vals, event_number);
0290 RecordRequest(namepath, typeid(vector< map<string,string> >).name());
0291 return res;
0292 }
0293
0294
0295
0296
0297 template<class T>
0298 bool JCalibration::Get(string namepath, vector< vector<T> > &vals, uint64_t event_number)
0299 {
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 vector< vector<string> >svals;
0331 bool res = GetCalib(namepath, svals, event_number);
0332 RecordRequest(namepath, typeid(vector< vector<T> >).name());
0333
0334
0335
0336 vals.clear();
0337 for(unsigned int i=0; i<svals.size(); i++){
0338 vector<string>::const_iterator iter;
0339 vector<T> vvals;
0340 for(iter=svals[i].begin(); iter!=svals[i].end(); ++iter){
0341
0342 T v;
0343 stringstream ss(*iter);
0344 ss >> v;
0345 vvals.push_back(v);
0346 }
0347 vals.push_back(vvals);
0348 }
0349
0350 return res;
0351 }
0352
0353 template<>
0354 inline bool JCalibration::Get(string namepath, vector< vector<string> > &vals, uint64_t event_number) {
0355 bool res = GetCalib(namepath, vals, event_number);
0356 RecordRequest(namepath, typeid(vector< vector<string> >).name());
0357 return res;
0358 }
0359
0360
0361
0362
0363 template<class T>
0364 bool JCalibration::Get(string namepath, const T* &vals, uint64_t event_number)
0365 {
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376 vals = NULL;
0377
0378
0379 pair<string, string> key;
0380 key.first = namepath;
0381 key.second = typeid(T).name();
0382
0383
0384 pthread_mutex_lock(&stored_mutex);
0385
0386
0387 map<pair<string,string>, void*>::iterator iter = stored.find(key);
0388 if(iter!=stored.end()){
0389 vals = (const T*)iter->second;
0390 pthread_mutex_unlock(&stored_mutex);
0391 RecordRequest(namepath, typeid(T).name());
0392 return false;
0393 }
0394
0395
0396
0397
0398 T* t = new T;
0399 bool res = Get(namepath, *t, event_number);
0400
0401
0402 if(!res){
0403 stored[key] = t;
0404 vals = t;
0405 }
0406
0407
0408 pthread_mutex_unlock(&stored_mutex);
0409
0410 return res;
0411 }
0412
0413
0414
0415
0416 template<class T> bool JCalibration::Get(string namepath, T &val)
0417 {
0418
0419
0420
0421
0422
0423
0424 vector<T> vals;
0425 bool ret = Get(namepath, vals);
0426 if(vals.empty()) return true;
0427 val = vals[0];
0428
0429 return ret;
0430 }
0431
0432
0433
0434
0435
0436 template<class T>
0437 bool JCalibration::Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, map<string,T> &vals, const string &comment)
0438 {
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449 map<string,string> svals;
0450
0451 __typeof__(vals.begin()) iter;
0452 for(iter=vals.begin(); iter!=vals.end(); ++iter){
0453
0454 stringstream ss;
0455 ss<<iter->second;
0456 svals[iter->first] = ss.str();
0457 }
0458
0459
0460 bool res = PutCalib(namepath, run_min, run_max, event_min, event_max, author, svals, comment);
0461
0462 return res;
0463 }
0464
0465
0466
0467
0468 template<class T>
0469 bool JCalibration::Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector<T> &vals, const string &comment)
0470 {
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481 map<string,string> svals;
0482 __typeof__(vals.begin()) iter;
0483 int i=0;
0484 for(iter=vals.begin(); iter!=vals.end(); ++iter, ++i){
0485
0486 stringstream ss;
0487 ss<<iter->second;
0488 stringstream iss;
0489 iss<<i;
0490 svals[iss.str()] = ss.str();
0491 }
0492
0493
0494 bool res = PutCalib(namepath, run_min, run_max, event_min, event_max, author, svals, comment);
0495
0496 return res;
0497 }
0498
0499
0500
0501
0502 template<class T>
0503 bool JCalibration::Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector< map<string,T> > &vals, const string &comment)
0504 {
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 vector<map<string,string> > vsvals;
0516 for(unsigned int i=0; i<vals.size(); i++){
0517
0518 map<string,string> svals;
0519 map<string, T> &mvals = vals[i];
0520 __typeof__(mvals.begin()) iter;
0521 for(iter=mvals.begin(); iter!=mvals.end(); ++iter){
0522
0523 stringstream ss;
0524 ss<<iter->second;
0525 svals[iter->first] = ss.str();
0526 }
0527 vsvals.push_back(svals);
0528 }
0529
0530
0531 bool res = PutCalib(namepath, run_min, run_max, event_min, event_max, author, vsvals, comment);
0532
0533 return res;
0534 }
0535
0536
0537
0538
0539 template<class T>
0540 bool JCalibration::Put(string namepath, int32_t run_min, int32_t run_max, uint64_t event_min, uint64_t event_max, string &author, vector< vector<T> > &vals, const string &comment)
0541 {
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552 vector<map<string,string> > vsvals;
0553 for(unsigned int i=0; i<vals.size(); i++){
0554
0555 map<string,string> svals;
0556 vector<T> &mvals = vals[i];
0557 for(unsigned int j=0; j<mvals.size(); j++){
0558
0559 stringstream ss;
0560 ss<<mvals[j];
0561 stringstream iss;
0562 iss<<i;
0563 svals[iss.str()] = ss.str();
0564 }
0565 vsvals.push_back(svals);
0566 }
0567
0568
0569 bool res = PutCalib(namepath, run_min, run_max, event_min, event_max, author, vsvals, comment);
0570
0571 return res;
0572 }
0573
0574
0575
0576
0577 template<typename T>
0578 bool JCalibration::TryDelete(map<pair<string,string>, void*>::iterator iter)
0579 {
0580
0581
0582
0583
0584 const string &type_name = iter->first.second;
0585 void *ptr = iter->second;
0586
0587 switch(TrycontainerType<T>(type_name)){
0588 case kVector: delete (vector<T>*)ptr; break;
0589 case kMap: delete (map<string,T>*)ptr; break;
0590 case kVectorVector: delete (vector<vector<T> >*)ptr; break;
0591 case kVectorMap: delete (vector<map<string,T> >*)ptr; break;
0592 default:
0593
0594 return false;
0595 }
0596
0597
0598 return true;
0599 }
0600
0601
0602
0603
0604 template<typename T>
0605 JCalibration::containerType_t JCalibration::TrycontainerType(string typeid_name)
0606 {
0607 if(typeid_name==typeid(vector<T>).name())return kVector;
0608 if(typeid_name==typeid(map<string,T>).name())return kMap;
0609 if(typeid_name==typeid(vector<vector<T> >).name())return kVectorVector;
0610 if(typeid_name==typeid(vector<map<string,T> >).name())return kVectorMap;
0611
0612 return kUnknownType;
0613 }
0614