-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotter.C
165 lines (140 loc) · 5.28 KB
/
Plotter.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
The following macro compares the normaized histograms in 2 different root files having similar heirarchy structures.
Its based on the structure of hadd.C and will work on all the folders, subfolders in the root file no matter the hierarchy.
Running the script in ROOT:
1) .L PLotter.C
2) Compare_Histograms( "file1.root", "Legend1", "file2.root", "Legend2")
This will create a folder "Plots" with png, pdf and C formats of the comparison Histograms where the legend is "Legend1" and "Legend2" for file.root and file2.root respectively.
By default the x-axis limits are from the root files itself and the y-axis are normalized.
*/
void Recursion(TDirectory* , const char*, const char*, const char*, const char* );
void Compare_Histograms(const char* fileName1, const char* Legend1, const char* fileName2, const char* Legend2)
{
TString InputFile = Form("%s",fileName1);
TFile *file=new TFile(InputFile.Data());
TString target1=file->GetPath();
gSystem->Exec("mkdir Plots");
file->GetListOfKeys()->Print();
Recursion(file, fileName1, Legend1, fileName2, Legend2);
}
void Recursion(TDirectory* target, const char* filename1, const char* leg1, const char* filename2, const char* leg2)
{
TFile *f1=TFile::Open( filename1, "r" );
TString path( (char*)strstr( target->GetPath(), ":" ) );
path.Remove( 0, 2 );
f1->cd(path);
TDirectory *temp= gDirectory;
TIter next(temp->GetListOfKeys());
TKey *key;
TH1::SetDefaultSumw2(kTRUE);
Int_t colors[8] = {kBlack,kBlue,kRed,kGreen+2,kOrange,kMagenta,kCyan,kViolet};
//==========Setting Up Canvas==========//
TCanvas *c = new TCanvas("c", "c",754,169,800,756);
gStyle->SetOptStat(0); // Dont show stats
gStyle->SetOptTitle(0); // Dont show Title
c->SetFillColor(0);
c->SetBorderSize(2);
c->SetTickx(1);
c->SetTicky(1);
c->SetLeftMargin(0.17);
c->SetRightMargin(0.04761905);
c->SetTopMargin(0.07152683);
c->SetBottomMargin(0.1224209);
c->SetFrameLineWidth(3);
c->SetFrameFillStyle(0);
c->SetFrameLineWidth(2);
c->SetFrameLineWidth(3);
while ((key=(TKey*)next()))
{
TObject *obj = key->ReadObj();
// xxxxxxxxxxxxxxxxxxxx If the TObject is a TDirectory xxxxxxxxxxxxxxxxxxxxxxxxxxx //
if ( obj->IsA()->InheritsFrom( TDirectory::Class()))
{
std::cout << "Found subdirectory " << obj->GetName() <<std::endl;
TDirectory *subdir = (TDirectory*)obj;
std::cout<<subdir->GetPath()<<std::endl;
Recursion( subdir , filename1, leg1, filename2, leg2);
}
// xxxxxxxxxxxxxxxxxxxx If the TObject is a TH1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //
else if (obj->IsA()->InheritsFrom( TH1::Class() ))
{
TH1F *h1 = (TH1F*)obj;
TDirectory *HistDir = h1->GetDirectory();
TString DirPath( (char*)strstr( HistDir->GetPath(), ":" ) );
DirPath.Remove( 0, 2 );
TFile *file2=TFile::Open( filename2, "r" );
TString HistName = key->GetName();
TString cmd2 = DirPath+"/"+HistName;
TH1F *h2 = (TH1F*)file2->Get(cmd2.Data());
TGaxis::SetMaxDigits(3); //Setting the digit limits of axis labels
//=========Setting Up Legend==========//
TLegend *leg = new TLegend(0.74,0.70,0.90,0.88);
leg->SetHeader("Campaign","C");
leg->SetBorderSize(1);
leg->SetTextFont(62);
leg->SetTextSize(0.04);
leg->SetLineColor(0);
leg->SetLineStyle(1);
leg->SetLineWidth(1);
leg->SetFillColor(0);
leg->SetFillStyle(0);
// =========== Histogram Details =========== //
h1->GetXaxis()->SetLabelFont(42);
h1->GetXaxis()->SetLabelSize(0.05);
h1->GetXaxis()->SetTitleSize(0.05);
h1->GetXaxis()->SetTickLength(0.05);
h1->GetXaxis()->SetTitleFont(42);
h1->GetYaxis()->SetTitle("a.u.");
h1->GetYaxis()->SetLabelFont(42);
h1->GetYaxis()->SetLabelSize(0.05);
h1->GetYaxis()->SetTitleSize(0.06);
h1->GetYaxis()->SetTickLength(0.05);
h1->GetYaxis()->SetTitleFont(42);
// ============ Plotting ============== //
h1->SetLineColor(colors[1]);
h1->SetLineWidth(3);
h1->DrawNormalized("EHIST");
h2->SetLineColor(colors[2]);
h2->SetLineWidth(3);
h2->DrawNormalized("EHISTSame");
//============== Legend Entries ================
TLegendEntry *entry=leg->AddEntry(h1,leg2,"l");
entry->SetFillStyle(1001);
entry->SetLineColor(colors[1]);
entry->SetLineStyle(1);
entry->SetLineWidth(3);
entry->SetMarkerColor(colors[1]);
entry->SetMarkerStyle(1);
entry->SetMarkerSize(1);
entry->SetTextFont(42);
leg->Draw();
entry=leg->AddEntry(h2,leg1,"l");
entry->SetFillStyle(1001);
entry->SetLineColor(colors[2]);
entry->SetLineStyle(1);
entry->SetLineWidth(3);
entry->SetMarkerColor(colors[2]);
entry->SetMarkerStyle(1);
entry->SetMarkerSize(1);
entry->SetTextFont(42);
leg->Draw();
gPad->RedrawAxis();
//============== Saving as PDF, png and C ================
TString PlotFormat[] = {"2.png", ".pdf", "2.C"};
for(int k=0; k<3; k++)
{
TString Format = HistName + PlotFormat[k];
c->SaveAs(Format.Data());
TString cmd4 = "mv " + Format + " Plots/pdf";
gSystem->Exec(cmd4.Data());
}
c->Clear();
h1->Clear();
h2->Clear();
}
else // if the TObject is not of class TDirectory or TH1
{ std::cout<<"The class is not a TDirectory or TH1 !"<<std::endl;
}
} // while loop
c->Close();
} //Recursion Function