test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
compareAlignments.cc File Reference
#include <string.h>
#include <cstring>
#include "TChain.h"
#include "TFile.h"
#include "TH1.h"
#include "TTree.h"
#include "TKey.h"
#include "TMath.h"
#include "Riostream.h"
#include <vector>
#include <sstream>
#include "TCanvas.h"
#include "TLegend.h"
#include "TROOT.h"
#include "TPaveStats.h"
#include "TObjArray.h"
#include "TObjString.h"
#include "TStyle.h"
#include "TEnv.h"
#include "Alignment/OfflineValidation/plugins/TkAlStyle.cc"

Go to the source code of this file.

Functions

void ColourStatsBoxes (TObjArray *hists)
 
void compareAlignments (TString namesandlabels="readFromFile", TString legendheader="", TString lefttitle="", TString righttitle="", bool bigtext=false)
 
void MergeRootfile (TDirectory *target, TList *sourcelist, TList *labellist, bool bigtext)
 
void nicePad (Int_t logx, Int_t logy)
 
void SetMinMaxRange (TObjArray *hists)
 

Variables

TList * FileList
 
TList * LabelList
 
std::vector< std::string > lowestlevels
 
TFile * Target
 
std::vector< int > theColors
 
std::vector< int > theStyles
 

Function Documentation

void ColourStatsBoxes ( TObjArray *  hists)

Definition at line 322 of file compareAlignments.cc.

References h.

Referenced by MergeRootfile().

323 {
324 
325  Double_t fStatsX1 = 0.85, fStatsX2 = 1., fStatsY1 = 0.77, fStatsY2 = 0.92;
326  // colours stats boxes like hists' line colors and moves the next to each other
327  if (!hists) return;
328  Double_t x1 = fStatsX1, x2 = fStatsX2, y1 = fStatsY1, y2 = fStatsY2;
329  for (Int_t iH = 0; iH < hists->GetEntries(); ++iH) {
330  TH1 *h = static_cast<TH1*>(hists->At(iH));
331  if (!h) continue;
332  TObject *statObj = h->GetListOfFunctions()->FindObject("stats");
333  if (statObj && statObj->InheritsFrom(TPaveStats::Class())) {
334  TPaveStats *stats = static_cast<TPaveStats*>(statObj);
335  stats->SetLineColor(static_cast<TH1*>(hists->At(iH))->GetLineColor());
336  stats->SetTextColor(static_cast<TH1*>(hists->At(iH))->GetLineColor());
337  stats->SetFillColor(10);
338  stats->SetX1NDC(x1);
339  stats->SetX2NDC(x2);
340  stats->SetY1NDC(y1);
341  stats->SetY2NDC(y2);
342  y2 = y1 - 0.005; // shift down 2
343  y1 = y2 - (fStatsY2 - fStatsY1); // shift down 1
344  if (y1 < 0.) {
345  y1 = fStatsY1; y2 = fStatsY2; // restart y-positions
346  x2 = x1 - 0.005; // shift left 2
347  x1 = x2 - (fStatsX2 - fStatsX1); // shift left 1
348  if (x1 < 0.) { // give up, start again:
349  x1 = fStatsX1, x2 = fStatsX2, y1 = fStatsY1, y2 = fStatsY2;
350  }
351  }
352  stats->DrawClone();
353  //} else if (gStyle->GetOptStat() != 0) { // failure in case changed in list via TExec....
354  //this->Warning("ColourStatsBoxes", "No stats found for %s", hists->At(iH)->GetName());
355  }
356  }
357 }
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void compareAlignments ( TString  namesandlabels = "readFromFile",
TString  legendheader = "",
TString  lefttitle = "",
TString  righttitle = "",
bool  bigtext = false 
)

Definition at line 37 of file compareAlignments.cc.

References gather_cfg::cout, CUSTOM, FileList, i, LabelList, TkAlStyle::legendheader, lowestlevels, MergeRootfile(), NONE, NULL, TkAlStyle::set(), Target, theColors, and theStyles.

