Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:04

0001 #include "BrilClient.h"
0002 
0003 #include <boost/property_tree/json_parser.hpp>
0004 #include <boost/property_tree/ptree.hpp>
0005 
0006 #include <filesystem>
0007 #include <iostream>
0008 #include <vector>
0009 
0010 #include "TH2F.h"
0011 
0012 BrilClient::BrilClient(const edm::ParameterSet &ps) {
0013   pathToken_ = consumes<std::string, edm::InLumi>(edm::InputTag("source", "sourceDataPath"));
0014   jsonToken_ = consumes<std::string, edm::InLumi>(edm::InputTag("source", "sourceJsonPath"));
0015 }
0016 
0017 BrilClient::~BrilClient() {}
0018 
0019 using boost::property_tree::ptree;
0020 
0021 template <typename T>
0022 std::vector<T> as_vector(ptree const &pt, ptree::key_type const &key) {
0023   std::vector<T> r;
0024   for (auto &item : pt.get_child(key))
0025     r.push_back(item.second.get_value<T>());
0026   return r;
0027 }
0028 
0029 void BrilClient::dqmEndLuminosityBlock(DQMStore::IBooker &ibooker_,
0030                                        DQMStore::IGetter &igetter_,
0031                                        edm::LuminosityBlock const &lb,
0032                                        edm::EventSetup const &) {
0033   edm::Handle<std::string> filePath_;
0034   lb.getByToken(pathToken_, filePath_);
0035 
0036   // edm::Handle<std::string> jsonPath_;
0037   // lb.getByToken(jsonToken_, jsonPath_);
0038   //
0039 
0040   ptree json;
0041   if (!std::filesystem::exists(*filePath_)) {
0042     edm::LogWarning("BrilClient") << "BrilClient"
0043                                   << " File missing: " << *filePath_ << std::endl;
0044 
0045     return;
0046   } else {
0047     edm::LogWarning("BrilClient") << "BrilClient"
0048                                   << " Opening: " << *filePath_ << std::endl;
0049 
0050     read_json(std::string(*filePath_), json);
0051   }
0052 
0053   // Parse the json
0054   for (auto &mainTree : json.get_child("OccupancyPlots")) {
0055     std::string title = mainTree.second.get<std::string>("titles");
0056     std::size_t pos = title.find(',');
0057     if (pos <= 0) {
0058       edm::LogWarning("BrilClient") << "BrilClient::dqmEndLuminosityBlock"
0059                                     << " Invalid title" << title << std::endl;
0060 
0061       continue;
0062     }
0063     std::string name = title.substr(0, pos);
0064 
0065     auto nBins = as_vector<int>(mainTree.second, "nbins");    // x, y
0066     auto xrange = as_vector<int>(mainTree.second, "xrange");  // min, max
0067     auto yrange = as_vector<int>(mainTree.second, "yrange");  // min, max
0068 
0069     TH2F *th = new TH2F(
0070         name.c_str(), title.c_str(), nBins.at(0), xrange.at(0), xrange.at(1), nBins.at(1), yrange.at(0), yrange.at(1));
0071 
0072     for (auto &dataArray : mainTree.second.get_child("data")) {
0073       int elements[3] = {0, 0, 0};  // binX, binY, binCont;
0074       auto element = std::begin(elements);
0075 
0076       for (auto &binContent : dataArray.second) {
0077         *element++ = stoi(binContent.second.get_value<std::string>());
0078         if (element == std::end(elements))
0079           break;
0080       }
0081 
0082       th->SetBinContent(elements[0], elements[1], elements[2]);
0083     }
0084 
0085     // Add it to the DQM store
0086     ibooker_.setCurrentFolder("BRIL/OccupancyPlots");
0087     igetter_.setCurrentFolder("BRIL/OccupancyPlots");
0088 
0089     MonitorElement *m = igetter_.get(name);
0090     if (m == nullptr) {
0091       m = ibooker_.book2D(name, th);
0092     } else {
0093       m->getTH1F()->Add(th);
0094     }
0095   }
0096 }
0097 
0098 #include "FWCore/Framework/interface/MakerMacros.h"
0099 DEFINE_FWK_MODULE(BrilClient);