CMS 3D CMS Logo

DQMRivetClient.cc
Go to the documentation of this file.
1 /*
2  * Class:DQMRivetClient
3  *
4  *
5  *
6  * \author Junghwan Goh - SungKyunKwan University
7  */
8 
10 
16 
17 #include <TH1F.h>
18 #include <TClass.h>
19 #include <TString.h>
20 #include <TPRegexp.h>
21 
22 #include <cmath>
23 #include <boost/tokenizer.hpp>
24 
25 using namespace std;
26 using namespace edm;
27 
29 
30 TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]");
31 
33  typedef std::vector<edm::ParameterSet> VPSet;
34  typedef std::vector<std::string> vstring;
35  typedef boost::escaped_list_separator<char> elsc;
36 
37  elsc commonEscapes("\\", " \t", "\'");
38 
39  // Parse Normalization commands
40  vstring normCmds = pset.getUntrackedParameter<vstring>("normalizationToIntegral", vstring());
41  for (vstring::const_iterator normCmd = normCmds.begin(); normCmd != normCmds.end(); ++normCmd) {
42  if (normCmd->empty())
43  continue;
44  boost::tokenizer<elsc> tokens(*normCmd, commonEscapes);
45 
46  vector<string> args;
47  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
48  if (iToken->empty())
49  continue;
50  args.push_back(*iToken);
51  }
52 
53  if (args.empty() or args.size() > 2) {
54  LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
55  continue;
56  }
57 
59  opt.name = args[0];
60  opt.normHistName = args.size() == 2 ? args[1] : args[0];
61 
62  normOptions_.push_back(opt);
63  }
64 
65  VPSet normSets = pset.getUntrackedParameter<VPSet>("normalizationToIntegralSets", VPSet());
66  for (VPSet::const_iterator normSet = normSets.begin(); normSet != normSets.end(); ++normSet) {
68  opt.name = normSet->getUntrackedParameter<string>("name");
69  opt.normHistName = normSet->getUntrackedParameter<string>("normalizedTo", opt.name);
70 
71  normOptions_.push_back(opt);
72  }
73 
74  //normalize to lumi
75  vstring lumiCmds = pset.getUntrackedParameter<vstring>("normalizationToLumi", vstring());
76  for (vstring::const_iterator lumiCmd = lumiCmds.begin(); lumiCmd != lumiCmds.end(); ++lumiCmd) {
77  if (lumiCmd->empty())
78  continue;
79  boost::tokenizer<elsc> tokens(*lumiCmd, commonEscapes);
80 
81  vector<string> args;
82  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
83  if (iToken->empty())
84  continue;
85  args.push_back(*iToken);
86  }
87 
88  if (args.size() != 2) {
89  LogInfo("DQMRivetClient") << "Wrong input to lumiCmds\n";
90  continue;
91  }
92 
94  opt.name = args[0];
95  opt.normHistName = args[1];
96  opt.xsection = pset.getUntrackedParameter<double>("xsection", -1.);
97  //opt.xsection = atof(args[2].c_str());
98 
99  //std::cout << opt.name << " " << opt.normHistName << " " << opt.xsection << std::endl;
100  lumiOptions_.push_back(opt);
101  }
102 
103  //multiply by a number
104  vstring scaleCmds = pset.getUntrackedParameter<vstring>("scaleBy", vstring());
105  for (vstring::const_iterator scaleCmd = scaleCmds.begin(); scaleCmd != scaleCmds.end(); ++scaleCmd) {
106  if (scaleCmd->empty())
107  continue;
108  boost::tokenizer<elsc> tokens(*scaleCmd, commonEscapes);
109 
110  vector<string> args;
111  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
112  if (iToken->empty())
113  continue;
114  args.push_back(*iToken);
115  }
116 
117  if (args.empty() or args.size() > 2) {
118  LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
119  continue;
120  }
121 
123  opt.name = args[0];
124  opt.scale = atof(args[1].c_str());
125  scaleOptions_.push_back(opt);
126  }
127 
128  outputFileName_ = pset.getUntrackedParameter<string>("outputFileName", "");
129  subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
130 }
131 
133  typedef vector<string> vstring;
134 
135  // Update 2009-09-23
136  // Migrated all code from endJob to this function
137  // endJob is not necessarily called in the proper sequence
138  // and does not necessarily book histograms produced in
139  // that step.
140  // It more robust to do the histogram manipulation in
141  // this endRun function
142 
143  theDQM = nullptr;
144  theDQM = Service<DQMStore>().operator->();
145 
146  if (!theDQM) {
147  LogInfo("DQMRivetClient") << "Cannot create DQMStore instance\n";
148  return;
149  }
150 
151  // Process wildcard in the sub-directory
152  set<string> subDirSet;
153 
154  for (vstring::const_iterator iSubDir = subDirs_.begin(); iSubDir != subDirs_.end(); ++iSubDir) {
155  string subDir = *iSubDir;
156 
157  if (subDir[subDir.size() - 1] == '/')
158  subDir.erase(subDir.size() - 1);
159 
160  subDirSet.insert(subDir);
161  }
162 
163  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
164  const string& dirName = *iSubDir;
165  for (vector<DQMGenericClient::NormOption>::const_iterator normOption = normOptions_.begin();
166  normOption != normOptions_.end();
167  ++normOption) {
168  normalizeToIntegral(dirName, normOption->name, normOption->normHistName);
169  }
170  }
171 
172  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
173  const string& dirName = *iSubDir;
174  for (vector<LumiOption>::const_iterator lumiOption = lumiOptions_.begin(); lumiOption != lumiOptions_.end();
175  ++lumiOption) {
176  normalizeToLumi(dirName, lumiOption->name, lumiOption->normHistName, lumiOption->xsection);
177  }
178  }
179 
180  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
181  const string& dirName = *iSubDir;
182  for (vector<ScaleFactorOption>::const_iterator scaleOption = scaleOptions_.begin();
183  scaleOption != scaleOptions_.end();
184  ++scaleOption) {
185  scaleByFactor(dirName, scaleOption->name, scaleOption->scale);
186  }
187  }
188 
189  if (!outputFileName_.empty())
190  theDQM->save(outputFileName_);
191 }
192 
194  // Update 2009-09-23
195  // Migrated all code from here to endRun
196 
197  LogTrace("DQMRivetClient") << "inside of DQMGenericClient::endJob()" << endl;
198 }
199 
201  const std::string& histName,
202  const std::string& normHistName) {
203  if (!theDQM->dirExists(startDir)) {
204  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
205  << "Cannot find sub-directory " << startDir << endl;
206  return;
207  }
208 
209  theDQM->cd();
210 
211  ME* element = theDQM->get(startDir + "/" + histName);
212  ME* normME = theDQM->get(startDir + "/" + normHistName);
213 
214  if (!element) {
215  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
216  << "No such element '" << histName << "' found\n";
217  return;
218  }
219 
220  if (!normME) {
221  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
222  << "No such element '" << normHistName << "' found\n";
223  return;
224  }
225 
226  TH1F* hist = element->getTH1F();
227  if (!hist) {
228  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
229  << "Cannot create TH1F from ME\n";
230  return;
231  }
232 
233  TH1F* normHist = normME->getTH1F();
234  if (!normHist) {
235  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
236  << "Cannot create TH1F from ME\n";
237  return;
238  }
239 
240  const double entries = normHist->Integral();
241  if (entries != 0) {
242  hist->Scale(1. / entries, "width");
243  } else {
244  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
245  << "Zero entries in histogram\n";
246  }
247 
248  return;
249 }
250 
252  const std::string& histName,
253  const std::string& normHistName,
254  double xsection) {
255  normalizeToIntegral(startDir, histName, normHistName);
256  theDQM->cd();
257  ME* element = theDQM->get(startDir + "/" + histName);
258  TH1F* hist = element->getTH1F();
259  if (!hist) {
260  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
261  << "Cannot create TH1F from ME\n";
262  return;
263  }
264  hist->Scale(xsection);
265  return;
266 }
267 
268 void DQMRivetClient::scaleByFactor(const std::string& startDir, const std::string& histName, double factor) {
269  if (!theDQM->dirExists(startDir)) {
270  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
271  << "Cannot find sub-directory " << startDir << endl;
272  return;
273  }
274 
275  theDQM->cd();
276 
277  ME* element = theDQM->get(startDir + "/" + histName);
278 
279  if (!element) {
280  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
281  << "No such element '" << histName << "' found\n";
282  return;
283  }
284 
285  TH1F* hist = element->getTH1F();
286  if (!hist) {
287  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
288  << "Cannot create TH1F from ME\n";
289  return;
290  }
291  hist->Scale(factor);
292 }
293 
295 
T getUntrackedParameter(std::string const &, T const &) const
vector< string > vstring
Definition: ExoticaDQM.cc:8
DQMRivetClient(const edm::ParameterSet &pset)
DQMRivetClient::MonitorElement ME
void scaleByFactor(const std::string &startDir, const std::string &histName, double factor)
TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]")
Definition: ME.h:11
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
#define LogTrace(id)
void normalizeToIntegral(const std::string &startDir, const std::string &histName, const std::string &normHistName)
HLT enums.
void endJob() override
void endRun(const edm::Run &r, const edm::EventSetup &c) override
EndRun.
void normalizeToLumi(const std::string &startDir, const std::string &histName, const std::string &normHistName, double xsection)
Definition: Run.h:45