File indexing completed on 2025-08-05 08:16:04
0001 #include "CDBTTree.h"
0002
0003 #include <phool/phool.h>
0004
0005 #include <TBranch.h> // for TBranch
0006 #include <TCollection.h> // for TIter
0007 #include <TDirectory.h> // for TDirectoryAtomicAdapter, TDirectory, gDirec...
0008 #include <TFile.h>
0009 #include <TLeaf.h> // for TLeaf
0010 #include <TObjArray.h> // for TObjArray
0011 #include <TROOT.h>
0012 #include <TSystem.h>
0013 #include <TTree.h>
0014
0015 #include <climits>
0016 #include <cmath> // for NAN, isfinite
0017 #include <cstdint> // for uint64_t
0018 #include <iostream>
0019 #include <limits> // for numeric_limits, numeric_limits<>::max_digits10
0020 #include <set> // for set
0021 #include <utility> // for pair, make_pair
0022
0023 CDBTTree::CDBTTree(const std::string &fname)
0024 : m_Filename(fname)
0025 {
0026 }
0027
0028 CDBTTree::~CDBTTree()
0029 {
0030 m_FloatEntryMap.clear();
0031 m_SingleFloatEntryMap.clear();
0032 }
0033
0034 void CDBTTree::SetFloatValue(int channel, const std::string &name, float value)
0035 {
0036 if (name == "ID")
0037 {
0038 std::cout << "Sorry ID is reserved as fieldname, pick anything else" << std::endl;
0039 gSystem->Exit(1);
0040 }
0041 std::string fieldname = "F" + name;
0042 if (m_Locked[MultipleEntries])
0043 {
0044 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0045 std::cout << "That does not work, restructure your code" << std::endl;
0046 gSystem->Exit(1);
0047 }
0048 m_FloatEntryMap[channel].insert(std::make_pair(fieldname, value));
0049 }
0050
0051 void CDBTTree::SetDoubleValue(int channel, const std::string &name, double value)
0052 {
0053 if (name == "ID")
0054 {
0055 std::cout << "Sorry ID is reserved as fieldname, pick anything else" << std::endl;
0056 gSystem->Exit(1);
0057 }
0058 std::string fieldname = "D" + name;
0059 if (m_Locked[MultipleEntries])
0060 {
0061 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0062 std::cout << "That does not work, restructure your code" << std::endl;
0063 gSystem->Exit(1);
0064 }
0065 m_DoubleEntryMap[channel].insert(std::make_pair(fieldname, value));
0066 }
0067
0068 void CDBTTree::SetIntValue(int channel, const std::string &name, int value)
0069 {
0070 if (name == "ID")
0071 {
0072 std::cout << "Sorry ID is reserved as fieldname, pick anything else" << std::endl;
0073 gSystem->Exit(1);
0074 }
0075 std::string fieldname = "I" + name;
0076 if (m_Locked[MultipleEntries])
0077 {
0078 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0079 std::cout << "That does not work, restructure your code" << std::endl;
0080 gSystem->Exit(1);
0081 }
0082 m_IntEntryMap[channel].insert(std::make_pair(fieldname, value));
0083 }
0084
0085 void CDBTTree::SetUInt64Value(int channel, const std::string &name, uint64_t value)
0086 {
0087 if (name == "ID")
0088 {
0089 std::cout << "Sorry ID is reserved as fieldname, pick anything else" << std::endl;
0090 gSystem->Exit(1);
0091 }
0092 std::string fieldname = "g" + name;
0093 if (m_Locked[MultipleEntries])
0094 {
0095 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0096 std::cout << "That does not work, restructure your code" << std::endl;
0097 gSystem->Exit(1);
0098 }
0099 m_UInt64EntryMap[channel].insert(std::make_pair(fieldname, value));
0100 }
0101
0102 void CDBTTree::Commit()
0103 {
0104 m_Locked[MultipleEntries] = true;
0105 }
0106
0107 void CDBTTree::WriteMultipleCDBTTree()
0108 {
0109 m_TTree[MultipleEntries] = new TTree(m_TTreeName[MultipleEntries].c_str(), m_TTreeName[MultipleEntries].c_str());
0110 std::set<int> id_set;
0111 std::map<std::string, float> floatmap;
0112 std::map<std::string, double> doublemap;
0113 std::map<std::string, int> intmap;
0114 std::map<std::string, uint64_t> uint64map;
0115 intmap.insert(std::make_pair("IID", std::numeric_limits<int>::min()));
0116 for (auto &f_entry : m_FloatEntryMap)
0117 {
0118 id_set.insert(f_entry.first);
0119 for (auto &f_val : f_entry.second)
0120 {
0121 floatmap.insert(std::make_pair(f_val.first, std::numeric_limits<float>::quiet_NaN()));
0122 }
0123 }
0124 for (auto &f_val : floatmap)
0125 {
0126 std::string fielddescriptor = f_val.first + "/F";
0127 m_TTree[MultipleEntries]->Branch(f_val.first.c_str(), &f_val.second, fielddescriptor.c_str());
0128 }
0129
0130 for (auto &f_entry : m_DoubleEntryMap)
0131 {
0132 id_set.insert(f_entry.first);
0133 for (auto &f_val : f_entry.second)
0134 {
0135 doublemap.insert(std::make_pair(f_val.first, std::numeric_limits<double>::quiet_NaN()));
0136 }
0137 }
0138 for (auto &f_val : doublemap)
0139 {
0140 std::string fielddescriptor = f_val.first + "/D";
0141 m_TTree[MultipleEntries]->Branch(f_val.first.c_str(), &f_val.second, fielddescriptor.c_str());
0142 }
0143
0144 for (auto &i_entry : m_IntEntryMap)
0145 {
0146 id_set.insert(i_entry.first);
0147 for (auto &i_val : i_entry.second)
0148 {
0149 intmap.insert(std::make_pair(i_val.first, std::numeric_limits<int>::min()));
0150 }
0151 }
0152 for (auto &i_val : intmap)
0153 {
0154 std::string fielddescriptor = i_val.first + "/I";
0155 m_TTree[MultipleEntries]->Branch(i_val.first.c_str(), &i_val.second, fielddescriptor.c_str());
0156 }
0157
0158 for (auto &i_entry : m_UInt64EntryMap)
0159 {
0160 id_set.insert(i_entry.first);
0161 for (auto &i_val : i_entry.second)
0162 {
0163 uint64map.insert(std::make_pair(i_val.first, std::numeric_limits<uint64_t>::max()));
0164 }
0165 }
0166 for (auto &i_val : uint64map)
0167 {
0168 std::string fielddescriptor = i_val.first + "/g";
0169 m_TTree[MultipleEntries]->Branch(i_val.first.c_str(), &i_val.second, fielddescriptor.c_str());
0170 }
0171
0172 for (auto ids : id_set)
0173 {
0174 intmap["IID"] = ids;
0175 auto fmapiter = m_FloatEntryMap.find(ids);
0176 if (fmapiter != m_FloatEntryMap.end())
0177 {
0178 for (auto &f_val : fmapiter->second)
0179 {
0180 floatmap[f_val.first] = f_val.second;
0181 }
0182 }
0183 auto dmapiter = m_DoubleEntryMap.find(ids);
0184 if (dmapiter != m_DoubleEntryMap.end())
0185 {
0186 for (auto &d_val : dmapiter->second)
0187 {
0188 doublemap[d_val.first] = d_val.second;
0189 }
0190 }
0191 auto imapiter = m_IntEntryMap.find(ids);
0192 if (imapiter != m_IntEntryMap.end())
0193 {
0194 for (auto &i_val : imapiter->second)
0195 {
0196 intmap[i_val.first] = i_val.second;
0197 }
0198 }
0199 auto uint64mapiter = m_UInt64EntryMap.find(ids);
0200 if (uint64mapiter != m_UInt64EntryMap.end())
0201 {
0202 for (auto &uint64_val : uint64mapiter->second)
0203 {
0204 uint64map[uint64_val.first] = uint64_val.second;
0205 }
0206 }
0207 m_TTree[MultipleEntries]->Fill();
0208 for (auto &f_val : floatmap)
0209 {
0210 f_val.second = std::numeric_limits<float>::quiet_NaN();
0211 }
0212 for (auto &f_val : doublemap)
0213 {
0214 f_val.second = std::numeric_limits<double>::quiet_NaN();
0215 }
0216 for (auto &i_val : intmap)
0217 {
0218 i_val.second = std::numeric_limits<int>::min();
0219 }
0220 for (auto &i_val : uint64map)
0221 {
0222 i_val.second = std::numeric_limits<uint64_t>::max();
0223 }
0224 }
0225 return;
0226 }
0227
0228 void CDBTTree::SetSingleFloatValue(const std::string &name, float value)
0229 {
0230 std::string fieldname = "F" + name;
0231 if (m_SingleFloatEntryMap.find(fieldname) == m_SingleFloatEntryMap.end())
0232 {
0233 if (m_Locked[SingleEntries])
0234 {
0235 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0236 std::cout << "That does not work, restructure your code" << std::endl;
0237 gSystem->Exit(1);
0238 }
0239 m_SingleFloatEntryMap.insert(std::make_pair(fieldname, value));
0240 return;
0241 }
0242 m_SingleFloatEntryMap[fieldname] = value;
0243 }
0244
0245 void CDBTTree::SetSingleDoubleValue(const std::string &name, double value)
0246 {
0247 std::string fieldname = "D" + name;
0248 if (m_SingleDoubleEntryMap.find(fieldname) == m_SingleDoubleEntryMap.end())
0249 {
0250 if (m_Locked[SingleEntries])
0251 {
0252 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0253 std::cout << "That does not work, restructure your code" << std::endl;
0254 gSystem->Exit(1);
0255 }
0256 m_SingleDoubleEntryMap.insert(std::make_pair(fieldname, value));
0257 return;
0258 }
0259 m_SingleDoubleEntryMap[fieldname] = value;
0260 }
0261
0262 void CDBTTree::SetSingleIntValue(const std::string &name, int value)
0263 {
0264 std::string fieldname = "I" + name;
0265 if (m_SingleIntEntryMap.find(fieldname) == m_SingleIntEntryMap.end())
0266 {
0267 if (m_Locked[SingleEntries])
0268 {
0269 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0270 std::cout << "That does not work, restructure your code" << std::endl;
0271 gSystem->Exit(1);
0272 }
0273 m_SingleIntEntryMap.insert(std::make_pair(fieldname, value));
0274 return;
0275 }
0276 m_SingleIntEntryMap[fieldname] = value;
0277 }
0278
0279 void CDBTTree::SetSingleUInt64Value(const std::string &name, uint64_t value)
0280 {
0281 std::string fieldname = "g" + name;
0282 if (m_SingleUInt64EntryMap.find(fieldname) == m_SingleUInt64EntryMap.end())
0283 {
0284 if (m_Locked[SingleEntries])
0285 {
0286 std::cout << "Trying to add field " << name << " after another entry was committed" << std::endl;
0287 std::cout << "That does not work, restructure your code" << std::endl;
0288 gSystem->Exit(1);
0289 }
0290 m_SingleUInt64EntryMap.insert(std::make_pair(fieldname, value));
0291 return;
0292 }
0293 m_SingleUInt64EntryMap[fieldname] = value;
0294 }
0295
0296 void CDBTTree::CommitSingle()
0297 {
0298 m_Locked[SingleEntries] = true;
0299 return;
0300 }
0301
0302 void CDBTTree::WriteSingleCDBTTree()
0303 {
0304 m_TTree[SingleEntries] = new TTree(m_TTreeName[SingleEntries].c_str(), m_TTreeName[SingleEntries].c_str());
0305 for (auto &field : m_SingleFloatEntryMap)
0306 {
0307 std::string fielddescriptor = field.first + "/F";
0308 m_TTree[SingleEntries]->Branch(field.first.c_str(), &field.second, fielddescriptor.c_str());
0309 }
0310 for (auto &field : m_SingleDoubleEntryMap)
0311 {
0312 std::string fielddescriptor = field.first + "/D";
0313 m_TTree[SingleEntries]->Branch(field.first.c_str(), &field.second, fielddescriptor.c_str());
0314 }
0315 for (auto &field : m_SingleIntEntryMap)
0316 {
0317 std::string fielddescriptor = field.first + "/I";
0318 m_TTree[SingleEntries]->Branch(field.first.c_str(), &field.second, fielddescriptor.c_str());
0319 }
0320 for (auto &field : m_SingleUInt64EntryMap)
0321 {
0322 std::string fielddescriptor = field.first + "/g";
0323 m_TTree[SingleEntries]->Branch(field.first.c_str(), &field.second, fielddescriptor.c_str());
0324 }
0325
0326 m_TTree[SingleEntries]->Fill();
0327 return;
0328 }
0329
0330 void CDBTTree::Print()
0331 {
0332 if (!m_FloatEntryMap.empty())
0333 {
0334 std::cout << "Number of float entries: " << m_FloatEntryMap.size() << std::endl;
0335 for (auto &field : m_FloatEntryMap)
0336 {
0337 std::cout << "ID: " << field.first << std::endl;
0338 for (auto &calibs : field.second)
0339 {
0340 std::string tmpstring = calibs.first;
0341 tmpstring.erase(0, 1);
0342 std::cout << "name " << tmpstring << " value: " << calibs.second << std::endl;
0343 }
0344 }
0345 std::cout << "--------------------------------------------------" << std::endl
0346 << std::endl;
0347 }
0348 if (!m_DoubleEntryMap.empty())
0349 {
0350 std::cout << "Number of double entries: " << m_DoubleEntryMap.size() << std::endl;
0351 for (auto &field : m_DoubleEntryMap)
0352 {
0353 std::cout << "ID: " << field.first << std::endl;
0354 for (auto &calibs : field.second)
0355 {
0356 std::string tmpstring = calibs.first;
0357 tmpstring.erase(0, 1);
0358 std::cout << "name " << tmpstring << " value: " << calibs.second << std::endl;
0359 }
0360 }
0361 std::cout << "--------------------------------------------------" << std::endl
0362 << std::endl;
0363 }
0364 if (!m_IntEntryMap.empty())
0365 {
0366 std::cout << "Number of int entries: " << m_IntEntryMap.size() << std::endl;
0367 for (auto &field : m_IntEntryMap)
0368 {
0369 std::cout << "ID: " << field.first << std::endl;
0370 for (auto &calibs : field.second)
0371 {
0372 std::string tmpstring = calibs.first;
0373 tmpstring.erase(0, 1);
0374 std::cout << "name " << tmpstring << " value: " << calibs.second << std::endl;
0375 }
0376 }
0377 }
0378 if (!m_UInt64EntryMap.empty())
0379 {
0380 std::cout << "Number of uint64 entries: " << m_UInt64EntryMap.size() << std::endl;
0381 for (auto &field : m_UInt64EntryMap)
0382 {
0383 std::cout << "ID: " << field.first << std::endl;
0384 for (auto &calibs : field.second)
0385 {
0386 std::string tmpstring = calibs.first;
0387 tmpstring.erase(0, 1);
0388 std::cout << "name " << tmpstring << " value: " << calibs.second << std::endl;
0389 }
0390 }
0391 }
0392
0393 if (!m_SingleFloatEntryMap.empty())
0394 {
0395 std::cout << "Number of single float fields: " << m_SingleFloatEntryMap.size() << std::endl;
0396 for (auto &field : m_SingleFloatEntryMap)
0397 {
0398 std::string tmpstring = field.first;
0399 tmpstring.erase(0, 1);
0400 std::cout << tmpstring << " value " << field.second << std::endl;
0401 }
0402 }
0403 if (!m_SingleDoubleEntryMap.empty())
0404 {
0405 std::cout << "Number of single double fields: " << m_SingleDoubleEntryMap.size() << std::endl;
0406
0407 std::ios oldState(nullptr);
0408 oldState.copyfmt(std::cout);
0409 std::cout.precision(std::numeric_limits<double>::max_digits10);
0410 for (auto &field : m_SingleDoubleEntryMap)
0411 {
0412 std::string tmpstring = field.first;
0413 tmpstring.erase(0, 1);
0414 std::cout << tmpstring << " value " << field.second << std::endl;
0415 }
0416 std::cout.copyfmt(oldState);
0417 }
0418 if (!m_SingleIntEntryMap.empty())
0419 {
0420 std::cout << "Number of single int fields: " << m_SingleIntEntryMap.size() << std::endl;
0421 for (auto &field : m_SingleIntEntryMap)
0422 {
0423 std::string tmpstring = field.first;
0424 tmpstring.erase(0, 1);
0425 std::cout << tmpstring << " value " << field.second << std::endl;
0426 }
0427 }
0428 if (!m_SingleUInt64EntryMap.empty())
0429 {
0430 std::cout << "Number of single uint64 fields: " << m_SingleUInt64EntryMap.size() << std::endl;
0431 for (auto &field : m_SingleUInt64EntryMap)
0432 {
0433 std::string tmpstring = field.first;
0434 tmpstring.erase(0, 1);
0435 std::cout << tmpstring << " value " << field.second << std::endl;
0436 }
0437 }
0438 }
0439
0440 void CDBTTree::WriteCDBTTree()
0441 {
0442 bool empty_single = m_SingleFloatEntryMap.empty() && m_SingleDoubleEntryMap.empty() &&
0443 m_SingleIntEntryMap.empty() && m_SingleUInt64EntryMap.empty();
0444 if (!empty_single && !m_Locked[SingleEntries])
0445 {
0446 std::cout << "You need to call CDBTTree::CommitSingle() before writing" << std::endl;
0447 return;
0448 }
0449 bool empty_multiple = m_FloatEntryMap.empty() && m_DoubleEntryMap.empty() &&
0450 m_IntEntryMap.empty() && m_UInt64EntryMap.empty();
0451 if (!empty_multiple && !m_Locked[MultipleEntries])
0452 {
0453 std::cout << "You need to call CDBTTree::Commit() before writing" << std::endl;
0454 return;
0455 }
0456 if (empty_single && empty_multiple)
0457 {
0458 std::cout << "no values to be saved" << std::endl;
0459 return;
0460 }
0461
0462 std::string currdir = gDirectory->GetPath();
0463
0464 TFile *f = TFile::Open(m_Filename.c_str(), "RECREATE");
0465 if (!empty_single)
0466 {
0467 WriteSingleCDBTTree();
0468 m_TTree[SingleEntries]->Write();
0469 }
0470 if (!empty_multiple)
0471 {
0472 WriteMultipleCDBTTree();
0473 m_TTree[MultipleEntries]->Write();
0474 }
0475 f->Close();
0476
0477 gROOT->cd(currdir.c_str());
0478 }
0479
0480 void CDBTTree::LoadCalibrations()
0481 {
0482 std::string currdir = gDirectory->GetPath();
0483
0484 if (m_Filename.empty())
0485 {
0486 std::cout << PHWHERE << "No filename given in ctor or via SetFilename()" << std::endl;
0487 gSystem->Exit(1);
0488 exit(1);
0489 }
0490 TFile *f = TFile::Open(m_Filename.c_str());
0491 if (!f)
0492 {
0493 std::cout << PHWHERE << "TFile::Open(" << m_Filename << ") failed" << std::endl;
0494 gSystem->Exit(1);
0495 exit(1);
0496 }
0497 f->GetObject(m_TTreeName[SingleEntries].c_str(), m_TTree[SingleEntries]);
0498 f->GetObject(m_TTreeName[MultipleEntries].c_str(), m_TTree[MultipleEntries]);
0499 if (m_TTree[SingleEntries] != nullptr)
0500 {
0501 TObjArray *branches = m_TTree[SingleEntries]->GetListOfBranches();
0502 TIter iter(branches);
0503 while (TBranch *thisbranch = static_cast<TBranch *>(iter.Next()))
0504 {
0505
0506 std::string DataType = thisbranch->GetLeaf(thisbranch->GetName())->GetTypeName();
0507 if (DataType == "Float_t")
0508 {
0509 auto itermap = m_SingleFloatEntryMap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<float>::quiet_NaN()));
0510 m_TTree[SingleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0511 }
0512 else if (DataType == "Double_t")
0513 {
0514 auto itermap = m_SingleDoubleEntryMap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<double>::quiet_NaN()));
0515 m_TTree[SingleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0516 }
0517 else if (DataType == "Int_t")
0518 {
0519 auto itermap = m_SingleIntEntryMap.insert(std::make_pair(thisbranch->GetName(), -99999));
0520 m_TTree[SingleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0521 }
0522 else if (DataType == "ULong_t")
0523 {
0524 auto itermap = m_SingleUInt64EntryMap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<uint64_t>::max()));
0525 m_TTree[SingleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0526 }
0527 else
0528 {
0529 std::cout << __PRETTY_FUNCTION__ << " data type " << DataType
0530 << " in " << m_TTree[SingleEntries]->GetName()
0531 << " from " << f->GetName()
0532 << " not implemented" << std::endl;
0533 gSystem->Exit(1);
0534 }
0535 }
0536 m_TTree[SingleEntries]->GetEntry(0);
0537 }
0538 if (m_TTree[MultipleEntries] != nullptr)
0539 {
0540 TObjArray *branches = m_TTree[MultipleEntries]->GetListOfBranches();
0541 TIter iter(branches);
0542 std::map<std::string, float> floatvalmap;
0543 std::map<std::string, double> doublevalmap;
0544 std::map<std::string, int> intvalmap;
0545 std::map<std::string, uint64_t> uint64valmap;
0546 while (TBranch *thisbranch = static_cast<TBranch *>(iter.Next()))
0547 {
0548
0549 std::string DataType = thisbranch->GetLeaf(thisbranch->GetName())->GetTypeName();
0550 if (DataType == "Float_t")
0551 {
0552 auto itermap = floatvalmap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<float>::quiet_NaN()));
0553 m_TTree[MultipleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0554 }
0555 if (DataType == "Double_t")
0556 {
0557 auto itermap = doublevalmap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<double>::quiet_NaN()));
0558 m_TTree[MultipleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0559 }
0560 if (DataType == "Int_t")
0561 {
0562 auto itermap = intvalmap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<int>::min()));
0563 m_TTree[MultipleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0564 }
0565 if (DataType == "ULong_t")
0566 {
0567 auto itermap = uint64valmap.insert(std::make_pair(thisbranch->GetName(), std::numeric_limits<uint64_t>::max()));
0568 m_TTree[MultipleEntries]->SetBranchAddress(thisbranch->GetName(), &(itermap.first)->second);
0569 }
0570 }
0571 for (auto entry = 0; entry < m_TTree[MultipleEntries]->GetEntries(); ++entry)
0572 {
0573 for (auto &field : floatvalmap)
0574 {
0575 field.second = std::numeric_limits<float>::quiet_NaN();
0576 }
0577 for (auto &field : doublevalmap)
0578 {
0579 field.second = std::numeric_limits<double>::quiet_NaN();
0580 }
0581 for (auto &field : intvalmap)
0582 {
0583 field.second = std::numeric_limits<int>::min();
0584 }
0585 for (auto &field : uint64valmap)
0586 {
0587 field.second = std::numeric_limits<uint64_t>::max();
0588 }
0589 m_TTree[MultipleEntries]->GetEntry(entry);
0590 int ID = intvalmap.find("IID")->second;
0591 std::map<std::string, float> tmp_floatvalmap;
0592 for (auto &field : floatvalmap)
0593 {
0594 if (std::isfinite(field.second))
0595 {
0596 tmp_floatvalmap.insert(std::make_pair(field.first, field.second));
0597 }
0598 }
0599 if (!tmp_floatvalmap.empty())
0600 {
0601 m_FloatEntryMap.insert(std::make_pair(ID, tmp_floatvalmap));
0602 }
0603
0604 std::map<std::string, double> tmp_doublevalmap;
0605 for (auto &field : doublevalmap)
0606 {
0607 if (std::isfinite(field.second))
0608 {
0609 tmp_doublevalmap.insert(std::make_pair(field.first, field.second));
0610 }
0611 }
0612 if (!tmp_doublevalmap.empty())
0613 {
0614 m_DoubleEntryMap.insert(std::make_pair(ID, tmp_doublevalmap));
0615 }
0616
0617 std::map<std::string, int> tmp_intvalmap;
0618 for (auto &field : intvalmap)
0619 {
0620 if (field.second != std::numeric_limits<int>::min() && field.first != "IID")
0621 {
0622 tmp_intvalmap.insert(std::make_pair(field.first, field.second));
0623 }
0624 }
0625 if (!tmp_intvalmap.empty())
0626 {
0627 m_IntEntryMap.insert(std::make_pair(ID, tmp_intvalmap));
0628 }
0629
0630 std::map<std::string, uint64_t> tmp_uint64valmap;
0631 for (auto &field : uint64valmap)
0632 {
0633 if (field.second != std::numeric_limits<uint64_t>::max())
0634 {
0635 tmp_uint64valmap.insert(std::make_pair(field.first, field.second));
0636 }
0637 }
0638 if (!tmp_uint64valmap.empty())
0639 {
0640 m_UInt64EntryMap.insert(std::make_pair(ID, tmp_uint64valmap));
0641 }
0642 }
0643 }
0644 for (auto ttree : m_TTree)
0645 {
0646 delete ttree;
0647 ttree = nullptr;
0648 }
0649 f->Close();
0650 gROOT->cd(currdir.c_str());
0651 }
0652
0653 float CDBTTree::GetSingleFloatValue(const std::string &name, int verbose)
0654 {
0655 if (m_SingleFloatEntryMap.empty())
0656 {
0657 LoadCalibrations();
0658 }
0659 std::string fieldname = "F" + name;
0660 auto singleiter = m_SingleFloatEntryMap.find(fieldname);
0661 if (singleiter == m_SingleFloatEntryMap.end())
0662 {
0663 if (verbose > 0)
0664 {
0665 std::cout << "Could not find " << name << " in single float calibrations" << std::endl;
0666 std::cout << "Existing values:" << std::endl;
0667 for (auto &eiter : m_SingleFloatEntryMap)
0668 {
0669 std::string tmpstring = eiter.first;
0670 tmpstring.erase(0, 1);
0671 std::cout << "name : " << tmpstring << ", value " << eiter.second
0672 << std::endl;
0673 }
0674 }
0675 return std::numeric_limits<float>::quiet_NaN();
0676 }
0677 return singleiter->second;
0678 }
0679
0680 float CDBTTree::GetFloatValue(int channel, const std::string &name, int verbose)
0681 {
0682 if (m_FloatEntryMap.empty())
0683 {
0684 LoadCalibrations();
0685 }
0686 auto channelmapiter = m_FloatEntryMap.find(channel);
0687 if (channelmapiter == m_FloatEntryMap.end())
0688 {
0689 if (verbose > 0)
0690 {
0691 std::cout << PHWHERE << " Could not find channel " << channel
0692 << " for " << name << " in float calibrations" << std::endl;
0693 }
0694 return std::numeric_limits<float>::quiet_NaN();
0695 }
0696 std::string fieldname = "F" + name;
0697 auto calibiter = channelmapiter->second.find(fieldname);
0698 if (calibiter == channelmapiter->second.end())
0699 {
0700 if (verbose > 0)
0701 {
0702 std::cout << "Could not find " << name << " among float calibrations of channel " << channel << std::endl;
0703 }
0704 return std::numeric_limits<float>::quiet_NaN();
0705 }
0706 return calibiter->second;
0707 }
0708
0709 double CDBTTree::GetSingleDoubleValue(const std::string &name, int verbose)
0710 {
0711 if (m_SingleDoubleEntryMap.empty())
0712 {
0713 LoadCalibrations();
0714 }
0715 std::string fieldname = "D" + name;
0716 auto singleiter = m_SingleDoubleEntryMap.find(fieldname);
0717 if (singleiter == m_SingleDoubleEntryMap.end())
0718 {
0719 if (verbose > 0)
0720 {
0721 std::cout << "Could not find " << name << " in single double calibrations" << std::endl;
0722 std::cout << "Existing values:" << std::endl;
0723 for (auto &eiter : m_SingleDoubleEntryMap)
0724 {
0725 std::string tmpstring = eiter.first;
0726 tmpstring.erase(0, 1);
0727 std::cout << "name : " << tmpstring << ", value " << eiter.second
0728 << std::endl;
0729 }
0730 }
0731 return std::numeric_limits<double>::quiet_NaN();
0732 }
0733 return singleiter->second;
0734 }
0735
0736 double CDBTTree::GetDoubleValue(int channel, const std::string &name, int verbose)
0737 {
0738 if (m_DoubleEntryMap.empty())
0739 {
0740 LoadCalibrations();
0741 }
0742 auto channelmapiter = m_DoubleEntryMap.find(channel);
0743 if (channelmapiter == m_DoubleEntryMap.end())
0744 {
0745 if (verbose > 0)
0746 {
0747 std::cout << PHWHERE << " Could not find channel " << channel
0748 << " for " << name << " in double calibrations" << std::endl;
0749 }
0750 return std::numeric_limits<double>::quiet_NaN();
0751 }
0752 std::string fieldname = "D" + name;
0753 auto calibiter = channelmapiter->second.find(fieldname);
0754 if (calibiter == channelmapiter->second.end())
0755 {
0756 if (verbose > 0)
0757 {
0758 std::cout << "Could not find " << name << " among double calibrations for channel " << channel << std::endl;
0759 }
0760 return std::numeric_limits<double>::quiet_NaN();
0761 }
0762 return calibiter->second;
0763 }
0764
0765 int CDBTTree::GetSingleIntValue(const std::string &name, int verbose)
0766 {
0767 if (m_SingleIntEntryMap.empty())
0768 {
0769 LoadCalibrations();
0770 }
0771 std::string fieldname = "I" + name;
0772 auto singleiter = m_SingleIntEntryMap.find(fieldname);
0773 if (singleiter == m_SingleIntEntryMap.end())
0774 {
0775 if (verbose > 0)
0776 {
0777 std::cout << "Could not find " << name << " in single int calibrations" << std::endl;
0778 std::cout << "Existing values:" << std::endl;
0779 for (auto &eiter : m_SingleIntEntryMap)
0780 {
0781 std::string tmpstring = eiter.first;
0782 tmpstring.erase(0, 1);
0783 std::cout << "name : " << tmpstring << ", value " << eiter.second
0784 << std::endl;
0785 }
0786 }
0787 return std::numeric_limits<int>::min();
0788 }
0789 return singleiter->second;
0790 }
0791
0792 int CDBTTree::GetIntValue(int channel, const std::string &name, int verbose)
0793 {
0794 if (m_IntEntryMap.empty())
0795 {
0796 LoadCalibrations();
0797 }
0798 auto channelmapiter = m_IntEntryMap.find(channel);
0799 if (channelmapiter == m_IntEntryMap.end())
0800 {
0801 if (verbose > 0)
0802 {
0803 std::cout << PHWHERE << " Could not find channel " << channel
0804 << " for " << name << " in int calibrations" << std::endl;
0805 }
0806 return std::numeric_limits<int>::min();
0807 }
0808 std::string fieldname = "I" + name;
0809 auto calibiter = channelmapiter->second.find(fieldname);
0810 if (calibiter == channelmapiter->second.end())
0811 {
0812 if (verbose > 0)
0813 {
0814 std::cout << "Could not find " << name << " among int calibrations for channel " << channel << std::endl;
0815 }
0816 return std::numeric_limits<int>::min();
0817 }
0818 return calibiter->second;
0819 }
0820
0821 uint64_t CDBTTree::GetSingleUInt64Value(const std::string &name, int verbose)
0822 {
0823 if (m_SingleUInt64EntryMap.empty())
0824 {
0825 LoadCalibrations();
0826 }
0827 std::string fieldname = "g" + name;
0828 auto singleiter = m_SingleUInt64EntryMap.find(fieldname);
0829 if (singleiter == m_SingleUInt64EntryMap.end())
0830 {
0831 if (verbose > 0)
0832 {
0833 std::cout << "Could not find " << name << " in single uint64 calibrations" << std::endl;
0834 std::cout << "Existing values:" << std::endl;
0835 for (auto &eiter : m_SingleUInt64EntryMap)
0836 {
0837 std::string tmpstring = eiter.first;
0838 tmpstring.erase(0, 1);
0839 std::cout << "name : " << tmpstring << ", value " << eiter.second
0840 << std::endl;
0841 }
0842 }
0843 return std::numeric_limits<uint64_t>::max();
0844 }
0845 return singleiter->second;
0846 }
0847
0848 uint64_t CDBTTree::GetUInt64Value(int channel, const std::string &name, int verbose)
0849 {
0850 if (m_UInt64EntryMap.empty())
0851 {
0852 LoadCalibrations();
0853 }
0854 auto channelmapiter = m_UInt64EntryMap.find(channel);
0855 if (channelmapiter == m_UInt64EntryMap.end())
0856 {
0857 if (verbose > 0)
0858 {
0859 std::cout << "Could not find channel " << channel << " in unint64 calibrations" << std::endl;
0860 }
0861 return std::numeric_limits<uint64_t>::max();
0862 }
0863 std::string fieldname = "g" + name;
0864 auto calibiter = channelmapiter->second.find(fieldname);
0865 if (calibiter == channelmapiter->second.end())
0866 {
0867 if (verbose > 0)
0868 {
0869 std::cout << PHWHERE << " Could not find channel " << channel
0870 << " for " << name << " in uint64_t calibrations" << std::endl;
0871 }
0872 return std::numeric_limits<uint64_t>::max();
0873 }
0874 return calibiter->second;
0875 }