38 {
39  cout << "Comparing using: >"<<namesandlabels<<"<"<<endl;
40 
41  TkAlStyle::legendheader = legendheader;
42  TkAlStyle::set(CUSTOM, NONE, lefttitle, righttitle);
43  gStyle->SetOptStat(111110);
44  gStyle->SetOptTitle(0);
45 
46  Target = TFile::Open( "result.root", "RECREATE" );
47  FileList = new TList();
48  LabelList = new TList();
49 
50  int formatCounter = 1;
51  //TObjArray* stringarray = namesandlabels.Tokenize(",");
52  TObjArray *nameandlabelpairs = namesandlabels.Tokenize(",");
53  for (Int_t i = 0; i < nameandlabelpairs->GetEntries(); ++i) {
54  TObjArray *aFileLegPair = TString(nameandlabelpairs->At(i)->GetName()).Tokenize("=");
55 
56  if(aFileLegPair->GetEntries() == 2) {
57  TFile* currentFile = TFile::Open(aFileLegPair->At(0)->GetName());
58  if( currentFile != NULL && !currentFile->IsZombie() ){
59  FileList->Add( currentFile ); // 2
60  if(TString(aFileLegPair->At(1)->GetName()).Contains("|")){
61  TObjArray* formatedLegendEntry = TString(aFileLegPair->At(1)->GetName()).Tokenize("|");
62  LabelList->Add( formatedLegendEntry->At(0) );
63  if(formatedLegendEntry->GetEntries() > 1){
64  theColors.push_back(atoi(formatedLegendEntry->At(1)->GetName()));
65 
66  if(formatedLegendEntry->GetEntries() > 2)
67  theStyles.push_back(atoi(formatedLegendEntry->At(2)->GetName()));
68  else
69  theStyles.push_back( formatCounter );
70  }else{
71  std::cout <<"if you give a \"|\" in the legend name you will need to at least give a int for the color"<<std::endl;
72  }
73  formatCounter++;
74  }else{
75  LabelList->Add( aFileLegPair->At(1) );
76  theColors.push_back(formatCounter);
77  theStyles.push_back(formatCounter);
78  formatCounter++;
79  }
80  }else{
81  std::cout << "Could not open: "<<aFileLegPair->At(0)->GetName()<<std::endl;
82  }
83  }
84  else {
85  std::cout << "Please give file name and legend entry in the following form:\n"
86  << " filename1=legendentry1,filename2=legendentry2[|color[|style]]"<<std::endl;
87 
88  }
89 
90  }
91 
92  // ************************************************************
93  // List of Files
94  //FileList->Add( TFile::Open("../test/AlignmentValidation_Elliptical.root") );
95  //FileList->Add( TFile::Open("../test/AlignmentValidation_10pb.root") ); // 2
96  //FileList->Add( TFile::Open("../test/AlignmentValidation_custom.root") ); // 2
97  // ************************************************************
98 
99  // put here the lowest level up to which you want to combine the
100  // histograms
101  lowestlevels.push_back("TPBLadder");
102  lowestlevels.push_back("TPEPanel");
103  lowestlevels.push_back("TIBHalfShell");
104  lowestlevels.push_back("TIDRing");
105  lowestlevels.push_back("TOBRod");
106  lowestlevels.push_back("TECSide");
107 // lowestlevels.push_back("Det");
108 
109 
110 
111  MergeRootfile( Target, FileList, LabelList, bigtext );
112 
113 }
std::vector< int > theStyles
int i
Definition: DBlmapReader.cc:9
std::vector< int > theColors
#define NULL
Definition: scimark2.h:8
static void set(const PublicationStatus status, const Era era=NONE, const TString customTitle="", const TString customRightTitle="")
Definition: TkAlStyle.cc:401
void MergeRootfile(TDirectory *target, TList *sourcelist, TList *labellist, bool bigtext)
static TString legendheader
Definition: TkAlStyle.cc:102
TList * LabelList
TFile * Target
std::vector< std::string > lowestlevels
tuple cout
Definition: gather_cfg.py:145
TList * FileList
void MergeRootfile ( TDirectory *  target,
TList *  sourcelist,
TList *  labellist,
bool  bigtext 
)

Definition at line 115 of file compareAlignments.cc.

References EnergyCorrector::c, ecal_dqm_sourceclient-live_cfg::cerr, ColourStatsBoxes(), gather_cfg::cout, HILowLumiHLTOfflineSource_cfi::dirname, TkAlStyle::drawStandardTitle(), relval_steps::key, create_public_lumi_plots::leg, TkAlStyle::legendheader, lowestlevels, bookConverter::max, nicePad(), NULL, getGTfromDQMFile::obj, fed_dqm_sourceclient-live_cfg::path, lumiQueryAPI::q, pileupReCalc_HLTpaths::scale, SetMinMaxRange(), mps_update::status, AlCaHLTBitMon_QueryRunRegistry::string, TkAlStyle::textSize, theColors, and theStyles.

Referenced by compareAlignments().

