Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:13:55

0001 #include <filesystem>
0002 
0003 #include <boost/algorithm/string.hpp>
0004 #include <boost/property_tree/json_parser.hpp>
0005 #include <boost/property_tree/ptree.hpp>
0006 #include <fmt/printf.h>
0007 
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 
0012 namespace dqmservices {
0013   class DQMStreamerWriteJsonAnalyzer : public edm::one::EDAnalyzer<> {
0014   public:
0015     DQMStreamerWriteJsonAnalyzer(edm::ParameterSet const& iPSet);
0016 
0017     void analyze(edm::Event const&, edm::EventSetup const&) override;
0018 
0019     static void fillDescriptions(edm::ConfigurationDescriptions& iDesc);
0020 
0021   private:
0022     void writeJson() const;
0023     void writeEndJob() const;
0024 
0025     std::filesystem::path writePath_;
0026     unsigned int const eventsPerLumi_;
0027     unsigned int const runNumber_;
0028     std::string const streamName_;
0029     std::vector<std::string> const dataFileForEachLumi_;
0030     unsigned int nEventsSeenSinceWrite_;
0031     unsigned int fileIndex_;
0032   };
0033 
0034   DQMStreamerWriteJsonAnalyzer::DQMStreamerWriteJsonAnalyzer(edm::ParameterSet const& iPSet)
0035       : eventsPerLumi_(iPSet.getUntrackedParameter<unsigned int>("eventsPerLumi")),
0036         runNumber_(iPSet.getUntrackedParameter<unsigned int>("runNumber")),
0037         streamName_(iPSet.getUntrackedParameter<std::string>("streamName")),
0038         dataFileForEachLumi_(iPSet.getUntrackedParameter<std::vector<std::string>>("dataFileForEachLumi")),
0039         nEventsSeenSinceWrite_{0},
0040         fileIndex_{0} {
0041     std::filesystem::path path = iPSet.getUntrackedParameter<std::string>("pathToWriteJson");
0042     writePath_ /= fmt::sprintf("run%06d", runNumber_);
0043 
0044     std::filesystem::create_directories(writePath_);
0045   }
0046 
0047   void DQMStreamerWriteJsonAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& iDesc) {
0048     edm::ParameterSetDescription pset;
0049     pset.addUntracked<unsigned int>("eventsPerLumi");
0050     pset.addUntracked<unsigned int>("runNumber");
0051     pset.addUntracked<std::string>("streamName");
0052     pset.addUntracked<std::string>("pathToWriteJson");
0053     pset.addUntracked<std::vector<std::string>>("dataFileForEachLumi");
0054 
0055     iDesc.addDefault(pset);
0056   }
0057 
0058   void DQMStreamerWriteJsonAnalyzer::analyze(edm::Event const&, edm::EventSetup const&) {
0059     if (++nEventsSeenSinceWrite_ >= eventsPerLumi_) {
0060       if (fileIndex_ == dataFileForEachLumi_.size()) {
0061         writeEndJob();
0062         return;
0063       }
0064       writeJson();
0065       ++fileIndex_;
0066       nEventsSeenSinceWrite_ = 0;
0067       return;
0068     }
0069   }
0070 
0071   void DQMStreamerWriteJsonAnalyzer::writeJson() const {
0072     auto currentFileBase = fmt::sprintf("run%06d_ls%04d_%s_local.jsn", runNumber_, fileIndex_ + 2, streamName_);
0073     auto currentJsonPath = (writePath_ / currentFileBase).string();
0074 
0075     using namespace boost::property_tree;
0076     ptree pt;
0077     ptree data;
0078 
0079     ptree child1, child2, child3;
0080     child1.put("", nEventsSeenSinceWrite_);  // Processed
0081     child2.put("", nEventsSeenSinceWrite_);  // Accepted
0082     child3.put("", dataFileForEachLumi_[fileIndex_]);
0083 
0084     data.push_back(std::make_pair("", child1));
0085     data.push_back(std::make_pair("", child2));
0086     data.push_back(std::make_pair("", child3));
0087 
0088     pt.add_child("data", data);
0089     pt.put("definition", "");
0090     pt.put("source", "");
0091 
0092     std::string json_tmp = currentJsonPath + ".open";
0093     write_json(json_tmp, pt);
0094     ::rename(json_tmp.c_str(), currentJsonPath.c_str());
0095   }
0096 
0097   void DQMStreamerWriteJsonAnalyzer::writeEndJob() const {
0098     auto currentFileBase = fmt::sprintf("run%06d_ls%04d_EoR.jsn", runNumber_, 0);
0099     auto currentJsonPath = (writePath_ / currentFileBase).string();
0100 
0101     using namespace boost::property_tree;
0102     ptree pt;
0103     std::string json_tmp = currentJsonPath + ".open";
0104     write_json(json_tmp, pt);
0105     ::rename(json_tmp.c_str(), currentJsonPath.c_str());
0106   }
0107 
0108 }  // namespace dqmservices
0109 
0110 typedef dqmservices::DQMStreamerWriteJsonAnalyzer DQMStreamerWriteJsonAnalyzer;
0111 DEFINE_FWK_MODULE(DQMStreamerWriteJsonAnalyzer);