CMS 3D CMS Logo

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