FairRoot/PandaRoot
drawhistos.C
Go to the documentation of this file.
1 
2 int drawhistos(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  //plothistosfromfile("HistoVertexing.root","pdf",3,3);
22  plotandfithistosfromfile(filename.Data(),"pdf",3,3);
23 
24  return 0;
25 
26 }
27 
28 
29 
30 plotandfithistosfromfile(TString filename = "histos.root", TString ext=".pdf", Int_t divx=2, Int_t divy=2, Int_t pix = 300)
31 { // Plot all histograms into a ps file
32  // works with TH1, TH2, & TProfile
33  TFile* file = new TFile(filename.Data());
34  if (!file) {cout<<"File \""<<filename.Data()<<"\" is not there..."<<endl;return;}
35  TCanvas* can = new TCanvas();
36  Int_t pixx = ceil(1.4*pix*divx);
37  Int_t pixy = pix*divy;
38  can->SetCanvasSize(pixx,pixy);
39  can->Divide(divx, divy);
40  TString picname = filename;
41  ext="-gfits."+ext;
42  ext.ReplaceAll("..",".");
43  picname.ReplaceAll(".root",ext); // ps, png, pdf ...
44  TString pic = picname + "["; // open empty ps
45  cout << "opening: " << pic.Data()<<endl;
46  can->Print(pic);
47  pic=picname;
48 
49  TList* list = file->GetListOfKeys();
50  if (!list) {cout<<"List not there..."<<endl;return;}
51  int padcount = 1;
52  TString keyclass="";
53  for(int i=0;i<list->GetEntries();i++)
54  {
55  if(padcount > divx*divy)
56  {
57  can->Print(pic.Data());
58  can->Clear();
59  can->SetCanvasSize(pixx,pixy);
60  can->Divide(divx, divy);
61  padcount=1;
62  }
63  can->cd(padcount);
64  TKey* key = (TKey*)list->At(i);
65  keyclass = key->GetClassName();
66  //cout<<keyclass.Data()<<endl;
67  if(keyclass.Contains("TH1"))
68  {
69  //cout<<"try plotting a TH1"<<endl;
70  TH1* his = (TH1*)key->ReadObj();
71  his->GetXaxis()->SetNoExponent(); // put exponents to numbers directly
72  his->GetYaxis()->SetNoExponent(); // put exponents to numbers directly
73  GaussFitPlot(his);
74  } else continue;
75 
76  padcount++;
77  }
78 
79  can->Print(pic.Data());
80  pic = picname + "]"; // close ps
81  can->Print(pic.Data());
82  cout << "closed: " << pic.Data()<<endl;
83  TString convertcmd = "test -r ps2pdf && ps2pdf ";
84  convertcmd += pic.Data();
85  gSystem->Exec(convertcmd.Data());
86  return;
87 }
88 
89 
90 
91 int ccc=0; // counter to avoid bad naming
92 
93 void GaussFitPlot(TH1* his) {
94  Double_t par[3]={0.,0.,0.};
95  TString fitname="mygaus";fitname+=ccc;
96  TF1* fitted = new TF1(fitname.Data(),"gaus",-5,5);
97  //fitted->SetParameters(par);
98  cout<<" --- Fitting Gaussian to histogram "<<his->GetName()<<" \""<<his->GetTitle()<<"\" ---"<<endl;
99  fitted->SetLineColor(4);
100  fitted->SetLineWidth(2);
101  fitted->SetLineStyle(2);
102  his->Fit(fitted);
103  fitted->GetParameters(par);
104  his->DrawCopy("");
105  Double_t mean = par[1];
106  Double_t sigma = par[2];
107  TString str="Gaussian Fit:";
108  DrawText( 0.2, 0.75, str.Data(),0.07,1);
109  str=Form("#mu = %.3g",mean);
110  DrawText( 0.2, 0.65, str.Data(),0.07,1);
111  str=Form("#sigma = %.3g",sigma);
112  DrawText( 0.2, 0.55, str.Data(),0.07,1);
113  ccc++;
114  return;
115 }
116 
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
Int_t i
Definition: run_full.C:25
TFile * file
Double_t par[3]
int ccc
Definition: drawhistos.C:91
Double_t sigma[nsteps]
Definition: dedx_bands.C:65
plotandfithistosfromfile(TString filename="histos.root", TString ext=".pdf", Int_t divx=2, Int_t divy=2, Int_t pix=300)
Definition: drawhistos.C:30
Double_t
void GaussFitPlot(TH1 *his)
Definition: drawhistos.C:93
int drawhistos(TString filename="Data/HistoVertexing.root")
Definition: drawhistos.C:2
TNtuple * ext
Definition: reco_muo.C:24
Double_t mean[nsteps]
Definition: dedx_bands.C:65
LoadPandaStyle()
const string filename