CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalHistoUtils.h
Go to the documentation of this file.
1 #ifndef GUARD_HCALTESTUTILS_H
2 #define GUARD_HCALTESTUTILS_H
3 
4 #define UTILS_ETAMIN -44.5
5 #define UTILS_ETAMAX 44.5
6 #define UTILS_PHIMIN -0.5
7 #define UTILS_PHIMAX 73.5
8 
9 #include "TROOT.h"
10 #include "TStyle.h"
11 #include "TColor.h"
12 
13 #include "TH1.h"
14 #include "TCanvas.h"
15 #include "TGaxis.h"
16 
17 #include "TH1F.h"
18 #include "TH2F.h"
19 #include "TProfile.h"
20 #include "TFile.h"
21 
22 #include <iostream>
23 #include <fstream>
24 
25 /******************************************************************************
26  *
27  * HcalTestUtils.h
28  * v1.1
29  * 4 May 2008
30  * by Jeff Temple (jtemple AT fnal.gov)
31  *
32  * Various template methods to grab any type of
33  * histogram stored as a MonitorElement, and
34  * to return .gif, .html output from the histogram.
35  *
36  * Methods:
37  *
38  * myHist* getAnyHisto(myHist* hist, std::string name, std::string process,
39  DQMStore* dbe_, bool verb, bool clone)
40  *
41  * std::string getAnyIMG(int runNo,myHist* hist, int size, std::string htmlDir,
42  const char* xlab, const char* ylab, bool setLogy, bool setLogx)
43  *
44  * void htmlAnyHisto(int runNo, myHist *hist,
45  const char* xlab, const char* ylab,
46  int width, std::ofstream& htmlFile,
47  std::string htmlDir, bool setLogy, bool setLogx)
48 
49  *
50  *****************************************************************************
51  */
52 
54 
55 // Template class 'getAnyHisto' functions contain memory leaks somewhere. Re-introduce getTH1F, getTH2F, getTProfile
56 
57 inline TH1F* getTH1F(std::string name, std::string process, std::string rootfolder, DQMStore* dbe_, bool verb, bool clone)
58 {
59 
60  if (!dbe_) return NULL;
61  std::stringstream title;
62  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
63 
64  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
65 
66  if (!me)
67  {
68  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
69  return NULL; // ME not found
70  } // if (!me)
71 
72  if (verb)
73  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
74 
75  std::stringstream clonehisto;
76  if (clone)
77  {
78  clonehisto<<"ME "<<name.c_str();
79  TH1F *out = dynamic_cast<TH1F*>(me->getTH1F()->Clone(clonehisto.str().c_str()));
80  return out;
81  }
82  else return (me->getTH1F());
83 }
84 
85 inline TH2F* getTH2F(std::string name, std::string process, std::string rootfolder, DQMStore* dbe_, bool verb, bool clone)
86 {
87  if (!dbe_) return NULL;
88  std::stringstream title;
89  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
90 
91  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
92 
93  if (!me)
94  {
95  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
96  return NULL; // ME not found
97  } // if (!me)
98 
99  if (verb)
100  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
101 
102  std::stringstream clonehisto;
103  if (clone)
104  {
105  clonehisto<<"ME "<<name.c_str();
106  TH2F *out = dynamic_cast<TH2F*>(me->getTH2F()->Clone(clonehisto.str().c_str()));
107  return out;
108  }
109  else return (me->getTH2F());
110 }
111 
112 
113 inline TH3F* getTH3F(std::string name, std::string process, std::string rootfolder, DQMStore* dbe_, bool verb, bool clone)
114 {
115  if (!dbe_) return NULL;
116  std::stringstream title;
117  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
118 
119  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
120 
121  if (!me)
122  {
123  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
124  return NULL; // ME not found
125  } // if (!me)
126 
127  if (verb)
128  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
129 
130  std::stringstream clonehisto;
131  if (clone)
132  {
133  clonehisto<<"ME "<<name.c_str();
134  TH3F *out = dynamic_cast<TH3F*>(me->getTH3F()->Clone(clonehisto.str().c_str()));
135  return out;
136  }
137  else return (me->getTH3F());
138 }
139 
140 inline TProfile* getTProfile(std::string name, std::string process, std::string rootfolder, DQMStore* dbe_, bool verb, bool clone)
141 {
142  if (!dbe_) return NULL;
143  std::stringstream title;
144  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
145 
146  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
147 
148  if (!me)
149  {
150  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
151  return NULL; // ME not found
152  } // if (!me)
153 
154  if (verb)
155  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
156 
157  std::stringstream clonehisto;
158  if (clone)
159  {
160  clonehisto<<"ME "<<name.c_str();
161  TProfile *out = dynamic_cast<TProfile*>(me->getTProfile()->Clone(clonehisto.str().c_str()));
162  return out;
163  }
164  else return (me->getTProfile());
165 }
166 
167 inline TProfile2D* getTProfile2D(std::string name, std::string process, std::string rootfolder, DQMStore* dbe_, bool verb, bool clone)
168 {
169  if (!dbe_) return NULL;
170  std::stringstream title;
171  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
172 
173  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
174 
175  if (!me)
176  {
177  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
178  return NULL; // ME not found
179  } // if (!me)
180 
181  if (verb)
182  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
183 
184  std::stringstream clonehisto;
185  if (clone)
186  {
187  clonehisto<<"ME "<<name.c_str();
188  TProfile2D *out = dynamic_cast<TProfile2D*>(me->getTProfile2D()->Clone(clonehisto.str().c_str()));
189  return out;
190  }
191  else return (me->getTProfile2D());
192 }
193 
194 
195 
196 template <class myHist>
197 myHist* getAnyHisto(myHist* hist,
199  bool verb, bool clone)
200 {
201 
202  // If subsystem folder not specified, assume that it's "Hcal"
203  myHist* theHist=getAnyHisto(hist, name, process, "Hcal",dbe_, verb, clone);
204  return theHist;
205 }
206 
207 
208 template <class myHist>
209 myHist* getAnyHisto(myHist* hist,
211  bool verb, bool clone)
212 {
213  /*
214  Method returns histogram named 'name' from DQMStore dbe_;
215  'hist' pointer must be declared with 'new' (e.g., new TH2F())
216  in function call so that the pointer actually points to something.
217  Otherwise, the call to hist->ClassName() will fail.
218  We might implement a scale functionality at some later point?
219  */
220 
221  if (!dbe_) return NULL;
222 
223  std::stringstream clonehisto;
224  std::stringstream title;
225  title <<process.c_str()<<rootfolder.c_str()<<"/"<<name.c_str();
226  //sprintf(title, "%sHcal/%s",process.c_str(),name.c_str());
227 
228  MonitorElement* me = dbe_->get(title.str().c_str()); // get Monitor Element named 'title'
229 
230  if (!me)
231  {
232  if (verb) std::cout <<"SORRY, COULD NOT FIND HISTOGRAM NAMED ["<< title.str().c_str()<<"]"<<std::endl;
233  return NULL; // ME not found
234  } // if (!me)
235 
236  if (verb)
237  std::cout << "Found '" << title.str().c_str() << "'" << std::endl;
238 
239  if (clone)
240  clonehisto<<"ME "<<name.c_str(); // set clone histogram name
241 
242  /* As of 25 April 2008, there are 5 histogram types associated with
243  Monitor Elements (TH1F, TH2F, TH3F, TProfile, and TProfile2D).
244  Provide a separate getter for each type. Add others if necessary.
245  */
246 
247  std::string histtype = hist->ClassName();
248 
249  // return TH1F from ME
250  if (histtype=="TH1F")
251  {
252  TH1F* out;
253  if (clone) out = dynamic_cast<TH1F*>(me->getTH1F()->Clone(clonehisto.str().c_str()));
254  else out = me->getTH1F();
255  if (verb) std::cout <<"Got histogram! Max = "<<out->GetMaximum()<<std::endl;
256  return dynamic_cast<myHist*>(out);
257  }
258 
259  // return TH2F from ME
260  else if (histtype=="TH2F")
261  {
262  TH2F* out;
263  if (clone) out = dynamic_cast<TH2F*>(me->getTH2F()->Clone(clonehisto.str().c_str()));
264  else out = me->getTH2F();
265 
266  if (verb) std::cout <<"Got histogram! Max = "<<out->GetMaximum()<<std::endl;
267  return dynamic_cast<myHist*>(out);
268  }
269 
270  // return TH3F from ME
271  else if (histtype=="TH3F")
272  {
273  TH3F* out;
274  if (clone) out = dynamic_cast<TH3F*>(me->getTH3F()->Clone(clonehisto.str().c_str()));
275  else out = me->getTH3F();
276  return dynamic_cast<myHist*>(out);
277  }
278 
279  // return TProfile from ME
280  else if (histtype=="TProfile")
281  {
282  TProfile* out;
283  if (clone) out = dynamic_cast<TProfile*>(me->getTProfile()->Clone(clonehisto.str().c_str()));
284  else out = me->getTProfile();
285  return dynamic_cast<myHist*>(out);
286  }
287 
288  // return TProfile2D from ME
289  else if (histtype=="TProfile2D")
290  {
291  TProfile2D* out;
292  if (clone) out = dynamic_cast<TProfile2D*>(me->getTProfile2D()->Clone(clonehisto.str().c_str()));
293  else out = me->getTProfile2D();
294  return dynamic_cast<myHist*>(out);
295  }
296 
297  else
298  {
299  if (verb)
300  {
301  std::cout <<"Don't know how to access histogram '"<<title;
302  std::cout<<"' of type '"<<histtype<<"'"<<std::endl;
303  }
304  return NULL;
305  }
306 
307  // Should never reach this point
308  if (verb)
309  std::cout <<"<HcalHistUtils::getAnyHisto> YOU SHOULD NEVER SEE THIS MESSAGE!"<<std::endl;
310  return NULL;
311 
312 } // myHist* getAnyHisto(...)
313 
314 
315 
316 
317 // MAKE GIF FROM HISTOGRAM IMAGE
318 template <class myHist>
319 std::string getAnyIMG(int runNo,myHist* hist, int size, std::string htmlDir,
320  const char* xlab, const char* ylab, int debug )
321 {
322  /*
323  Template function draws histogram plot, and saves it as a .gif image.
324  If size==1, thumbnail image is made. Otherwise, full-size image is made.
325  */
326 
327  if(hist==NULL)
328  {
329  return ""; // no histogram provided
330  }
331 
332  // Grab the histogram's title, and convert it to something more palatable for use as a file name
333 
334  // Run cleanString algorithm -- direct call of cleanString causes a crash
335  std::string name = (std::string)hist->GetTitle();
336  if (debug>9) std::cout <<"NAME = ["<<name<<"]"<<std::endl;
337  for ( unsigned int i = 0; i < name.size(); ++i ) {
338  if ( name.substr(i, 6) == " - Run" ){
339  name.replace(i, name.size()-i, "");
340  }
341  if ( name.substr(i, 4) == "_Run" ){
342  name.replace(i, name.size()-i, "");
343  }
344  if ( name.substr(i, 5) == "__Run" ){
345  name.replace(i, name.size()-i, "");
346  }
347 
348  if (name.substr(i,1) == "(" || name.substr(i,1)==")")
349  name.replace(i,1,"_");
350  else if (name.substr(i,1)==",")
351  name.replace(i,1,"_");
352  else if (name.substr(i,1)=="<")
353  name.replace(i,1,"_lt_");
354  else if (name.substr(i,1)==">")
355  name.replace(i,1,"_gt_");
356  else if (name.substr(i,1)=="+")
357  name.replace(i,1,"_plus_");
358  else if (name.substr(i,1)=="#")
359  name.replace(i,1,"");
360  else if (name.substr(i,1)=="/")
361  name.replace(i,1,"_div_");
362  } // for (unsigned int i=0; i< name.size();
363  //std::cout <<"NEWNAME = ["<<name<<"]"<<std::endl;
364 
365  char dest[512]; // stores name of destination .gif file
366  if(runNo>-1) sprintf(dest,"%s - Run %d",name.c_str(),runNo);
367  else sprintf(dest,"%s",name.c_str());
368 
369  //hist->SetTitle(dest); // no need to change the histogram title itself, right?
371 
372  int xwid = 900;
373  int ywid =540;
374 
375  if(size==1) // thumbnail specified
376  {
377  title = title+"_tmb";
378  xwid = 600;
379  ywid = 360;
380  }
381 
382  // run parseString algorithm -- calling it directly causes a crash
383  for ( unsigned int i = 0; i < title.size(); ++i ) {
384  if ( title.substr(i, 1) == " " ){
385  title.replace(i, 1, "_");
386  }
387  if ( title.substr(i, 1) == "#" ){
388  title.replace(i, 1, "N");
389  }
390  if ( title.substr(i, 1) == "-" ){
391  title.replace(i, 1, "_");
392  }
393  if ( title.substr(i, 1) == "&" ){
394  title.replace(i, 1, "_and_");
395  }
396  if ( title.substr(i, 1) == "("
397  || title.substr(i, 1) == ")"
398  ) {
399  title.replace(i, 1, "_");
400  }
401  if ( title.substr(i,1) == "="){
402  title.replace(i,1,"_");
403  }
404  } // for (unsigned int i=0; i < title.size();...)
405 
406  std::string outName = title+".gif";
407  std::string saveName = htmlDir + outName;
408 
409 
410  // Create canvas for histogram
411  TCanvas* can = new TCanvas(dest,dest, xwid, ywid);
412  TAxis* xaxis=0;
413  TAxis* yaxis=0;
414  TLine* vert=0;
415  TLine* horiz=0;
416  hist->SetXTitle(xlab);
417  hist->SetYTitle(ylab);
418  std::string histtype=hist->ClassName();
419  //can->GetFrame()->SetFillColor(21); // change canvas to different default color?
420 
421  // Don't draw stat box for color plots
422  if (((std::string)hist->GetOption())=="col" ||
423  ((std::string)hist->GetOption())=="colz")
424  hist->SetStats(false);
425 
426  // Draw with whatever options are set for the particular histogram
427 
428  hist->Draw(hist->GetOption());// I think that Draw should automatically use the GetOption() value, but include it here to be sure.
429 
430  // Draw Grid Lines
431 
432  if (histtype=="TH2F")
433  {
434  TAxis *xaxis = hist->GetXaxis();
435  TAxis *yaxis=hist->GetYaxis();
436  // Draw vertical lines
437  //for (int xx=int(UTILS_ETAMIN);xx<=int(UTILS_ETAMAX);++xx)
438 
439 
440  if (xaxis->GetXmax()==UTILS_ETAMAX && xaxis->GetXmin()==UTILS_ETAMIN
441  && yaxis->GetXmax()==UTILS_PHIMAX && yaxis->GetXmin()==UTILS_PHIMIN) // ad hoc method for only drawing grids for eta-phi graphs; need to be more clever later?
442  {
443  for (int xx=int(xaxis->GetXmin());
444  xx<=int(xaxis->GetXmax()); ++xx)
445  {
446  if (xx<-42 || xx >= 42) continue;
447  vert = new TLine(xx+0.5,0.5,xx+0.5,72.5);
448  //if (xx%vertlinespace!=0) continue;
449  //TLine *vert = new TLine(xx,yaxis->GetXmin(),xx,yaxis->GetXmax());
450 
451  vert->SetLineStyle(3);
452  vert->Draw("same");
453  }
454  // Draw horizontal lines
455  for (int yy=int(yaxis->GetXmin()); yy<int(yaxis->GetXmax());++yy)
456  {
457  if (yy%4==0)
458  horiz = new TLine(-41.5,yy+0.5,41.5,yy+0.5);
459  else if (yy%2==0)
460  horiz = new TLine(-39.5,yy+0.5,39.5,yy+0.5);
461  else
462  horiz = new TLine(-20.5,yy+0.5,20.5,yy+0.5);
463  //if (yy%horizlinespace!=0) continue;
464  //TLine *horiz = new TLine(xaxis->GetXmin(),yy,xaxis->GetXmax(),yy);
465  horiz->SetLineStyle(3);
466  horiz->Draw("same");
467  }
468  } //if (xaxis->GetXmax()==44)
469  } // if (histtype=="TH2F")
470 
471  can->SaveAs(saveName.c_str());
472  delete can;
473  delete vert;
474  delete horiz;
475  delete xaxis;
476  delete yaxis;
477 
478  return outName;
479 } // std::string getAnyIMG(...)
480 
481 
482 
483 
484 // make HTML from histogram
485 template <class myHist>
486 void htmlAnyHisto(int runNo, myHist *hist,
487  const char* xlab, const char* ylab,
488  int width, std::ofstream& htmlFile,
489  std::string htmlDir,
490  int debug=0)
491 {
492 
493  /*
494  Generates html output from any kind of input histogram
495  */
496 
497  if(hist!=NULL)
498  {
499  std::string histtype=hist->ClassName();
500 
501  // Set 2D histogram default option to "colz"
502  if (histtype=="TH2F" && ((std::string)hist->GetOption())=="")
503  {
504  hist->SetOption("colz");
505  }
506 
507  // Form full-sized and thumbnail .gifs from histogram
508  //std::string imgNameTMB = "";
509  //std::string imgNameTMB = getAnyIMG(runNo,hist,1,htmlDir,xlab,ylab,debug);
510  //std::string imgName = "";
511  std::string imgName = getAnyIMG(runNo,hist,2,htmlDir,xlab,ylab,debug);
512 
513  // Add thumbnail image to html code, linked to full-sized image
514  if (imgName.size() != 0 )
515  {
516  // Always make width = 100% ?
517  //htmlFile << "<td align=\"center\"><a href=\"" << imgName << "\"><img src=\"" << imgName << "\" width = \"100%\"></a><br>"<<hist->GetName()<<"</td>" << std::endl;
518  htmlFile <<"<td align=\"center\"><a href=\"" <<imgName<<"\"><img src=\""<<imgName<<"\" width=600 height=360\"></a><br>"<<hist->GetName()<<"</td>"<<std::endl;
519 }
520  else
521  {
522  htmlFile << "<td align=\"center\"><img src=\"" << " " << "\"></td>" << std::endl;
523  }
524  } // (hist != NULL)
525 
526  else // if no image found, make a blank table entry (maybe improve on this later? -- write an error message?)
527  {
528  htmlFile<<"<td align=\"center\"><br><br> Histogram does not exist in ROOT file!<br>Diagnostic flag may be off.<br>(This may be normal in online running.)</td>"<<std::endl;
529  //htmlFile << "<td><img src=\"" << " " << "\"></td>" << std::endl;
530  }
531  return;
532 } //void htmlAnyHisto(...)
533 
534 
535 
536 #endif
int i
Definition: DBlmapReader.cc:9
TProfile * getTProfile(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
TProfile2D * getTProfile2D(void) const
void htmlAnyHisto(int runNo, myHist *hist, const char *xlab, const char *ylab, int width, std::ofstream &htmlFile, std::string htmlDir, int debug=0)
myHist * getAnyHisto(myHist *hist, std::string name, std::string process, DQMStore *dbe_, bool verb, bool clone)
#define UTILS_PHIMIN
Definition: HcalHistoUtils.h:6
TH3F * getTH3F(void) const
#define NULL
Definition: scimark2.h:8
#define UTILS_ETAMIN
Definition: HcalHistoUtils.h:4
TH3F * getTH3F(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
TH1F * getTH1F(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
TProfile2D * getTProfile2D(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1729
#define UTILS_ETAMAX
Definition: HcalHistoUtils.h:5
DQMStore * dbe_
tuple out
Definition: dbtoconf.py:99
#define debug
Definition: HDRShower.cc:19
#define UTILS_PHIMAX
Definition: HcalHistoUtils.h:7
TH1F * getTH1F(void) const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
TProfile * getTProfile(void) const
TH2F * getTH2F(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
tuple cout
Definition: gather_cfg.py:121
TH2F * getTH2F(void) const
tuple process
Definition: LaserDQM_cfg.py:3
std::string getAnyIMG(int runNo, myHist *hist, int size, std::string htmlDir, const char *xlab, const char *ylab, int debug)
tuple size
Write out results.