115  {
116 
117  if( sourcelist->GetSize() == 0){
118  std::cout<< "Cowardly refuse to merge empty SourceList! " <<std::endl;
119  return;
120  }
121 
122  TString path( (char*)strstr( target->GetPath(), ":" ) );
123  path.Remove( 0, 2 );
124 
125  TFile *first_source = (TFile*)sourcelist->First();
126  TObjString *first_label = (TObjString*)labellist->First();
127 
128  first_source->cd( path );
129  TDirectory *current_sourcedir = gDirectory;
130  //gain time, do not add the objects in the list in memory
131  Bool_t status = TH1::AddDirectoryStatus();
132  TH1::AddDirectory(kFALSE);
133 
134  // loop over all keys in this directory
135 
136  TIter nextkey( current_sourcedir->GetListOfKeys() );
137  TKey *key, *oldkey=0;
138  while ( (key = (TKey*)nextkey())) {
139 
140  //keep only the highest cycle number for each key
141  if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
142 
143  // read object from first source file
144  first_source->cd( path );
145  TObject *obj = key->ReadObj();
146 
147  if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
148  // descendant of TH1 -> merge it
149  TCanvas c(obj->GetName(),obj->GetName());
150  c.SetFillColor(10);
151 
152  bool is2d = false;
153  if(strstr(obj->ClassName() ,"TH2") != NULL )
154  is2d = true;
155  TH1 *h1 = static_cast<TH1*>(obj);
156 
157  int q = 1;
158  TObjArray *histarray = new TObjArray;
159 
160  h1->SetLineStyle(theStyles.at(q-1));
161  h1->SetLineWidth(2);
162 
163  h1->SetTitle("");
164 
165  h1->SetLineColor(theColors.at(q-1));
166  h1->GetYaxis()->SetTitleOffset(1.5);
167  if(strstr(h1->GetName(),"summary") != NULL )
168  h1->Draw("x0e1*H");
169  else if(is2d)
170  h1->Draw();
171  else
172  h1->Draw();
173 
174  double max = h1->GetMaximum();
175  double scale = 1;
176  if (max > 1000) {
177  int power = (int)(TMath::Log10(max / 100));
178  scale = 1/TMath::Power(10, power);
179  h1->GetYaxis()->SetTitle(((TString(h1->GetYaxis()->GetTitle()) + " [#times 10^{") += (int)power) + "}]");
180  }
181  h1->Scale(scale);
182 
183  int nPlots = sourcelist->GetSize();
184  double legendY = 0.80;
185  if (nPlots > 3) { legendY -= 0.01 * (nPlots - 3); }
186  if (bigtext) { legendY -= 0.05; }
187  if (legendY < 0.6) {
188  std::cerr << "Warning: Huge legend!" << std::endl;
189  legendY = 0.6;
190  }
191  TLegend leg(0.17, legendY, 0.85, 0.88);
192  bool hasheader = (TkAlStyle::legendheader != "");
193  if (hasheader) leg.SetHeader(TkAlStyle::legendheader);
194  if (bigtext) leg.SetTextSize(TkAlStyle::textSize);
195  leg.AddEntry(h1,first_label->String().Data(),"L");
196  leg.SetBorderSize(0);
197  leg.SetFillColor(10);
198  // loop over all source files and add the content of the
199  // correspondant histogram to the one pointed to by "h1"
200  TFile *nextsource = (TFile*)sourcelist->After( first_source );
201  TObjString *nextlabel = (TObjString*)labellist->After( labellist->First() );
202 
203  histarray->Add(h1);
204  while ( nextsource ) {
205 
206  // make sure we are at the correct directory level by cd'ing to path
207 
208  nextsource->cd( path );
209  TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
210  if (key2) {
211  ++q;
212  TH1 *h2 = (TH1*)key2->ReadObj();
213 
214  if(!is2d){
215  h2->SetLineStyle(theStyles.at(q-1));
216  h2->SetLineWidth(2);
217  }
218 
219  h2->SetLineColor(theColors.at(q-1));
220  std::stringstream newname;
221  newname << h2->GetName() << q;
222  h2->Scale(scale);
223 
224  h2->SetName(newname.str().c_str());
225  if(strstr(newname.str().c_str(),"summary") != NULL )
226  h2->DrawClone("x0*He1sames");
227  else if(is2d)
228  h2->DrawClone("sames");
229  else
230  h2->DrawClone("sames");
231  leg.AddEntry(c.FindObject(h2->GetName()),nextlabel->String().Data(),"L");
232  histarray->Add(c.FindObject(h2->GetName()));
233  delete h2;
234  } else {
235  std::cerr << "Histogram "<< key2->GetTitle() << " is not present in file " << nextsource->GetName() << std::endl;
236  }
237 
238  nextsource = (TFile*)sourcelist->After( nextsource );
239  nextlabel = (TObjString*)labellist->After(nextlabel);
240  }
241  nicePad(0,0);
242  leg.Draw();
244  c.Update();
245  if(strstr(h1->GetName(),"summary") == NULL )
246  SetMinMaxRange(histarray);
247  ColourStatsBoxes(histarray);
248  target->cd();
249  c.Update();
250  c.Write();
251  histarray->Delete();
252 
253 
254 
255 
256  } else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
257  // it's a subdirectory
258 
259  std::string dirname = obj->GetName();
260  for( std::vector< std::string >::const_iterator lowlevelit = lowestlevels.begin(),
261  lowlevelitend = lowestlevels.end(); lowlevelit != lowlevelitend; ++lowlevelit)
262  if( dirname.find(*lowlevelit) != std::string::npos )
263  return;
264 
265  // create a new subdir of same name and title in the target file
266  target->cd();
267  TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
268 
269  // newdir is now the starting point of another round of merging
270  // newdir still knows its depth within the target file via
271  // GetPath(), so we can still figure out where we are in the recursion
272  MergeRootfile( newdir, sourcelist, labellist, bigtext );
273 
274 
275  } else {
276 
277  // object is of no type that we know or can handle
278  cout << "Unknown object type, name: "
279  << obj->GetName() << " title: " << obj->GetTitle() << endl;
280  }
281 
282 
283  } // while ( ( TKey *key = (TKey*)nextkey() ) )
284 
285  // save modifications to target file
286  target->SaveSelf(kTRUE);
287  TH1::AddDirectory(status);
288 }
std::vector< int > theStyles
std::vector< int > theColors
#define NULL
Definition: scimark2.h:8
static double textSize
Definition: TkAlStyle.cc:104
void ColourStatsBoxes(TObjArray *hists)
static void drawStandardTitle()
Definition: TkAlStyle.cc:75
void MergeRootfile(TDirectory *target, TList *sourcelist, TList *labellist, bool bigtext)
static TString legendheader
Definition: TkAlStyle.cc:102
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
void SetMinMaxRange(TObjArray *hists)
std::vector< std::string > lowestlevels
tuple cout
Definition: gather_cfg.py:145
tuple status
Definition: mps_update.py:57
void nicePad(Int_t logx, Int_t logy)
void nicePad ( Int_t  logx,
Int_t  logy 
)

