FairRoot/PandaRoot
PmtTask/drawhistosA4.C
Go to the documentation of this file.
1 
2 int drawhistosA4(TString filename="Data/HistoVertexing.root")
3 {
4  gROOT->LoadMacro("$VMCWORKDIR/macro/run/Tools.C");
6  //swotch things off
7  gStyle->SetOptTitle(1);
8  gStyle->SetOptStat(0);
9  gStyle->SetOptFit(0);
10 
11  // use bigger font for many plots on a page
12  gStyle->SetTextSize(0.10);
13  gStyle->SetLabelSize(0.07,"x");
14  gStyle->SetTitleSize(0.07,"x");
15  gStyle->SetLabelSize(0.07,"y");
16  gStyle->SetTitleSize(0.07,"y");
17  gStyle->SetLabelSize(0.07,"z");
18  gStyle->SetTitleSize(0.07,"z");
19  gStyle->SetPadBottomMargin(0.17);
20 
21  plotandfithistosfromfile(filename.Data(),"pdf",3,5,1200);
22 
23  return 0;
24 
25 }
26 
27 
28 
29 plotandfithistosfromfile(TString filename = "histos.root", TString ext=".pdf", Int_t divx=2, Int_t divy=2, Int_t pix = 1200)
30 { // Plot all histograms into a ps file
31  // works with TH1, TH2, & TProfile
32  TFile* file = new TFile(filename.Data());
33  if (!file) {cout<<"File \""<<filename.Data()<<"\" is not there..."<<endl;return;}
34  TCanvas* can = new TCanvas();
35  Int_t pixx = pix;
36  Int_t pixy = ceil(1.2*pix);
37  can->SetCanvasSize(pixx,pixy);
38  can->Divide(divx, divy);
39  TString picname = filename;
40  ext="-gfitsA4."+ext;
41  ext.ReplaceAll("..",".");
42  picname.ReplaceAll(".root",ext); // ps, png, pdf ...
43  TString pic = picname + "["; // open empty ps
44  cout << "opening: " << pic.Data()<<endl;
45  can->Print(pic);
46  pic=picname;
47 
48  TList* list = file->GetListOfKeys();
49  if (!list) {cout<<"List not there..."<<endl;return;}
50  int padcount = 1;
51  TString keyclass="";
52  for(int i=0;i<list->GetEntries();i++)
53  {
54  if(padcount > divx*divy)
55  {
56  can->Print(pic.Data());
57  can->Clear();
58  can->SetCanvasSize(pixx,pixy);
59  can->Divide(divx, divy);
60  padcount=1;
61  }
62  can->cd(padcount);
63  TKey* key = (TKey*)list->At(i);
64  keyclass = key->GetClassName();
65  //cout<<keyclass.Data()<<endl;
66  if(keyclass.Contains("TH1"))
67  {
68  //cout<<"try plotting a TH1"<<endl;
69  TH1* his = (TH1*)key->ReadObj();
70  TString titel=his->GetTitle();
71  TString vorname = his->GetName();
72  if(titel.Contains("value")) continue; // skip this one
73  if(titel.Contains("#Theta")) continue; // skip this one
74  if(titel.Contains("#Phi")) continue; // skip this one
75  //if(titel.Contains("kin")) continue; // skip this one
76  //if(titel.Contains("pull")&&!titel.Contains("full")) continue; // skip this one
77  //if(titel.Contains("Pull")) continue; // skip this one
78  if(vorname.EndsWith("the")||vorname.EndsWith("phi"))continue;
79 
80  if(titel.Contains("Momentum reco")) his->SetTitle("Momentum Smearing");
81  his->GetXaxis()->SetNoExponent(); // put exponents to numbers directly
82  his->GetYaxis()->SetNoExponent(); // put exponents to numbers directly
83  if( (titel.Contains("#chi")||titel.Contains("#Chi")) ){
84  if( titel.Contains("P") )
85  {
86  LineFitPlot(his);
87  } else
88  {
89  gPad->SetLogy();
90  his->Draw();
91  }
92  } else if( titel.Contains("error") )
93  {
94  his->Draw();
95  } else if( titel.Contains("CPU") )
96  {
97  //gPad->SetLogx();
98  his->SetFillColor(kAzure+2);
99  his->SetBarWidth(0.75);
100  his->SetBarOffset(0.125);
101  gStyle->SetPaintTextFormat("4.3f ms");
102  his->SetMarkerSize(2.);
103  his->Draw("BAR");
104  his->Draw("TEXT0 SAME");
105  } else {
106  GaussFitPlot(his);
107  }
108  } else continue;
109 
110  padcount++;
111  }
112 
113  can->Print(pic.Data());
114  pic = picname + "]"; // close ps
115  can->Print(pic.Data());
116  cout << "closed: " << pic.Data()<<endl;
117  TString convertcmd = "test -r ps2pdf && ps2pdf ";
118  convertcmd += pic.Data();
119  gSystem->Exec(convertcmd.Data());
120  return;
121 }
122 
123 
124 
125 int ccc=0; // counter to avoid bad naming
126 
127 void GaussFitPlot(TH1* his) {
128  Double_t par[3]={0.,0.,0.};
129  TString fitname="mygaus";fitname+=ccc;
130  TF1* fitted = new TF1(fitname.Data(),"gaus",-5,5);
131  //fitted->SetParameters(par);
132  cout<<" --- Fitting Gaussian to histogram "<<his->GetName()<<" \""<<his->GetTitle()<<"\" ---"<<endl;
133  fitted->SetLineColor(kAzure+2);
134  fitted->SetLineWidth(2);
135  fitted->SetLineStyle(2);
136  his->Fit(fitted);
137  fitted->GetParameters(par);
138  his->DrawCopy("");
139  Double_t mean = par[1];
140  Double_t sigma = par[2];
141  TString str="Gaussian Fit:";
142  DrawText( 0.2, 0.75, str.Data(),0.07,1);
143  str=Form("#mu = %.3g",mean);
144  DrawText( 0.2, 0.65, str.Data(),0.07,1);
145  str=Form("#sigma = %.3g",sigma);
146  DrawText( 0.2, 0.55, str.Data(),0.07,1);
147  ccc++;
148  return;
149 }
150 
151 
152 void LineFitPlot(TH1* his) {
153  range=1.2*his->GetMaximum();
154  his->SetAxisRange(0.,range,"Y");
155  Double_t par[2]={0.,0.};
156  TString fitname="myline";fitname+=ccc;
157  TF1* fitted = new TF1(fitname.Data(),"pol1");
158  cout<<" --- Fitting Line to histogram "<<his->GetName()<<" \""<<his->GetTitle()<<"\" ---"<<endl;
159  fitted->SetLineColor(kAzure+2);
160  fitted->SetLineWidth(2);
161  fitted->SetLineStyle(2);
162  his->Fit(fitted);
163  fitted->GetParameters(par);
164  his->DrawCopy("");
165  TString str="Line Fit:";
166  DrawText( 0.2, 0.425, str.Data(),0.07,1);
167  str=Form("const = %.3g",par[0]);
168  DrawText( 0.2, 0.325, str.Data(),0.07,1);
169  str=Form("slope = %.2g",par[1]);
170  DrawText( 0.2, 0.225, str.Data(),0.07,1);
171  ccc++;
172  return;
173 }
174 
void LineFitPlot(TH1 *his)
bool DrawText(Double_t posX=0., Double_t posY=0., const char *text="", Double_t size=0.08, Int_t col=1)
Definition: style/Tools.C:49
double range[]
Definition: evaltrig.C:67
Int_t i
Definition: run_full.C:25
TFile * file
plotandfithistosfromfile(TString filename="histos.root", TString ext=".pdf", Int_t divx=2, Int_t divy=2, Int_t pix=1200)
Double_t par[3]
Double_t sigma[nsteps]
Definition: dedx_bands.C:65
int drawhistosA4(TString filename="Data/HistoVertexing.root")
void GaussFitPlot(TH1 *his)
Double_t
TNtuple * ext
Definition: reco_muo.C:24
Double_t mean[nsteps]
Definition: dedx_bands.C:65
LoadPandaStyle()
const string filename