CMS 3D CMS Logo

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