File indexing completed on 2024-04-06 12:10:10
0001 #include "DQMServices/Core/interface/LegacyIOHelper.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003
0004 #include <cstdio>
0005 #include <cfloat>
0006 #include <vector>
0007 #include <string>
0008
0009 #include "TString.h"
0010 #include "TSystem.h"
0011 #include "TFile.h"
0012 #include "TKey.h"
0013 #include <sys/stat.h>
0014
0015 void LegacyIOHelper::save(std::string const &filename,
0016 std::string const &path ,
0017 uint32_t const run ,
0018 bool saveall ,
0019 std::string const &fileupdate ) {
0020
0021
0022
0023
0024
0025 class TFileNoSync : public TFile {
0026 public:
0027 TFileNoSync(char const *file, char const *opt) : TFile{file, opt} {}
0028 Int_t SysSync(Int_t) override { return 0; }
0029 };
0030
0031 std::cout << "DQMFileSaver::globalEndRun()" << std::endl;
0032
0033 char suffix[64];
0034 sprintf(suffix, "R%09d", run);
0035 TFileNoSync *file = new TFileNoSync(filename.c_str(), fileupdate.c_str());
0036
0037
0038 std::vector<MonitorElement *> mes;
0039 if (saveall) {
0040
0041 mes = dbe_->getAllContents(path);
0042 } else {
0043
0044 mes = dbe_->getAllContents(path, run, 0);
0045 }
0046
0047 for (auto me : mes) {
0048
0049
0050
0051
0052 std::string dirName = me->getPathname();
0053 uint64_t firstSlashPos = dirName.find('/');
0054 if (firstSlashPos == std::string::npos) {
0055 firstSlashPos = dirName.length();
0056 }
0057
0058 if (run) {
0059
0060
0061 dirName = dirName.substr(0, firstSlashPos) + "/Run summary" + dirName.substr(firstSlashPos, dirName.size());
0062 dirName = "DQMData/Run " + std::to_string(run) + "/" + dirName;
0063 }
0064
0065 std::string objectName = me->getName();
0066
0067
0068 createDirectoryIfNeededAndCd(dirName);
0069
0070
0071
0072
0073 if (me->kind() == MonitorElement::Kind::INT) {
0074 int value = me->getIntValue();
0075 std::string content = "<" + objectName + ">i=" + std::to_string(value) + "</" + objectName + ">";
0076 TObjString str(content.c_str());
0077 str.Write();
0078 } else if (me->kind() == MonitorElement::Kind::REAL) {
0079 double value = me->getFloatValue();
0080 char buf[64];
0081
0082 std::snprintf(buf, sizeof(buf), "%.*g", DBL_DIG + 2, value);
0083 std::string content = "<" + objectName + ">f=" + buf + "</" + objectName + ">";
0084 TObjString str(content.c_str());
0085 str.Write();
0086 } else if (me->kind() == MonitorElement::Kind::STRING) {
0087 const std::string &value = me->getStringValue();
0088 std::string content = "<" + objectName + ">s=" + value + "</" + objectName + ">";
0089 TObjString str(content.c_str());
0090 str.Write();
0091 } else {
0092
0093 TH1 *value = me->getTH1();
0094 value->Write();
0095
0096 if (me->getEfficiencyFlag()) {
0097 std::string content = "<" + objectName + ">e=1</" + objectName + ">";
0098 TObjString str(content.c_str());
0099 str.Write();
0100 }
0101
0102 for (QReport *qr : me->getQReports()) {
0103 std::string result;
0104
0105 char buf[64];
0106 std::snprintf(buf, sizeof(buf), "qr=st:%d:%.*g:", qr->getStatus(), DBL_DIG + 2, qr->getQTresult());
0107 result = '<' + objectName + '.' + qr->getQRName() + '>';
0108 result += buf;
0109 result += qr->getAlgorithm() + ':' + qr->getMessage();
0110 result += "</" + objectName + '.' + qr->getQRName() + '>';
0111 TObjString str(result.c_str());
0112 str.Write();
0113 }
0114 }
0115
0116
0117 gDirectory->cd("/");
0118 }
0119
0120 file->Close();
0121 }
0122
0123
0124
0125
0126 bool LegacyIOHelper::createDirectoryIfNeededAndCd(const std::string &path) {
0127 assert(!path.empty());
0128
0129
0130 size_t start = 0;
0131 size_t end = path.find('/', start);
0132 if (end == std::string::npos)
0133 end = path.size();
0134
0135 while (true) {
0136
0137
0138 std::string part(path, start, end - start);
0139 TObject *o = gDirectory->Get(part.c_str());
0140 if (o && !dynamic_cast<TDirectory *>(o))
0141 throw cms::Exception("DQMFileSaver") << "Attempt to create directory '" << path
0142 << "' in a file"
0143 " fails because the part '"
0144 << part
0145 << "' already exists and is not"
0146 " directory";
0147 else if (!o)
0148 gDirectory->mkdir(part.c_str());
0149
0150 if (!gDirectory->cd(part.c_str()))
0151 throw cms::Exception("DQMFileSaver") << "Attempt to create directory '" << path
0152 << "' in a file"
0153 " fails because could not cd into subdirectory '"
0154 << part << "'";
0155
0156
0157 if (end + 1 >= path.size())
0158 break;
0159
0160
0161 start = end + 1;
0162 end = path.find('/', start);
0163 if (end == std::string::npos)
0164 end = path.size();
0165 }
0166
0167 return true;
0168 }
0169
0170 bool LegacyIOHelper::readdir(TDirectory *dir, const std::string &toppath) {
0171 TDirectory *dirsav = gDirectory;
0172 LogDebug("LegacyIOHelper") << "Inside:" << gDirectory->GetPath() << std::endl;
0173 TIter next(dir->GetListOfKeys());
0174 TKey *key;
0175 while ((key = (TKey *)next())) {
0176 if (key->IsFolder()) {
0177 LogDebug("LegacyIOHelper") << key->GetName() << std::endl;
0178 dir->cd(key->GetName());
0179 TDirectory *subdir = gDirectory;
0180 readdir(subdir, toppath);
0181 dirsav->cd();
0182 continue;
0183 } else {
0184 TClass *cl = gROOT->GetClass(key->GetClassName());
0185 std::string meName;
0186 if (cl->InheritsFrom("TProfile")) {
0187 TProfile *h = dynamic_cast<TProfile *>(key->ReadObject<TProfile>()->Clone());
0188 h->SetDirectory(nullptr);
0189 if (h) {
0190 getMEName<TProfile>(h, toppath, meName);
0191 data_.insert(dbe_->bookProfile(meName, h));
0192 }
0193 } else if (cl->InheritsFrom("TProfile2D")) {
0194 TProfile2D *h = dynamic_cast<TProfile2D *>(key->ReadObject<TProfile2D>()->Clone());
0195 h->SetDirectory(nullptr);
0196 if (h) {
0197 getMEName<TProfile2D>(h, toppath, meName);
0198 data_.insert(dbe_->bookProfile2D(meName, h));
0199 }
0200 } else if (cl->InheritsFrom("TH1F")) {
0201 TH1F *h = dynamic_cast<TH1F *>(key->ReadObject<TH1F>()->Clone());
0202 h->SetDirectory(nullptr);
0203 if (h) {
0204 getMEName<TH1F>(h, toppath, meName);
0205 data_.insert(dbe_->book1D(meName, h));
0206 }
0207 } else if (cl->InheritsFrom("TH1S")) {
0208 TH1S *h = dynamic_cast<TH1S *>(key->ReadObject<TH1S>()->Clone());
0209 h->SetDirectory(nullptr);
0210 if (h) {
0211 getMEName<TH1S>(h, toppath, meName);
0212 data_.insert(dbe_->book1S(meName, h));
0213 }
0214 } else if (cl->InheritsFrom("TH1D")) {
0215 TH1D *h = dynamic_cast<TH1D *>(key->ReadObject<TH1D>()->Clone());
0216 h->SetDirectory(nullptr);
0217 if (h) {
0218 getMEName<TH1D>(h, toppath, meName);
0219 data_.insert(dbe_->book1DD(meName, h));
0220 }
0221 } else if (cl->InheritsFrom("TH1I")) {
0222 TH1I *h = dynamic_cast<TH1I *>(key->ReadObject<TH1I>()->Clone());
0223 h->SetDirectory(nullptr);
0224 if (h) {
0225 getMEName<TH1I>(h, toppath, meName);
0226 data_.insert(dbe_->book1I(meName, h));
0227 }
0228 } else if (cl->InheritsFrom("TH2F")) {
0229 TH2F *h = dynamic_cast<TH2F *>(key->ReadObject<TH2F>()->Clone());
0230 h->SetDirectory(nullptr);
0231 if (h) {
0232 getMEName<TH2F>(h, toppath, meName);
0233 data_.insert(dbe_->book2D(meName, h));
0234 }
0235 } else if (cl->InheritsFrom("TH2S")) {
0236 TH2S *h = dynamic_cast<TH2S *>(key->ReadObject<TH2S>()->Clone());
0237 h->SetDirectory(nullptr);
0238 if (h) {
0239 getMEName<TH2S>(h, toppath, meName);
0240 data_.insert(dbe_->book2S(meName, h));
0241 }
0242 } else if (cl->InheritsFrom("TH2D")) {
0243 TH2D *h = dynamic_cast<TH2D *>(key->ReadObject<TH2D>()->Clone());
0244 h->SetDirectory(nullptr);
0245 if (h) {
0246 getMEName<TH2D>(h, toppath, meName);
0247 data_.insert(dbe_->book2DD(meName, h));
0248 }
0249 } else if (cl->InheritsFrom("TH2I")) {
0250 TH2I *h = dynamic_cast<TH2I *>(key->ReadObject<TH2I>()->Clone());
0251 h->SetDirectory(nullptr);
0252 if (h) {
0253 getMEName<TH2I>(h, toppath, meName);
0254 data_.insert(dbe_->book2I(meName, h));
0255 }
0256 } else if (cl->InheritsFrom("TH3F")) {
0257 TH3F *h = dynamic_cast<TH3F *>(key->ReadObject<TH3F>()->Clone());
0258 h->SetDirectory(nullptr);
0259 if (h) {
0260 getMEName<TH3F>(h, toppath, meName);
0261 data_.insert(dbe_->book3D(meName, h));
0262 }
0263 }
0264 }
0265 }
0266 if (!data_.empty())
0267 return true;
0268 return false;
0269 }
0270
0271 bool LegacyIOHelper::open(std::string const &filename, std::string const &path, uint32_t const run) {
0272 TFile *f1 = TFile::Open(filename.c_str());
0273 if (!f1)
0274 return false;
0275 std::ostringstream toppath;
0276 toppath << filename << ":/DQMData/Run " << run << "/";
0277 std::string dirpath = toppath.str();
0278 edm::LogPrint("LegacyIOHelper") << dirpath << std::endl;
0279 bool flag = readdir(f1, dirpath);
0280 f1->Close();
0281 return flag;
0282 }