CMS 3D CMS Logo

DQMHistNormalizer.cc
Go to the documentation of this file.
1 
8 // framework & common header files
15 
16 //DQM services
19 
20 //Regexp handling
21 #include <regex>
22 #include <string>
23 #include <vector>
24 #include <map>
25 
26 using namespace std;
27 
28 namespace {
29  // Three implementations were tested: char-by-char (this version),
30  // using std::string::find + std::string::replace and std::regex_replace.
31  // First one takes ~60 ns per iteration, second one ~85 ns,
32  // and the regex implementation takes nearly 1 us
33  std::string globToRegex(const std::string& s) {
35  out.reserve(s.size());
36  for (auto ch : s) {
37  if (ch == '*') {
38  out.push_back('.');
39  }
40  out.push_back(ch);
41  }
42  return out;
43  }
44 } // namespace
45 
46 class DQMHistNormalizer : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns> {
47 public:
50 
51  explicit DQMHistNormalizer(const edm::ParameterSet&);
52  ~DQMHistNormalizer() override;
53  void analyze(const edm::Event&, const edm::EventSetup&) override;
54  void beginRun(const edm::Run& r, const edm::EventSetup& c) override {}
55  void endRun(const edm::Run& r, const edm::EventSetup& c) override;
56 
57 private:
58  vector<string> plotNamesToNormalize_; //root name used by all the plots that must be normalized
59  string reference_;
60 };
61 
63  : plotNamesToNormalize_(cfg.getParameter<std::vector<string> >("plotNamesToNormalize")),
64  reference_(cfg.getParameter<string>("reference")) {
65  usesResource("DQMStore");
66  //std::cout << "<DQMHistNormalizer::DQMHistNormalizer>:" << std::endl;
67 }
68 
70  //--- nothing to be done yet
71 }
72 
74  //--- nothing to be done yet
75 }
76 
78  //std::cout << "<DQMHistNormalizer::endJob>:" << std::endl;
79 
80  //--- check that DQMStore service is available
81  if (!edm::Service<DQMStore>().isAvailable()) {
82  edm::LogError("endJob") << " Failed to access dqmStore --> histograms will NOT be plotted !!";
83  return;
84  }
85 
87 
88  vector<MonitorElement*> allOurMEs = dqmStore.getAllContents("RecoTauV/");
89  std::regex refregex = std::regex(".*RecoTauV/.*/" + globToRegex(reference_), std::regex::nosubs);
90  vector<std::regex> toNormRegex;
91  for (std::vector<string>::const_iterator toNorm = plotNamesToNormalize_.begin();
92  toNorm != plotNamesToNormalize_.end();
93  ++toNorm)
94  toNormRegex.emplace_back(".*RecoTauV/.*/" + globToRegex(*toNorm), std::regex::nosubs);
95 
96  map<string, MonitorElement*> refsMap;
97  vector<MonitorElement*> toNormElements;
98  std::smatch path_match;
99 
100  for (vector<MonitorElement*>::const_iterator element = allOurMEs.begin(); element != allOurMEs.end(); ++element) {
101  string pathname = (*element)->getFullname();
102  //cout << pathname << endl;
103  //Matches reference
104  if (std::regex_match(pathname, path_match, refregex)) {
105  //cout << "Matched to ref" << endl;
106  string dir = pathname.substr(0, pathname.rfind('/'));
107  if (refsMap.find(dir) != refsMap.end()) {
108  edm::LogInfo("DQMHistNormalizer")
109  << "DQMHistNormalizer::endRun: Warning! found multiple normalizing references for dir: " << dir << "!";
110  edm::LogInfo("DQMHistNormalizer") << " " << (refsMap[dir])->getFullname();
111  edm::LogInfo("DQMHistNormalizer") << " " << pathname;
112  } else {
113  refsMap[dir] = *element;
114  }
115  }
116 
117  //Matches targets
118  for (const auto& reg : toNormRegex) {
119  if (std::regex_match(pathname, path_match, reg)) {
120  //cout << "Matched to target" << endl;
121  toNormElements.push_back(*element);
122  //cout << "Filled the collection" << endl;
123  }
124  }
125  }
126 
127  for (vector<MonitorElement*>::const_iterator matchingElement = toNormElements.begin();
128  matchingElement != toNormElements.end();
129  ++matchingElement) {
130  string meName = (*matchingElement)->getFullname();
131  string dir = meName.substr(0, meName.rfind('/'));
132 
133  if (refsMap.find(dir) == refsMap.end()) {
134  edm::LogInfo("DQMHistNormalizer") << "DQMHistNormalizer::endRun: Error! normalizing references for " << meName
135  << " not found! Skipping...";
136  continue;
137  }
138 
139  float norm = refsMap[dir]->getTH1()->GetEntries();
140  TH1* hist = (*matchingElement)->getTH1();
141  if (norm != 0.) {
142  if (!hist->GetSumw2N())
143  hist->Sumw2();
144  hist->Scale(1 / norm); //use option "width" to divide the bin contents and errors by the bin width?
145  } else {
146  edm::LogInfo("DQMHistNormalizer") << "DQMHistNormalizer::endRun: Error! Normalization failed in "
147  << hist->GetTitle() << "!";
148  }
149 
150  } // for(vector<MonitorElement *>::const_iterator matchingElement = matchingElemts.begin(); matchingElement = matchingElemts.end(); ++matchingElement)
151 }
152 
~DQMHistNormalizer() override
void endRun(const edm::Run &r, const edm::EventSetup &c) override
vector< string > plotNamesToNormalize_
Log< level::Error, false > LogError
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Info, false > LogInfo
void analyze(const edm::Event &, const edm::EventSetup &) override
DQMHistNormalizer(const edm::ParameterSet &)
dqm::legacy::MonitorElement MonitorElement
dqm::legacy::DQMStore DQMStore
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: Run.h:45