CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "classlib/utils/RegexpMatch.h"
22 #include "classlib/utils/Regexp.h"
23 
24 #include <string>
25 #include <vector>
26 #include <map>
27 
28 using namespace std;
29 
31 public:
34 
35  explicit DQMHistNormalizer(const edm::ParameterSet&);
36  ~DQMHistNormalizer() override;
37  void analyze(const edm::Event&, const edm::EventSetup&) override;
38  void endRun(const edm::Run& r, const edm::EventSetup& c) override;
39 
40 private:
41  lat::Regexp* buildRegex(const string& expr);
42  vector<string> plotNamesToNormalize_; //root name used by all the plots that must be normalized
43  string reference_;
44 };
45 
47  : plotNamesToNormalize_(cfg.getParameter<std::vector<string> >("plotNamesToNormalize")),
48  reference_(cfg.getParameter<string>("reference")) {
49  //std::cout << "<DQMHistNormalizer::DQMHistNormalizer>:" << std::endl;
50 }
51 
53  //--- nothing to be done yet
54 }
55 
57  //--- nothing to be done yet
58 }
59 
60 lat::Regexp* DQMHistNormalizer::buildRegex(const string& expr) {
61  lat::Regexp* rx = nullptr;
62  try {
63  rx = new lat::Regexp(expr, 0, lat::Regexp::Wildcard);
64  rx->study();
65  } catch (lat::Error& e) {
66  throw cms::Exception("DQMHistNormalizer")
67  << "Invalid regular expression '" << expr.c_str() << "':" << e.explain().c_str();
68  }
69  return rx;
70 }
71 
73  //std::cout << "<DQMHistNormalizer::endJob>:" << std::endl;
74 
75  //--- check that DQMStore service is available
76  if (!edm::Service<DQMStore>().isAvailable()) {
77  edm::LogError("endJob") << " Failed to access dqmStore --> histograms will NOT be plotted !!";
78  return;
79  }
80 
81  DQMStore& dqmStore = (*edm::Service<DQMStore>());
82 
83  vector<MonitorElement*> allOurMEs = dqmStore.getAllContents("RecoTauV/");
84  lat::Regexp* refregex = buildRegex("*RecoTauV/*/" + reference_);
85  vector<lat::Regexp*> toNormRegex;
86  for (std::vector<string>::const_iterator toNorm = plotNamesToNormalize_.begin();
87  toNorm != plotNamesToNormalize_.end();
88  ++toNorm)
89  toNormRegex.push_back(buildRegex("*RecoTauV/*/" + *toNorm));
90 
91  map<string, MonitorElement*> refsMap;
92  vector<MonitorElement*> toNormElements;
93 
94  for (vector<MonitorElement*>::const_iterator element = allOurMEs.begin(); element != allOurMEs.end(); ++element) {
95  string pathname = (*element)->getFullname();
96  //cout << pathname << endl;
97  //Matches reference
98  if (refregex->match(pathname)) {
99  //cout << "Matched to ref" << endl;
100  string dir = pathname.substr(0, pathname.rfind('/'));
101  if (refsMap.find(dir) != refsMap.end()) {
102  edm::LogInfo("DQMHistNormalizer")
103  << "DQMHistNormalizer::endRun: Warning! found multiple normalizing references for dir: " << dir << "!";
104  edm::LogInfo("DQMHistNormalizer") << " " << (refsMap[dir])->getFullname();
105  edm::LogInfo("DQMHistNormalizer") << " " << pathname;
106  } else {
107  refsMap[dir] = *element;
108  }
109  }
110 
111  //Matches targets
112  for (vector<lat::Regexp*>::const_iterator reg = toNormRegex.begin(); reg != toNormRegex.end(); ++reg) {
113  if ((*reg)->match(pathname)) {
114  //cout << "Matched to target" << endl;
115  toNormElements.push_back(*element);
116  //cout << "Filled the collection" << endl;
117  }
118  }
119  }
120 
121  delete refregex;
122  for (vector<lat::Regexp*>::const_iterator reg = toNormRegex.begin(); reg != toNormRegex.end(); ++reg)
123  delete *reg;
124 
125  for (vector<MonitorElement*>::const_iterator matchingElement = toNormElements.begin();
126  matchingElement != toNormElements.end();
127  ++matchingElement) {
128  string meName = (*matchingElement)->getFullname();
129  string dir = meName.substr(0, meName.rfind('/'));
130 
131  if (refsMap.find(dir) == refsMap.end()) {
132  edm::LogInfo("DQMHistNormalizer") << "DQMHistNormalizer::endRun: Error! normalizing references for " << meName
133  << " not found! Skipping...";
134  continue;
135  }
136 
137  float norm = refsMap[dir]->getTH1()->GetEntries();
138  TH1* hist = (*matchingElement)->getTH1();
139  if (norm != 0.) {
140  if (!hist->GetSumw2N())
141  hist->Sumw2();
142  hist->Scale(1 / norm); //use option "width" to divide the bin contents and errors by the bin width?
143  } else {
144  edm::LogInfo("DQMHistNormalizer") << "DQMHistNormalizer::endRun: Error! Normalization failed in "
145  << hist->GetTitle() << "!";
146  }
147 
148  } // for(vector<MonitorElement *>::const_iterator matchingElement = matchingElemts.begin(); matchingElement = matchingElemts.end(); ++matchingElement)
149 }
150 
edm::ErrorSummaryEntry Error
const edm::EventSetup & c
tuple cfg
Definition: looper.py:296
~DQMHistNormalizer() override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
virtual std::vector< dqm::harvesting::MonitorElement * > getAllContents(std::string const &path) const
Definition: DQMStore.cc:609
lat::Regexp * buildRegex(const string &expr)
__shared__ Hist hist
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
Definition: Run.h:45