Definition at line 293 of file compareAlignments.cc.

Referenced by MergeRootfile().

294 {
295 /*
296  gPad->SetBottomMargin(0.10);
297  gPad->SetRightMargin(0.1);
298  gPad->SetLeftMargin(0.15);
299  gPad->SetTopMargin(0.15);
300  gPad->SetTickx(1);
301  gPad->SetTicky(1);
302 */
303  if(logy==1)
304  {
305  gPad->SetLogy();
306  }
307  else
308  {
309  gPad->SetLogy(0);
310  }
311  if(logx==1)
312  {
313  gPad->SetLogx();
314  }
315  else
316  {
317  gPad->SetLogx(0);
318  }
319 }
void SetMinMaxRange ( TObjArray *  hists)

Definition at line 360 of file compareAlignments.cc.

References h, i, bookConverter::max, and min().

Referenced by MergeRootfile().

361 {
362  Double_t min = 100000;
363  Double_t max = -100000;
364  for (Int_t iH = 0; iH < hists->GetEntries(); ++iH) {
365  TH1 *h = static_cast<TH1*>(hists->At(iH));
366  if (!h) continue;
367  for(int i = 1; i <= h->GetNbinsX(); ++i) {
368  if(h->GetBinContent(i) + h->GetBinError(i) > max ) max = h->GetBinContent(i) + h->GetBinError(i);
369  if(h->GetBinContent(i) - h->GetBinError(i) < min ) min = h->GetBinContent(i) - h->GetBinError(i);
370  }
371  }
372 
373  TH1 *h_first = static_cast<TH1*>(hists->At(0));
374  h_first->SetMaximum(max*1.3);
375  if(min == 0.) {
376  min = -1111;
377  h_first->SetMinimum(min);
378  } else {
379  h_first->SetMinimum(min-min*0.1);
380  }
381 }
int i
Definition: DBlmapReader.cc:9
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
T min(T a, T b)
Definition: MathUtil.h:58

Variable Documentation

TList* FileList

Definition at line 24 of file compareAlignments.cc.

Referenced by compareAlignments().

TList* LabelList

Definition at line 25 of file compareAlignments.cc.

Referenced by compareAlignments().

std::vector< std::string > lowestlevels

Definition at line 27 of file compareAlignments.cc.

Referenced by compareAlignments(), and MergeRootfile().

TFile* Target

Definition at line 26 of file compareAlignments.cc.

Referenced by compareAlignments(), and HistoData::setResultTarget().

std::vector< int > theColors

Definition at line 28 of file compareAlignments.cc.

Referenced by compareAlignments(), and MergeRootfile().

std::vector< int > theStyles

Definition at line 29 of file compareAlignments.cc.

Referenced by compareAlignments(), and MergeRootfile().