Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-02 23:48:50

0001 // -*- C++ -*-
0002 //
0003 // Package:     FwkIO
0004 // Class  :     DQMRootSource
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Tue May  3 11:13:47 CDT 2011
0011 //
0012 
0013 // system include files
0014 #include <memory>
0015 
0016 #include "TFile.h"
0017 #include "TString.h"
0018 #include "TTree.h"
0019 #include <map>
0020 #include <string>
0021 #include <vector>
0022 
0023 #include "DQMServices/Core/interface/DQMStore.h"
0024 #include "DataFormats/Histograms/interface/DQMToken.h"
0025 #include "FWCore/ServiceRegistry/interface/Service.h"
0026 
0027 #include "FWCore/Framework/interface/InputSource.h"
0028 #include "FWCore/Sources/interface/PuttableSourceBase.h"
0029 #include "FWCore/Catalog/interface/InputFileCatalog.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/ExceptionPropagate.h"
0032 
0033 #include "FWCore/Framework/interface/RunPrincipal.h"
0034 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0035 
0036 #include "FWCore/Framework/interface/Run.h"
0037 #include "FWCore/Framework/interface/LuminosityBlock.h"
0038 #include "DataFormats/Provenance/interface/LuminosityBlockID.h"
0039 #include "DataFormats/Provenance/interface/LuminosityBlockRange.h"
0040 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
0041 
0042 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0043 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0044 
0045 #include "FWCore/Framework/interface/InputSourceMacros.h"
0046 #include "FWCore/Framework/interface/FileBlock.h"
0047 
0048 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0049 #include "FWCore/MessageLogger/interface/JobReport.h"
0050 #include "FWCore/Utilities/interface/TimeOfDay.h"
0051 
0052 #include "format.h"
0053 
0054 // class rather than namespace so we can make this a friend of the
0055 // MonitorElement to get access to constructors etc.
0056 struct DQMTTreeIO {
0057   typedef dqm::harvesting::MonitorElement MonitorElement;
0058   typedef dqm::harvesting::DQMStore DQMStore;
0059 
0060   // TODO: this should probably be moved somewhere else
0061   class DQMMergeHelper {
0062   public:
0063     // Utility function to check the consistency of the axis labels
0064     // Taken from TH1::CheckBinLabels which is not public
0065     static bool CheckBinLabels(const TAxis* a1, const TAxis* a2) {
0066       // Check that axis have same labels
0067       THashList* l1 = (const_cast<TAxis*>(a1))->GetLabels();
0068       THashList* l2 = (const_cast<TAxis*>(a2))->GetLabels();
0069 
0070       if (!l1 && !l2)
0071         return true;
0072       if (!l1 || !l2) {
0073         return false;
0074       }
0075 
0076       // Check now labels sizes  are the same
0077       if (l1->GetSize() != l2->GetSize()) {
0078         return false;
0079       }
0080 
0081       for (int i = 1; i <= a1->GetNbins(); ++i) {
0082         std::string_view label1 = a1->GetBinLabel(i);
0083         std::string_view label2 = a2->GetBinLabel(i);
0084         if (label1 != label2) {
0085           return false;
0086         }
0087       }
0088 
0089       return true;
0090     }
0091 
0092     // NOTE: the merge logic comes from DataFormats/Histograms/interface/MEtoEDMFormat.h
0093     static void mergeTogether(TH1* original, TH1* toAdd) {
0094       if (original->CanExtendAllAxes() && toAdd->CanExtendAllAxes()) {
0095         TList list;
0096         list.Add(toAdd);
0097         if (original->Merge(&list) == -1) {
0098           edm::LogError("MergeFailure") << "Failed to merge DQM element " << original->GetName();
0099         }
0100       } else {
0101         // TODO: Redo. This is both more strict than what ROOT checks for yet
0102         // allows cases where ROOT fails with merging.
0103         if (original->GetNbinsX() == toAdd->GetNbinsX() &&
0104             original->GetXaxis()->GetXmin() == toAdd->GetXaxis()->GetXmin() &&
0105             original->GetXaxis()->GetXmax() == toAdd->GetXaxis()->GetXmax() &&
0106             original->GetNbinsY() == toAdd->GetNbinsY() &&
0107             original->GetYaxis()->GetXmin() == toAdd->GetYaxis()->GetXmin() &&
0108             original->GetYaxis()->GetXmax() == toAdd->GetYaxis()->GetXmax() &&
0109             original->GetNbinsZ() == toAdd->GetNbinsZ() &&
0110             original->GetZaxis()->GetXmin() == toAdd->GetZaxis()->GetXmin() &&
0111             original->GetZaxis()->GetXmax() == toAdd->GetZaxis()->GetXmax() &&
0112             CheckBinLabels(original->GetXaxis(), toAdd->GetXaxis()) &&
0113             CheckBinLabels(original->GetYaxis(), toAdd->GetYaxis()) &&
0114             CheckBinLabels(original->GetZaxis(), toAdd->GetZaxis())) {
0115           original->Add(toAdd);
0116         } else {
0117           edm::LogError("MergeFailure") << "Found histograms with different axis limits or different labels '"
0118                                         << original->GetName() << "' not merged.";
0119         }
0120       }
0121     }
0122   };
0123 
0124   // This struct allows to find all MEs belonging to a run-lumi pair
0125   // All files will be open at once so m_file property indicates the file where data is saved.
0126   struct FileMetadata {
0127     unsigned int m_run;
0128     unsigned int m_lumi;
0129     ULong64_t m_beginTime;
0130     ULong64_t m_endTime;
0131     ULong64_t m_firstIndex;
0132     ULong64_t m_lastIndex;  // Last is inclusive
0133     unsigned int m_type;
0134     TFile* m_file;
0135 
0136     // This will be used when sorting a vector
0137     bool operator<(const FileMetadata& obj) const {
0138       if (m_run == obj.m_run)
0139         return m_lumi < obj.m_lumi;
0140       else
0141         return m_run < obj.m_run;
0142     }
0143 
0144     void describe() {
0145       std::cout << "read r:" << m_run << " l:" << m_lumi << " bt:" << m_beginTime << " et:" << m_endTime
0146                 << " fi:" << m_firstIndex << " li:" << m_lastIndex << " type:" << m_type << " file: " << m_file
0147                 << std::endl;
0148     }
0149   };
0150 
0151   class TreeReaderBase {
0152   public:
0153     TreeReaderBase(MonitorElementData::Kind kind, MonitorElementData::Scope rescope)
0154         : m_kind(kind), m_rescope(rescope) {}
0155     virtual ~TreeReaderBase() {}
0156 
0157     MonitorElementData::Key makeKey(std::string const& fullname, int run, int lumi) {
0158       MonitorElementData::Key key;
0159       key.kind_ = m_kind;
0160       key.path_.set(fullname, MonitorElementData::Path::Type::DIR_AND_NAME);
0161       if (m_rescope == MonitorElementData::Scope::LUMI) {
0162         // no rescoping
0163         key.scope_ = lumi == 0 ? MonitorElementData::Scope::RUN : MonitorElementData::Scope::LUMI;
0164         key.id_ = edm::LuminosityBlockID(run, lumi);
0165       } else if (m_rescope == MonitorElementData::Scope::RUN) {
0166         // everything becomes run, we'll never see Scope::JOB inside DQMIO files.
0167         key.scope_ = MonitorElementData::Scope::RUN;
0168         key.id_ = edm::LuminosityBlockID(run, 0);
0169       } else if (m_rescope == MonitorElementData::Scope::JOB) {
0170         // Everything is aggregated over the entire job.
0171         key.scope_ = MonitorElementData::Scope::JOB;
0172         key.id_ = edm::LuminosityBlockID(0, 0);
0173       } else {
0174         assert(!"Invalid Scope in rescope option.");
0175       }
0176       return key;
0177     }
0178     virtual void read(ULong64_t iIndex, DQMStore* dqmstore, int run, int lumi) = 0;
0179     virtual void setTree(TTree* iTree) = 0;
0180 
0181   protected:
0182     MonitorElementData::Kind m_kind;
0183     MonitorElementData::Scope m_rescope;
0184   };
0185 
0186   template <class T>
0187   class TreeObjectReader : public TreeReaderBase {
0188   public:
0189     TreeObjectReader(MonitorElementData::Kind kind, MonitorElementData::Scope rescope) : TreeReaderBase(kind, rescope) {
0190       assert(m_kind != MonitorElementData::Kind::INT);
0191       assert(m_kind != MonitorElementData::Kind::REAL);
0192       assert(m_kind != MonitorElementData::Kind::STRING);
0193     }
0194 
0195     void read(ULong64_t iIndex, DQMStore* dqmstore, int run, int lumi) override {
0196       // This will populate the fields as defined in setTree method
0197       try {
0198         m_tree->GetEntry(iIndex);
0199 
0200         auto key = makeKey(*m_fullName, run, lumi);
0201         auto existing = dqmstore->findOrRecycle(key);
0202         if (existing) {
0203           // TODO: make sure there is sufficient locking here.
0204           DQMMergeHelper::mergeTogether(existing->getTH1(), m_buffer);
0205         } else {
0206           // We make our own MEs here, to avoid a round-trip through the booking API.
0207           MonitorElementData meData;
0208           meData.key_ = key;
0209           meData.value_.object_ = std::unique_ptr<T>((T*)(m_buffer->Clone()));
0210           auto me = new MonitorElement(std::move(meData));
0211           dqmstore->putME(me);
0212         }
0213       } catch (cms::Exception& iExcept) {
0214         using namespace std::string_literals;
0215         iExcept.addContext("failed while reading "s + *m_fullName);
0216         throw;
0217       }
0218     }
0219 
0220     void setTree(TTree* iTree) override {
0221       m_tree = iTree;
0222       m_tree->SetBranchAddress(kFullNameBranch, &m_fullName);
0223       m_tree->SetBranchAddress(kFlagBranch, &m_tag);
0224       m_tree->SetBranchAddress(kValueBranch, &m_buffer);
0225     }
0226 
0227   private:
0228     TTree* m_tree = nullptr;
0229     std::string* m_fullName = nullptr;
0230     T* m_buffer = nullptr;
0231     uint32_t m_tag = 0;
0232   };
0233 
0234   class TreeStringReader : public TreeReaderBase {
0235   public:
0236     TreeStringReader(MonitorElementData::Kind kind, MonitorElementData::Scope rescope) : TreeReaderBase(kind, rescope) {
0237       assert(m_kind == MonitorElementData::Kind::STRING);
0238     }
0239 
0240     void read(ULong64_t iIndex, DQMStore* dqmstore, int run, int lumi) override {
0241       // This will populate the fields as defined in setTree method
0242       m_tree->GetEntry(iIndex);
0243 
0244       auto key = makeKey(*m_fullName, run, lumi);
0245       auto existing = dqmstore->findOrRecycle(key);
0246 
0247       if (existing) {
0248         existing->Fill(*m_value);
0249       } else {
0250         // We make our own MEs here, to avoid a round-trip through the booking API.
0251         MonitorElementData meData;
0252         meData.key_ = key;
0253         meData.value_.scalar_.str = *m_value;
0254         auto me = new MonitorElement(std::move(meData));
0255         dqmstore->putME(me);
0256       }
0257     }
0258 
0259     void setTree(TTree* iTree) override {
0260       m_tree = iTree;
0261       m_tree->SetBranchAddress(kFullNameBranch, &m_fullName);
0262       m_tree->SetBranchAddress(kFlagBranch, &m_tag);
0263       m_tree->SetBranchAddress(kValueBranch, &m_value);
0264     }
0265 
0266   private:
0267     TTree* m_tree = nullptr;
0268     std::string* m_fullName = nullptr;
0269     std::string* m_value = nullptr;
0270     uint32_t m_tag = 0;
0271   };
0272 
0273   template <class T>
0274   class TreeSimpleReader : public TreeReaderBase {
0275   public:
0276     TreeSimpleReader(MonitorElementData::Kind kind, MonitorElementData::Scope rescope) : TreeReaderBase(kind, rescope) {
0277       assert(m_kind == MonitorElementData::Kind::INT || m_kind == MonitorElementData::Kind::REAL);
0278     }
0279 
0280     void read(ULong64_t iIndex, DQMStore* dqmstore, int run, int lumi) override {
0281       // This will populate the fields as defined in setTree method
0282       m_tree->GetEntry(iIndex);
0283 
0284       auto key = makeKey(*m_fullName, run, lumi);
0285       auto existing = dqmstore->findOrRecycle(key);
0286 
0287       if (existing) {
0288         existing->Fill(m_buffer);
0289       } else {
0290         // We make our own MEs here, to avoid a round-trip through the booking API.
0291         MonitorElementData meData;
0292         meData.key_ = key;
0293         if (m_kind == MonitorElementData::Kind::INT)
0294           meData.value_.scalar_.num = m_buffer;
0295         else if (m_kind == MonitorElementData::Kind::REAL)
0296           meData.value_.scalar_.real = m_buffer;
0297         auto me = new MonitorElement(std::move(meData));
0298         dqmstore->putME(me);
0299       }
0300     }
0301 
0302     void setTree(TTree* iTree) override {
0303       m_tree = iTree;
0304       m_tree->SetBranchAddress(kFullNameBranch, &m_fullName);
0305       m_tree->SetBranchAddress(kFlagBranch, &m_tag);
0306       m_tree->SetBranchAddress(kValueBranch, &m_buffer);
0307     }
0308 
0309   private:
0310     TTree* m_tree = nullptr;
0311     std::string* m_fullName = nullptr;
0312     T m_buffer = 0;
0313     uint32_t m_tag = 0;
0314   };
0315 };
0316 
0317 class DQMRootSource : public edm::PuttableSourceBase, DQMTTreeIO {
0318 public:
0319   DQMRootSource(edm::ParameterSet const&, const edm::InputSourceDescription&);
0320   DQMRootSource(const DQMRootSource&) = delete;
0321   ~DQMRootSource() override;
0322 
0323   // ---------- const member functions ---------------------
0324 
0325   const DQMRootSource& operator=(const DQMRootSource&) = delete;  // stop default
0326 
0327   // ---------- static member functions --------------------
0328 
0329   // ---------- member functions ---------------------------
0330   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0331 
0332 private:
0333   edm::InputSource::ItemType getNextItemType() override;
0334 
0335   std::shared_ptr<edm::FileBlock> readFile_() override;
0336   std::shared_ptr<edm::RunAuxiliary> readRunAuxiliary_() override;
0337   std::shared_ptr<edm::LuminosityBlockAuxiliary> readLuminosityBlockAuxiliary_() override;
0338   void readRun_(edm::RunPrincipal& rpCache) override;
0339   void readLuminosityBlock_(edm::LuminosityBlockPrincipal& lbCache) override;
0340   void readEvent_(edm::EventPrincipal&) override;
0341 
0342   // Read MEs from m_fileMetadatas to DQMStore  till run or lumi transition
0343   void readElements();
0344   // True if m_currentIndex points to an element that has a different
0345   // run or lumi than the previous element (a transition needs to happen).
0346   // False otherwise.
0347   bool isRunOrLumiTransition() const;
0348   void readNextItemType();
0349 
0350   // These methods will be called by the framework.
0351   // MEs in DQMStore  will be put to products.
0352   void beginRun(edm::Run& run) override;
0353   void beginLuminosityBlock(edm::LuminosityBlock& lumi) override;
0354 
0355   // If the run matches the filterOnRun configuration parameter, the run
0356   // (and all its lumis) will be kept.
0357   // Otherwise, check if a run and a lumi are in the range that needs to be processed.
0358   // Range is retrieved from lumisToProcess configuration parameter.
0359   // If at least one lumi of a run needs to be kept, per run MEs of that run will also be kept.
0360   bool keepIt(edm::RunNumber_t, edm::LuminosityBlockNumber_t) const;
0361   void logFileAction(char const* msg, char const* fileName) const;
0362 
0363   // ---------- member data --------------------------------
0364 
0365   // Properties from python config
0366   bool m_skipBadFiles;
0367   unsigned int m_filterOnRun;
0368   edm::InputFileCatalog m_catalog;
0369   std::vector<edm::LuminosityBlockRange> m_lumisToProcess;
0370   MonitorElementData::Scope m_rescope;
0371 
0372   edm::InputSource::ItemType m_nextItemType;
0373   // Each ME type gets its own reader
0374   std::vector<std::shared_ptr<TreeReaderBase>> m_treeReaders;
0375 
0376   // Index of currenlty processed row in m_fileMetadatas
0377   unsigned int m_currentIndex;
0378 
0379   // All open DQMIO files
0380   struct OpenFileInfo {
0381     OpenFileInfo(TFile* file, edm::JobReport::Token jrToken) : m_file(file), m_jrToken(jrToken) {}
0382     ~OpenFileInfo() {
0383       edm::Service<edm::JobReport> jr;
0384       jr->inputFileClosed(edm::InputType::Primary, m_jrToken);
0385     }
0386 
0387     OpenFileInfo(OpenFileInfo&&) = default;
0388     OpenFileInfo& operator=(OpenFileInfo&&) = default;
0389 
0390     std::unique_ptr<TFile> m_file;
0391     edm::JobReport::Token m_jrToken;
0392   };
0393   std::vector<OpenFileInfo> m_openFiles;
0394 
0395   // An item here is a row read from DQMIO indices (metadata) table
0396   std::vector<FileMetadata> m_fileMetadatas;
0397 };
0398 
0399 //
0400 // constants, enums and typedefs
0401 //
0402 
0403 //
0404 // static data member definitions
0405 //
0406 
0407 void DQMRootSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0408   edm::ParameterSetDescription desc;
0409   desc.addUntracked<std::vector<std::string>>("fileNames")->setComment("Names of files to be processed.");
0410   desc.addUntracked<unsigned int>("filterOnRun", 0)->setComment("Just limit the process to the selected run.");
0411   desc.addUntracked<std::string>("reScope", "JOB")
0412       ->setComment(
0413           "Accumulate histograms more coarsely."
0414           " Options: \"\": keep unchanged, \"RUN\": turn LUMI histograms into RUN histograms, \"JOB\": turn everything "
0415           "into JOB histograms.");
0416   desc.addUntracked<bool>("skipBadFiles", false)->setComment("Skip the file if it is not valid");
0417   desc.addUntracked<std::string>("overrideCatalog", std::string())
0418       ->setComment("An alternate file catalog to use instead of the standard site one.");
0419   std::vector<edm::LuminosityBlockRange> defaultLumis;
0420   desc.addUntracked<std::vector<edm::LuminosityBlockRange>>("lumisToProcess", defaultLumis)
0421       ->setComment("Skip any lumi inside the specified run:lumi range.");
0422 
0423   descriptions.addDefault(desc);
0424 }
0425 
0426 //
0427 // constructors and destructor
0428 //
0429 
0430 DQMRootSource::DQMRootSource(edm::ParameterSet const& iPSet, const edm::InputSourceDescription& iDesc)
0431     : edm::PuttableSourceBase(iPSet, iDesc),
0432       m_skipBadFiles(iPSet.getUntrackedParameter<bool>("skipBadFiles", false)),
0433       m_filterOnRun(iPSet.getUntrackedParameter<unsigned int>("filterOnRun", 0)),
0434       m_catalog(iPSet.getUntrackedParameter<std::vector<std::string>>("fileNames"),
0435                 iPSet.getUntrackedParameter<std::string>("overrideCatalog")),
0436       m_lumisToProcess(iPSet.getUntrackedParameter<std::vector<edm::LuminosityBlockRange>>(
0437           "lumisToProcess", std::vector<edm::LuminosityBlockRange>())),
0438       m_rescope(std::map<std::string, MonitorElementData::Scope>{
0439           {"", MonitorElementData::Scope::LUMI},
0440           {"LUMI", MonitorElementData::Scope::LUMI},
0441           {"RUN", MonitorElementData::Scope::RUN},
0442           {"JOB", MonitorElementData::Scope::JOB}}[iPSet.getUntrackedParameter<std::string>("reScope", "JOB")]),
0443       m_nextItemType(edm::InputSource::IsFile),
0444       m_treeReaders(kNIndicies, std::shared_ptr<TreeReaderBase>()),
0445       m_currentIndex(0),
0446       m_openFiles(std::vector<OpenFileInfo>()),
0447       m_fileMetadatas(std::vector<FileMetadata>()) {
0448   edm::sortAndRemoveOverlaps(m_lumisToProcess);
0449 
0450   if (m_catalog.fileNames(0).empty()) {
0451     m_nextItemType = edm::InputSource::IsStop;
0452   } else {
0453     m_treeReaders[kIntIndex].reset(new TreeSimpleReader<Long64_t>(MonitorElementData::Kind::INT, m_rescope));
0454     m_treeReaders[kFloatIndex].reset(new TreeSimpleReader<double>(MonitorElementData::Kind::REAL, m_rescope));
0455     m_treeReaders[kStringIndex].reset(new TreeStringReader(MonitorElementData::Kind::STRING, m_rescope));
0456     m_treeReaders[kTH1FIndex].reset(new TreeObjectReader<TH1F>(MonitorElementData::Kind::TH1F, m_rescope));
0457     m_treeReaders[kTH1SIndex].reset(new TreeObjectReader<TH1S>(MonitorElementData::Kind::TH1S, m_rescope));
0458     m_treeReaders[kTH1DIndex].reset(new TreeObjectReader<TH1D>(MonitorElementData::Kind::TH1D, m_rescope));
0459     m_treeReaders[kTH1IIndex].reset(new TreeObjectReader<TH1I>(MonitorElementData::Kind::TH1I, m_rescope));
0460     m_treeReaders[kTH2FIndex].reset(new TreeObjectReader<TH2F>(MonitorElementData::Kind::TH2F, m_rescope));
0461     m_treeReaders[kTH2SIndex].reset(new TreeObjectReader<TH2S>(MonitorElementData::Kind::TH2S, m_rescope));
0462     m_treeReaders[kTH2DIndex].reset(new TreeObjectReader<TH2D>(MonitorElementData::Kind::TH2D, m_rescope));
0463     m_treeReaders[kTH2IIndex].reset(new TreeObjectReader<TH2I>(MonitorElementData::Kind::TH2I, m_rescope));
0464     m_treeReaders[kTH3FIndex].reset(new TreeObjectReader<TH3F>(MonitorElementData::Kind::TH3F, m_rescope));
0465     m_treeReaders[kTProfileIndex].reset(new TreeObjectReader<TProfile>(MonitorElementData::Kind::TPROFILE, m_rescope));
0466     m_treeReaders[kTProfile2DIndex].reset(
0467         new TreeObjectReader<TProfile2D>(MonitorElementData::Kind::TPROFILE2D, m_rescope));
0468   }
0469 
0470   produces<DQMToken, edm::Transition::BeginRun>("DQMGenerationRecoRun");
0471   produces<DQMToken, edm::Transition::BeginLuminosityBlock>("DQMGenerationRecoLumi");
0472 }
0473 
0474 DQMRootSource::~DQMRootSource() {
0475   for (auto& file : m_openFiles) {
0476     if (file.m_file && file.m_file->IsOpen()) {
0477       logFileAction("Closed file", "");
0478     }
0479   }
0480 }
0481 
0482 //
0483 // member functions
0484 //
0485 
0486 edm::InputSource::ItemType DQMRootSource::getNextItemType() { return m_nextItemType; }
0487 
0488 // We will read the metadata of all files and fill m_fileMetadatas vector
0489 std::shared_ptr<edm::FileBlock> DQMRootSource::readFile_() {
0490   const int numFiles = m_catalog.fileNames(0).size();
0491   m_openFiles.reserve(numFiles);
0492 
0493   for (auto& fileitem : m_catalog.fileCatalogItems()) {
0494     TFile* file = nullptr;
0495     std::string pfn;
0496     std::string lfn;
0497     std::list<std::string> exInfo;
0498     //loop over names of a file, each of them corresponds to a data catalog
0499     bool isGoodFile(true);
0500     //get all names of a file, each of them corresponds to a data catalog
0501     const std::vector<std::string>& fNames = fileitem.fileNames();
0502     for (std::vector<std::string>::const_iterator it = fNames.begin(); it != fNames.end(); ++it) {
0503       // Try to open a file
0504       try {
0505         file = TFile::Open(it->c_str());
0506 
0507         // Exception will be trapped so we pull it out ourselves
0508         std::exception_ptr e = edm::threadLocalException::getException();
0509         if (e != std::exception_ptr()) {
0510           edm::threadLocalException::setException(std::exception_ptr());
0511           std::rethrow_exception(e);
0512         }
0513 
0514       } catch (cms::Exception const& e) {
0515         file = nullptr;                       // is there anything we need to free?
0516         if (std::next(it) == fNames.end()) {  //last name corresponding to the last data catalog to try
0517           if (!m_skipBadFiles) {
0518             edm::Exception ex(edm::errors::FileOpenError, "", e);
0519             ex.addContext("Opening DQM Root file");
0520             ex << "\nInput file " << *it << " was not found, could not be opened, or is corrupted.\n";
0521             //report previous exceptions when use other names to open file
0522             for (auto const& s : exInfo)
0523               ex.addAdditionalInfo(s);
0524             throw ex;
0525           }
0526           isGoodFile = false;
0527         }
0528         // save in case of error when trying next name
0529         for (auto const& s : e.additionalInfo())
0530           exInfo.push_back(s);
0531       }
0532 
0533       // Check if a file is usable
0534       if (file && !file->IsZombie()) {
0535         logFileAction("Successfully opened file ", it->c_str());
0536         pfn = *it;
0537         lfn = fileitem.logicalFileName();
0538         break;
0539       } else {
0540         if (std::next(it) == fNames.end()) {
0541           if (!m_skipBadFiles) {
0542             edm::Exception ex(edm::errors::FileOpenError);
0543             ex << "Input file " << *it << " could not be opened.\n";
0544             ex.addContext("Opening DQM Root file");
0545             //report previous exceptions when use other names to open file
0546             for (auto const& s : exInfo)
0547               ex.addAdditionalInfo(s);
0548             throw ex;
0549           }
0550           isGoodFile = false;
0551         }
0552         if (file) {
0553           delete file;
0554           file = nullptr;
0555         }
0556       }
0557     }  //end loop over names of the file
0558 
0559     if (!file || (!isGoodFile && m_skipBadFiles))
0560       continue;
0561 
0562     std::unique_ptr<std::string> guid{file->Get<std::string>(kCmsGuid)};
0563     if (not guid) {
0564       guid = std::make_unique<std::string>(file->GetUUID().AsString());
0565       std::transform(guid->begin(), guid->end(), guid->begin(), (int (*)(int))std::toupper);
0566     }
0567 
0568     edm::Service<edm::JobReport> jr;
0569     auto jrToken = jr->inputFileOpened(
0570         pfn, lfn, std::string(), std::string(), "DQMRootSource", "source", *guid, std::vector<std::string>());
0571     m_openFiles.emplace_back(file, jrToken);
0572 
0573     // Check file format version, which is encoded in the Title of the TFile
0574     if (strcmp(file->GetTitle(), "1") != 0) {
0575       edm::Exception ex(edm::errors::FileReadError);
0576       ex << "Input file " << fNames[0] << " does not appear to be a DQM Root file.\n";
0577     }
0578 
0579     // Read metadata from the file
0580     TTree* indicesTree = dynamic_cast<TTree*>(file->Get(kIndicesTree));
0581     assert(indicesTree != nullptr);
0582 
0583     FileMetadata temp;
0584     // Each line of metadata will be read into the coresponding fields of temp.
0585     indicesTree->SetBranchAddress(kRunBranch, &temp.m_run);
0586     indicesTree->SetBranchAddress(kLumiBranch, &temp.m_lumi);
0587     indicesTree->SetBranchAddress(kBeginTimeBranch, &temp.m_beginTime);
0588     indicesTree->SetBranchAddress(kEndTimeBranch, &temp.m_endTime);
0589     indicesTree->SetBranchAddress(kTypeBranch, &temp.m_type);
0590     indicesTree->SetBranchAddress(kFirstIndex, &temp.m_firstIndex);
0591     indicesTree->SetBranchAddress(kLastIndex, &temp.m_lastIndex);
0592 
0593     for (Long64_t index = 0; index != indicesTree->GetEntries(); ++index) {
0594       indicesTree->GetEntry(index);
0595       temp.m_file = file;
0596 
0597       if (keepIt(temp.m_run, temp.m_lumi)) {
0598         m_fileMetadatas.push_back(temp);
0599       }
0600     }
0601 
0602   }  //end loop over files
0603 
0604   // Sort to make sure runs and lumis appear in sequential order
0605   std::stable_sort(m_fileMetadatas.begin(), m_fileMetadatas.end());
0606 
0607   // If we have lumisections without matching runs, insert dummy runs here.
0608   unsigned int run = 0;
0609   auto toadd = std::vector<FileMetadata>();
0610   for (auto& metadata : m_fileMetadatas) {
0611     if (run < metadata.m_run && metadata.m_lumi != 0) {
0612       // run transition and lumi transition at the same time!
0613       FileMetadata dummy{};  // zero initialize
0614       dummy.m_run = metadata.m_run;
0615       dummy.m_lumi = 0;
0616       dummy.m_type = kNoTypesStored;
0617       toadd.push_back(dummy);
0618     }
0619     run = metadata.m_run;
0620   }
0621 
0622   if (!toadd.empty()) {
0623     // rather than trying to insert at the right places, just append and sort again.
0624     m_fileMetadatas.insert(m_fileMetadatas.end(), toadd.begin(), toadd.end());
0625     std::stable_sort(m_fileMetadatas.begin(), m_fileMetadatas.end());
0626   }
0627 
0628   //for (auto& metadata : m_fileMetadatas)
0629   //  metadata.describe();
0630 
0631   // Stop if there's nothing to process. Otherwise start the run.
0632   if (m_fileMetadatas.empty())
0633     m_nextItemType = edm::InputSource::IsStop;
0634   else
0635     m_nextItemType = edm::InputSource::IsRun;
0636 
0637   // We have to return something but not sure why
0638   return std::make_shared<edm::FileBlock>();
0639 }
0640 
0641 std::shared_ptr<edm::RunAuxiliary> DQMRootSource::readRunAuxiliary_() {
0642   FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0643   auto runAux =
0644       edm::RunAuxiliary(metadata.m_run, edm::Timestamp(metadata.m_beginTime), edm::Timestamp(metadata.m_endTime));
0645   return std::make_shared<edm::RunAuxiliary>(runAux);
0646 }
0647 
0648 std::shared_ptr<edm::LuminosityBlockAuxiliary> DQMRootSource::readLuminosityBlockAuxiliary_() {
0649   FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0650   auto lumiAux = edm::LuminosityBlockAuxiliary(edm::LuminosityBlockID(metadata.m_run, metadata.m_lumi),
0651                                                edm::Timestamp(metadata.m_beginTime),
0652                                                edm::Timestamp(metadata.m_endTime));
0653   return std::make_shared<edm::LuminosityBlockAuxiliary>(lumiAux);
0654 }
0655 
0656 void DQMRootSource::readRun_(edm::RunPrincipal& rpCache) {
0657   // Read elements of a current run.
0658   do {
0659     FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0660     if (metadata.m_lumi == 0) {
0661       readElements();
0662     }
0663     m_currentIndex++;
0664   } while (!isRunOrLumiTransition());
0665 
0666   readNextItemType();
0667 
0668   edm::Service<edm::JobReport> jr;
0669   jr->reportInputRunNumber(rpCache.id().run());
0670   rpCache.fillRunPrincipal(processHistoryRegistryForUpdate());
0671 }
0672 
0673 void DQMRootSource::readLuminosityBlock_(edm::LuminosityBlockPrincipal& lbCache) {
0674   // Read elements of a current lumi.
0675   do {
0676     readElements();
0677     m_currentIndex++;
0678   } while (!isRunOrLumiTransition());
0679 
0680   readNextItemType();
0681 
0682   edm::Service<edm::JobReport> jr;
0683   jr->reportInputLumiSection(lbCache.id().run(), lbCache.id().luminosityBlock());
0684   lbCache.fillLuminosityBlockPrincipal(processHistoryRegistry().getMapped(lbCache.aux().processHistoryID()));
0685 }
0686 
0687 void DQMRootSource::readEvent_(edm::EventPrincipal&) {}
0688 
0689 void DQMRootSource::readElements() {
0690   FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0691 
0692   if (metadata.m_type != kNoTypesStored) {
0693     std::shared_ptr<TreeReaderBase> reader = m_treeReaders[metadata.m_type];
0694     TTree* tree = dynamic_cast<TTree*>(metadata.m_file->Get(kTypeNames[metadata.m_type]));
0695     // The Reset() below screws up the tree, so we need to re-read it from file
0696     // before use here.
0697     tree->Refresh();
0698 
0699     reader->setTree(tree);
0700 
0701     ULong64_t index = metadata.m_firstIndex;
0702     ULong64_t endIndex = metadata.m_lastIndex + 1;
0703 
0704     for (; index != endIndex; ++index) {
0705       reader->read(index, edm::Service<DQMStore>().operator->(), metadata.m_run, metadata.m_lumi);
0706     }
0707     // Drop buffers in the TTree. This reduces memory consuption while the tree
0708     // just sits there and waits for the next block to be read.
0709     tree->Reset();
0710   }
0711 }
0712 
0713 bool DQMRootSource::isRunOrLumiTransition() const {
0714   if (m_currentIndex == 0) {
0715     return false;
0716   }
0717 
0718   if (m_currentIndex > m_fileMetadatas.size() - 1) {
0719     // We reached the end
0720     return true;
0721   }
0722 
0723   FileMetadata previousMetadata = m_fileMetadatas[m_currentIndex - 1];
0724   FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0725 
0726   return previousMetadata.m_run != metadata.m_run || previousMetadata.m_lumi != metadata.m_lumi;
0727 }
0728 
0729 void DQMRootSource::readNextItemType() {
0730   if (m_currentIndex == 0) {
0731     m_nextItemType = edm::InputSource::IsRun;
0732   } else if (m_currentIndex > m_fileMetadatas.size() - 1) {
0733     // We reached the end
0734     m_nextItemType = edm::InputSource::IsStop;
0735   } else {
0736     FileMetadata previousMetadata = m_fileMetadatas[m_currentIndex - 1];
0737     FileMetadata metadata = m_fileMetadatas[m_currentIndex];
0738 
0739     if (previousMetadata.m_run != metadata.m_run) {
0740       m_nextItemType = edm::InputSource::IsRun;
0741     } else if (previousMetadata.m_lumi != metadata.m_lumi) {
0742       m_nextItemType = edm::InputSource::IsLumi;
0743     }
0744   }
0745 }
0746 
0747 void DQMRootSource::beginRun(edm::Run& run) {
0748   std::unique_ptr<DQMToken> product = std::make_unique<DQMToken>();
0749   run.put(std::move(product), "DQMGenerationRecoRun");
0750 }
0751 
0752 void DQMRootSource::beginLuminosityBlock(edm::LuminosityBlock& lumi) {
0753   std::unique_ptr<DQMToken> product = std::make_unique<DQMToken>();
0754   lumi.put(std::move(product), "DQMGenerationRecoLumi");
0755 }
0756 
0757 bool DQMRootSource::keepIt(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi) const {
0758   if (m_filterOnRun != 0 && run != m_filterOnRun) {
0759     return false;
0760   }
0761 
0762   if (m_lumisToProcess.empty()) {
0763     return true;
0764   }
0765 
0766   for (edm::LuminosityBlockRange const& lumiToProcess : m_lumisToProcess) {
0767     if (run >= lumiToProcess.startRun() && run <= lumiToProcess.endRun()) {
0768       if (lumi >= lumiToProcess.startLumi() && lumi <= lumiToProcess.endLumi()) {
0769         return true;
0770       } else if (lumi == 0) {
0771         return true;
0772       }
0773     }
0774   }
0775   return false;
0776 }
0777 
0778 void DQMRootSource::logFileAction(char const* msg, char const* fileName) const {
0779   edm::LogAbsolute("fileAction") << std::setprecision(0) << edm::TimeOfDay() << msg << fileName;
0780   edm::FlushMessageLog();
0781 }
0782 
0783 //
0784 // const member functions
0785 //
0786 
0787 //
0788 // static member functions
0789 //
0790 DEFINE_FWK_INPUT_SOURCE(DQMRootSource);