CMS 3D CMS Logo

BrilClient.cc
Go to the documentation of this file.
1 #include "BrilClient.h"
2 
3 #include <boost/property_tree/json_parser.hpp>
4 #include <boost/property_tree/ptree.hpp>
5 
6 #include <filesystem>
7 #include <iostream>
8 #include <vector>
9 
10 #include "TH2F.h"
11 
13  pathToken_ = consumes<std::string, edm::InLumi>(edm::InputTag("source", "sourceDataPath"));
14  jsonToken_ = consumes<std::string, edm::InLumi>(edm::InputTag("source", "sourceJsonPath"));
15 }
16 
18 
19 using boost::property_tree::ptree;
20 
21 template <typename T>
22 std::vector<T> as_vector(ptree const &pt, ptree::key_type const &key) {
23  std::vector<T> r;
24  for (auto &item : pt.get_child(key))
25  r.push_back(item.second.get_value<T>());
26  return r;
27 }
28 
30  DQMStore::IGetter &igetter_,
31  edm::LuminosityBlock const &lb,
32  edm::EventSetup const &) {
33  edm::Handle<std::string> filePath_;
34  lb.getByToken(pathToken_, filePath_);
35 
36  // edm::Handle<std::string> jsonPath_;
37  // lb.getByToken(jsonToken_, jsonPath_);
38  //
39 
40  ptree json;
41  if (!std::filesystem::exists(*filePath_)) {
42  edm::LogWarning("BrilClient") << "BrilClient"
43  << " File missing: " << *filePath_ << std::endl;
44 
45  return;
46  } else {
47  edm::LogWarning("BrilClient") << "BrilClient"
48  << " Opening: " << *filePath_ << std::endl;
49 
50  read_json(std::string(*filePath_), json);
51  }
52 
53  // Parse the json
54  for (auto &mainTree : json.get_child("OccupancyPlots")) {
55  std::string title = mainTree.second.get<std::string>("titles");
56  std::size_t pos = title.find(',');
57  if (pos <= 0) {
58  edm::LogWarning("BrilClient") << "BrilClient::dqmEndLuminosityBlock"
59  << " Invalid title" << title << std::endl;
60 
61  continue;
62  }
63  std::string name = title.substr(0, pos);
64 
65  auto nBins = as_vector<int>(mainTree.second, "nbins"); // x, y
66  auto xrange = as_vector<int>(mainTree.second, "xrange"); // min, max
67  auto yrange = as_vector<int>(mainTree.second, "yrange"); // min, max
68 
69  TH2F *th = new TH2F(
70  name.c_str(), title.c_str(), nBins.at(0), xrange.at(0), xrange.at(1), nBins.at(1), yrange.at(0), yrange.at(1));
71 
72  for (auto &dataArray : mainTree.second.get_child("data")) {
73  int elements[3] = {0, 0, 0}; // binX, binY, binCont;
74  auto element = std::begin(elements);
75 
76  for (auto &binContent : dataArray.second) {
77  *element++ = stoi(binContent.second.get_value<std::string>());
78  if (element == std::end(elements))
79  break;
80  }
81 
82  th->SetBinContent(elements[0], elements[1], elements[2]);
83  }
84 
85  // Add it to the DQM store
86  ibooker_.setCurrentFolder("BRIL/OccupancyPlots");
87  igetter_.setCurrentFolder("BRIL/OccupancyPlots");
88 
89  MonitorElement *m = igetter_.get(name);
90  if (m == nullptr) {
91  m = ibooker_.book2D(name, th);
92  } else {
93  m->getTH1F()->Add(th);
94  }
95  }
96 }
97 
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
std::vector< T > as_vector(ptree const &pt, ptree::key_type const &key)
Definition: BrilClient.cc:22
nlohmann::json json
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~BrilClient() override
Definition: BrilClient.cc:17
edm::EDGetTokenT< std::string > pathToken_
Definition: BrilClient.h:28
void dqmEndLuminosityBlock(DQMStore::IBooker &, DQMStore::IGetter &, edm::LuminosityBlock const &, edm::EventSetup const &) override
Definition: BrilClient.cc:29
BrilClient(const edm::ParameterSet &ps)
Definition: BrilClient.cc:12
bool getByToken(EDGetToken token, Handle< PROD > &result) const
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:212
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:680
Log< level::Warning, false > LogWarning
long double T
edm::EDGetTokenT< std::string > jsonToken_
Definition: BrilClient.h:32