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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
#include <vector>
#include "TROOT.h"
#include "TFile.h"
#include "TDirectory.h"
#include "TChain.h"
#include "TObject.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TLegend.h"
#include "TGraph.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TTree.h"
#include "TF1.h"
#include "TPaveText.h"
#include "TPaveStats.h"
#include "tdrstyle.C"
int Color [] = {2,4,1,8,6,7,3,9,5};
int Marker[] = {21,22,23,20,29,3,2};
int Style [] = {1,2,5,7,9,10};
char buffer[2048];
char* FileName;
void Core(char* SavePath);
TObject* GetObjectFromPath(TDirectory* File, const char* Path);
void SaveCanvas(TCanvas* c, const char* path, const char* name, bool OnlyPPNG=false);
void DrawStatBox(TObject** Histos, std::vector<char*> legend, bool Mean , double X=0.15, double Y=0.93, double W=0.15, double H=0.03);
void DrawLegend (TObject** Histos, std::vector<char*> legend, char* Title, char* Style, double X=0.79, double Y=0.93, double W=0.20, double H=0.05);
void DrawSuperposedHistos(TH1D** Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax);
void DrawTH2D (TH2D** Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax);
void makePlot()
{
setTDRStyle();
gStyle->SetPadLeftMargin (0.18);
gStyle->SetPadTopMargin (0.05);
gStyle->SetPadBottomMargin(0.10);
gStyle->SetPadRightMargin (0.18);
gStyle->SetTitleSize(0.04, "XYZ");
gStyle->SetTitleXOffset(1.1);
gStyle->SetTitleYOffset(1.35);
gStyle->SetPalette(1);
gStyle->SetCanvasColor(0);
gStyle->SetBarOffset(0);
TCanvas* c1;
TObject** Histos = new TObject*[10];
std::vector<char*> legend;
FileName = "ProbaMap.root";
system("mkdir -p pictures");
Core("pictures/");
}
void Core(char* SavePath)
{
TFile* f1 = new TFile(FileName);
TH3F* Charge_Vs_Path3D = (TH3F*)GetObjectFromPath(f1,"Charge_Vs_Path");
for(int x=0;x<15;x++){
char xProjName[255];
if(x==0){
sprintf(xProjName,"%s","inc");
Charge_Vs_Path3D->GetXaxis()->SetRange(0,15);
}else{
sprintf(xProjName,"%02i",x);
Charge_Vs_Path3D->GetXaxis()->SetRange(x,x);
}
printf("---------------\n%s------------\n",xProjName);
string xProjNameStr(xProjName);
TH2D* Charge_Vs_Path2D = (TH2D*)Charge_Vs_Path3D->Project3D("zy");
double binMinA = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(4);
double binMaxA = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(6);
TH1D* Charge_Vs_PathA = (TH1D*)Charge_Vs_Path2D->ProjectionY("projA",4,6);
Charge_Vs_PathA->Rebin(2);
char ALegend[1024];sprintf(ALegend,"[%5.2f,%5.2f]",binMinA,binMaxA);
double binMinB = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(7);
double binMaxB = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(9);
TH1D* Charge_Vs_PathB = (TH1D*)Charge_Vs_Path2D->ProjectionY("projB",7,9);
Charge_Vs_PathB->Rebin(2);
char BLegend[1024];sprintf(BLegend,"[%5.2f,%5.2f]",binMinB,binMaxB);
double binMinC = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(10);
double binMaxC = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(12);
TH1D* Charge_Vs_PathC = (TH1D*)Charge_Vs_Path2D->ProjectionY("projC",10,12);
Charge_Vs_PathC->Rebin(2);
char CLegend[1024];sprintf(CLegend,"[%5.2f,%5.2f]",binMinC,binMaxC);
double binMinD = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(13);
double binMaxD = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(15);
TH1D* Charge_Vs_PathD = (TH1D*)Charge_Vs_Path2D->ProjectionY("projD",13,15);
Charge_Vs_PathD->Rebin(2);
char DLegend[1024];sprintf(DLegend,"[%5.2f,%5.2f]",binMinD,binMaxD);
printf("%f to %f\n",binMinA,binMaxA);
printf("%f to %f\n",binMinB,binMaxB);
printf("%f to %f\n",binMinC,binMaxC);
printf("%f to %f\n",binMinD,binMaxD);
TCanvas* c0;
TObject** Histos = new TObject*[10];
std::vector<char*> legend;
c0 = new TCanvas("c0", "c0", 600,600);
Charge_Vs_Path2D->SetTitle("");
Charge_Vs_Path2D->SetStats(kFALSE);
Charge_Vs_Path2D->GetXaxis()->SetTitle("pathlength (mm)");
Charge_Vs_Path2D->GetYaxis()->SetTitle("#Delta E/#Delta x (ADC/mm)");
Charge_Vs_Path2D->GetYaxis()->SetTitleOffset(1.80);
Charge_Vs_Path2D->Draw("COLZ");
c0->SetLogz(true);
SaveCanvas(c0,SavePath,(xProjNameStr+"_TH2").c_str());
delete c0;
//Compute Probability Map.
TH2D* Prob_ChargePath = new TH2D ("Prob_ChargePath" , "Prob_ChargePath" , Charge_Vs_Path2D->GetXaxis()->GetNbins(), Charge_Vs_Path2D->GetXaxis()->GetXmin(), Charge_Vs_Path2D->GetXaxis()->GetXmax(), Charge_Vs_Path2D->GetYaxis()->GetNbins(), Charge_Vs_Path2D->GetYaxis()->GetXmin(), Charge_Vs_Path2D->GetYaxis()->GetXmax());
for(int j=0;j<=Prob_ChargePath->GetXaxis()->GetNbins()+1;j++){
double Ni = 0;
for(int k=0;k<=Prob_ChargePath->GetYaxis()->GetNbins()+1;k++){ Ni+=Charge_Vs_Path2D->GetBinContent(j,k);}
for(int k=0;k<=Prob_ChargePath->GetYaxis()->GetNbins()+1;k++){
double tmp = 1E-10;
for(int l=0;l<=k;l++){ tmp+=Charge_Vs_Path2D->GetBinContent(j,l);}
if(Ni>0){
Prob_ChargePath->SetBinContent (j, k, tmp/Ni);
}else{
Prob_ChargePath->SetBinContent (j, k, 0);
}
}
}
c0 = new TCanvas("c0", "c0", 600,600);
Prob_ChargePath->SetTitle("Probability MIP(#DeltaE/#DeltaX) < Obs (#DeltaE/#DeltaX)");
Prob_ChargePath->SetStats(kFALSE);
Prob_ChargePath->GetXaxis()->SetTitle("pathlength (mm)");
Prob_ChargePath->GetYaxis()->SetTitle("Observed #DeltaE/#DeltaX (ADC/mm)");
Prob_ChargePath->GetYaxis()->SetTitleOffset(1.80);
Prob_ChargePath->GetXaxis()->SetRangeUser(0.28,1.2);
// Prob_ChargePath->GetYaxis()->SetRangeUser(0,1000);
Prob_ChargePath->Draw("COLZ");
//c0->SetLogz(true);
SaveCanvas(c0,SavePath,(xProjNameStr+"_TH2Proba").c_str());
delete c0;
c0 = new TCanvas("c1","c1,",600,600); legend.clear();
Histos[0] = Charge_Vs_PathA; legend.push_back(ALegend);
Histos[1] = Charge_Vs_PathB; legend.push_back(BLegend);
Histos[2] = Charge_Vs_PathC; legend.push_back(CLegend);
Histos[3] = Charge_Vs_PathD; legend.push_back(DLegend);
if(((TH1*)Histos[0])->Integral()>=1)((TH1*)Histos[0])->Scale(1/((TH1*)Histos[0])->Integral());
if(((TH1*)Histos[1])->Integral()>=1)((TH1*)Histos[1])->Scale(1/((TH1*)Histos[1])->Integral());
if(((TH1*)Histos[2])->Integral()>=1)((TH1*)Histos[2])->Scale(1/((TH1*)Histos[2])->Integral());
if(((TH1*)Histos[3])->Integral()>=1)((TH1*)Histos[3])->Scale(1/((TH1*)Histos[3])->Integral());
// DrawSuperposedHistos((TH1D**)Histos, legend, "", "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,1200, 0,0);
DrawSuperposedHistos((TH1D**)Histos, legend, "", "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,600, 0,0);
DrawLegend(Histos,legend,"PathLength (mm):","L");
c0->SetGridx(true);
Charge_Vs_PathA->GetXaxis()->SetNdivisions(520);
SaveCanvas(c0,SavePath,(xProjNameStr+"_TH1Linear").c_str());
delete c0;
c0 = new TCanvas("c1","c1,",600,600); legend.clear();
Histos[0] = Charge_Vs_PathA; legend.push_back(ALegend);
Histos[1] = Charge_Vs_PathB; legend.push_back(BLegend);
Histos[2] = Charge_Vs_PathC; legend.push_back(CLegend);
Histos[3] = Charge_Vs_PathD; legend.push_back(DLegend);
if(((TH1*)Histos[0])->Integral()>=1)((TH1*)Histos[0])->Scale(1/((TH1*)Histos[0])->Integral());
if(((TH1*)Histos[1])->Integral()>=1)((TH1*)Histos[1])->Scale(1/((TH1*)Histos[1])->Integral());
if(((TH1*)Histos[2])->Integral()>=1)((TH1*)Histos[2])->Scale(1/((TH1*)Histos[2])->Integral());
if(((TH1*)Histos[3])->Integral()>=1)((TH1*)Histos[3])->Scale(1/((TH1*)Histos[3])->Integral());
DrawSuperposedHistos((TH1D**)Histos, legend, "", "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,3000, 0,0);
// DrawLegend(Histos,legend,"PathLength (mm):","L");
c0->SetLogy(true);
SaveCanvas(c0,SavePath,(xProjNameStr+"_TH1").c_str());
delete c0;
delete Charge_Vs_Path2D;
delete Charge_Vs_PathA;
delete Charge_Vs_PathB;
delete Charge_Vs_PathC;
delete Charge_Vs_PathD;
delete Prob_ChargePath;
}
}
TObject* GetObjectFromPath(TDirectory* File, const char* Path)
{
string str(Path);
size_t pos = str.find("/");
if(pos < 256){
string firstPart = str.substr(0,pos);
string endPart = str.substr(pos+1,str.length());
TDirectory* TMP = (TDirectory*)File->Get(firstPart.c_str());
if(TMP!=NULL)return GetObjectFromPath(TMP,endPart.c_str());
printf("BUG\n");
return NULL;
}else{
return File->Get(Path);
}
}
void SaveCanvas(TCanvas* c, const char* path, const char* name, bool OnlyPPNG){
char buff[1024];
sprintf(buff,"%s/%s.png",path,name); c->SaveAs(buff); if(OnlyPPNG)return;
sprintf(buff,"%s/%s.eps",path,name); c->SaveAs(buff);
sprintf(buff,"%s/%s.C" ,path,name); c->SaveAs(buff);
}
void DrawLegend(TObject** Histos, std::vector<char*> legend, char* Title, char* Style, double X, double Y, double W, double H)
{
int N = legend.size();
if(strcmp(legend[0],"")!=0){
TLegend* leg;
leg = new TLegend(X,Y,X-W,Y - N*H);
leg->SetFillColor(0);
leg->SetBorderSize(0);
//leg->SetTextAlign(32);
if(strcmp(Title,"")!=0)leg->SetHeader(Title);
if(strcmp(Style,"DataMC")==0){
for(int i=0;i<N;i++){
TH2D* temp = (TH2D*)Histos[i];//->Clone();
temp->SetMarkerSize(1.3);
if(i==0){
leg->AddEntry(temp, legend[i] ,"P");
}else{
leg->AddEntry(temp, legend[i] ,"L");
}
}
}else{
for(int i=0;i<N;i++){
TH2D* temp = (TH2D*)Histos[i];//->Clone();
temp->SetMarkerSize(1.3);
leg->AddEntry(temp, legend[i] ,Style);
}
}
leg->Draw();
}
}
void DrawStatBox(TObject** Histos, std::vector<char*> legend, bool Mean, double X, double Y, double W, double H)
{
int N = legend.size();
char buffer[255];
if(Mean)H*=3;
for(int i=0;i<N;i++){
TPaveText* stat = new TPaveText(X,Y-(i*H), X+W, Y-(i+1)*H, "NDC");
TH1* Histo = (TH1*)Histos[i];
sprintf(buffer,"Entries : %i\n",(int)Histo->GetEntries());
stat->AddText(buffer);
if(Mean){
sprintf(buffer,"Mean : %6.2f\n",Histo->GetMean());
stat->AddText(buffer);
sprintf(buffer,"RMS : %6.2f\n",Histo->GetRMS());
stat->AddText(buffer);
}
stat->SetFillColor(0);
stat->SetLineColor(Color[i]);
stat->SetTextColor(Color[i]);
stat->SetBorderSize(0);
stat->SetMargin(0.05);
stat->SetTextAlign(12);
stat->Draw();
}
}
void DrawTH2D(TH2D** Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax)
{
int N = legend.size();
for(int i=0;i<N;i++){
if(!Histos[i])continue;
Histos[i]->SetTitle("");
Histos[i]->SetStats(kFALSE);
Histos[i]->GetXaxis()->SetTitle(Xlegend);
Histos[i]->GetYaxis()->SetTitle(Ylegend);
Histos[i]->GetYaxis()->SetTitleOffset(1.60);
if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
Histos[i]->SetMarkerStyle(Marker[i]);
Histos[i]->SetMarkerColor(Color[i]);
Histos[i]->SetMarkerSize(0.3);
}
char Buffer[256];
Histos[0]->Draw(Style);
for(int i=1;i<N;i++){
sprintf(Buffer,"%s same",Style);
Histos[i]->Draw(Buffer);
}
}
void DrawSuperposedHistos(TH1D** Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax)
{
int N = legend.size();
double HistoMax = -1;
int HistoHeighest = -1;
for(int i=0;i<N;i++){
if(!Histos[i])continue;
Histos[i]->SetTitle("");
Histos[i]->SetStats(kFALSE);
Histos[i]->GetXaxis()->SetTitle(Xlegend);
Histos[i]->GetYaxis()->SetTitle(Ylegend);
Histos[i]->GetXaxis()->SetTitleOffset(1.20);
Histos[i]->GetYaxis()->SetTitleOffset(1.70);
if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
Histos[i]->SetFillColor(0);
Histos[i]->SetMarkerStyle(Marker[i]);
Histos[i]->SetMarkerColor(Color[i]);
Histos[i]->SetMarkerSize(0.5);
Histos[i]->SetLineColor(Color[i]);
Histos[i]->SetLineWidth(2);
if(strcmp(Style,"DataMC")==0 && i==0){
Histos[i]->SetFillColor(0);
Histos[i]->SetMarkerStyle(20);
Histos[i]->SetMarkerColor(1);
Histos[i]->SetMarkerSize(1);
Histos[i]->SetLineColor(1);
Histos[i]->SetLineWidth(2);
}
if(Histos[i]->GetMaximum() >= HistoMax){
HistoMax = Histos[i]->GetMaximum();
HistoHeighest = i;
}
}
char Buffer[256];
if(strcmp(Style,"DataMC")==0){
if(HistoHeighest==0){
Histos[HistoHeighest]->Draw("E1");
}else{
Histos[HistoHeighest]->Draw("HIST");
}
for(int i=0;i<N;i++){
if(i==0){
Histos[i]->Draw("same E1");
}else{
Histos[i]->Draw("same");
}
}
}else{
Histos[HistoHeighest]->Draw(Style);
for(int i=0;i<N;i++){
if(strcmp(Style,"")!=0){
sprintf(Buffer,"same %s",Style);
}else{
sprintf(Buffer,"same");
}
Histos[i]->Draw(Buffer);
}
}
}
|