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
|
#include <TFile.h>
#include <TLatex.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TStyle.h>
#include <TGaxis.h>
#include <string>
#include <map>
#include <TH1.h>
#include <TH2.h>
#include <TKey.h>
#include <iostream>
/*--------------------------------------------------------------------*/
void cmsPrel(const TCanvas* canvas, float correction = 0.) {
/*--------------------------------------------------------------------*/
// Create and draw the CMS text
TLatex* latexCMS = new TLatex(canvas->GetLeftMargin(), 1.01 - canvas->GetTopMargin(), "#bf{CMS} #it{Preliminary}");
latexCMS->SetNDC(kTRUE);
latexCMS->SetTextFont(42);
latexCMS->SetTextSize(0.042);
latexCMS->Draw();
// Create and draw the Internal (13 TeV) text
TLatex* latexInternal = new TLatex(
1 - canvas->GetRightMargin() - correction, 1.01 - canvas->GetTopMargin(), "pp collisions (2022) 13.6 TeV");
latexInternal->SetNDC(kTRUE);
latexInternal->SetTextAlign(31);
latexInternal->SetTextFont(42);
latexInternal->SetTextSize(0.042);
latexInternal->Draw();
}
/*--------------------------------------------------------------------*/
void adjustCanvasMargins(TCanvas* canvas) {
/*--------------------------------------------------------------------*/
canvas->SetLeftMargin(0.14);
canvas->SetRightMargin(0.04);
canvas->SetTopMargin(0.06);
canvas->SetBottomMargin(0.12);
}
/*--------------------------------------------------------------------*/
void adjustCanvasMargins2D(TCanvas* canvas) {
/*--------------------------------------------------------------------*/
canvas->SetLeftMargin(0.10);
canvas->SetRightMargin(0.185);
canvas->SetTopMargin(0.06);
canvas->SetBottomMargin(0.12);
}
// Function to modify axis title if it contains "phi"
/*--------------------------------------------------------------------*/
void modifyAxisTitle(TH1* hist, const TString& axisName)
/*--------------------------------------------------------------------*/
{
// Get the current axis title
TString axisTitle;
if (axisName == "X") {
axisTitle = hist->GetXaxis()->GetTitle();
} else if (axisName == "Y") {
axisTitle = hist->GetYaxis()->GetTitle();
} else {
std::cerr << "Invalid axis name: " << axisName << ". Use 'X' or 'Y'." << std::endl;
return;
}
// Convert to lower case for case-insensitive comparison
TString axisTitleLower = axisTitle;
axisTitleLower.ToLower();
// Check if "phi" is in the axis title
if (axisTitleLower.Contains("phi")) {
// Append " [rad]" if "phi" is found
axisTitle += " [rad]";
if (axisName == "X") {
hist->GetXaxis()->SetTitle(axisTitle);
} else if (axisName == "Y") {
hist->GetYaxis()->SetTitle(axisTitle);
}
std::cout << "Updated " << axisName << "-axis title to: " << axisTitle << std::endl;
}
}
/*--------------------------------------------------------------------*/
void makeNicePlotStyle(TH1* hist, int color, int markerStyle)
/*--------------------------------------------------------------------*/
{
hist->SetStats(kFALSE);
hist->SetLineWidth(2);
hist->SetLineColor(color);
hist->SetMarkerColor(color);
hist->SetMarkerStyle(markerStyle);
//hist->GetXaxis()->CenterTitle(true);
//hist->GetYaxis()->CenterTitle(true);
hist->GetXaxis()->SetTitleFont(42);
hist->GetYaxis()->SetTitleFont(42);
hist->GetXaxis()->SetTitleSize(0.05);
hist->GetYaxis()->SetTitleSize(0.05);
hist->GetZaxis()->SetTitleSize(0.05);
hist->GetXaxis()->SetTitleOffset(1.2);
if (hist->InheritsFrom("TH2")) {
hist->GetYaxis()->SetTitleOffset(1.0);
} else {
hist->GetYaxis()->SetTitleOffset(1.3);
}
hist->GetZaxis()->SetTitleOffset(1.3);
hist->GetXaxis()->SetLabelFont(42);
hist->GetYaxis()->SetLabelFont(42);
hist->GetYaxis()->SetLabelSize(.05);
hist->GetXaxis()->SetLabelSize(.05);
hist->GetZaxis()->SetLabelSize(.05);
// Modify the axis titles if they contain "phi"
modifyAxisTitle(hist, "X");
modifyAxisTitle(hist, "Y");
TGaxis::SetExponentOffset(-0.1, 0.01, "y"); // Y offset
}
/*--------------------------------------------------------------------*/
std::pair<Double_t, Double_t> getExtrema(TObjArray* array)
/*--------------------------------------------------------------------*/
{
Double_t theMaximum = (static_cast<TH1*>(array->At(0)))->GetMaximum();
Double_t theMinimum = (static_cast<TH1*>(array->At(0)))->GetMinimum();
for (Int_t i = 0; i < array->GetSize(); i++) {
if ((static_cast<TH1*>(array->At(i)))->GetMaximum() > theMaximum) {
theMaximum = (static_cast<TH1*>(array->At(i)))->GetMaximum();
}
if ((static_cast<TH1*>(array->At(i)))->GetMinimum() < theMinimum) {
theMinimum = (static_cast<TH1*>(array->At(i)))->GetMinimum();
}
}
return std::make_pair(theMinimum, theMaximum);
}
void overlayHistograms(const std::vector<std::string>& fileNames,
const std::vector<string>& labels,
const std::string& type) {
gStyle->SetOptTitle(0);
// Create a new canvas for each histogram
TCanvas* c = nullptr;
std::vector<int> colors = {kBlack, kRed, kBlue, kGreen, kAzure, kYellow};
std::vector<int> markers = {20, 21, 22, 23, 24, 25};
// Map to store histograms with the same name
std::map<std::string, std::vector<TH1*>> histMap;
std::map<std::string, std::vector<TH2*>> hist2DMap;
// Loop over all the input files
for (const auto& fileName : fileNames) {
// Open the input file
TFile* file = TFile::Open(fileName.c_str());
if (!file || file->IsZombie()) {
std::cerr << "Could not open file " << fileName << std::endl;
continue;
}
// Loop over all histograms in the directory
TIter nexthist(file->GetListOfKeys());
TKey* key = nullptr;
while ((key = static_cast<TKey*>(nexthist()))) {
TObject* obj = key->ReadObj();
if (obj->InheritsFrom(TH1::Class())) {
TH1* hist = static_cast<TH1*>(obj);
std::string histName = hist->GetName();
std::cout << "pushing back: " << histName << std::endl;
if (!obj->InheritsFrom(TH2::Class())) {
histMap[histName].push_back(hist);
} else {
TH2* hist = static_cast<TH2*>(obj);
hist2DMap[histName].push_back(hist);
}
}
}
}
// Close the input file
//file->Close();
// Loop over the histograms in the map
for (const auto& histPair : histMap) {
const std::string& histName = histPair.first;
const std::vector<TH1*>& histVec = histPair.second;
if (histName.find("delta_iter_") != std::string::npos)
continue;
// Create a new canvas for the histogram
c = new TCanvas((histName + type).c_str(), histName.c_str(), 800, 800);
//if(histName.find("Delta") != std::string::npos) {
// c->SetLogy();
//}
adjustCanvasMargins(c);
TObjArray* array = new TObjArray(histVec.size());
for (const auto& histo : histVec) {
array->Add(histo);
}
std::pair<Double_t, Double_t> extrema = getExtrema(array);
const auto& DELTA = std::abs(extrema.second - extrema.first);
delete array;
// Draw the first histogram
histVec[0]->Draw();
makeNicePlotStyle(histVec[0], kBlack, 20);
if ((histName.find("avg") != std::string::npos)) {
histVec[0]->SetMinimum(extrema.first - DELTA / 3.);
histVec[0]->SetMaximum(extrema.second + DELTA / 3.);
} else if (histName.find("RMS") != std::string::npos) {
histVec[0]->SetMinimum(0.);
histVec[0]->SetMaximum(extrema.second + DELTA / 3.);
} else if (histName.find("Correction") != std::string::npos) {
histVec[0]->SetMinimum(extrema.first - DELTA / 3.);
histVec[0]->SetMaximum(extrema.second + DELTA / 3.);
} else {
histVec[0]->SetMaximum(extrema.second + DELTA / 3.);
}
// Loop over the remaining histograms and overlay them
for (size_t i = 1; i < histVec.size(); ++i) {
histVec[i]->Draw("SAME");
makeNicePlotStyle(histVec[i], colors[i], markers[i]);
}
// Draw the legend
TLegend* infoBox = new TLegend(0.44, 0.80, 0.94, 0.93, "");
infoBox->SetShadowColor(0); // 0 = transparent
infoBox->SetBorderSize(0); // 0 = transparent
infoBox->SetFillColor(kWhite);
infoBox->SetTextSize(0.035);
for (unsigned int i = 0; i < histVec.size(); i++) {
infoBox->AddEntry(histVec[i], labels[i].c_str(), "PL");
}
infoBox->Draw("same");
TLatex* latex = new TLatex();
latex->SetTextAlign(22);
latex->SetTextSize(0.045);
//latex->DrawLatexNDC(0.75, 0.85, "Z^{0} #rightarrow#mu^{+}#mu^{-}");
cmsPrel(c);
// Update the canvas
c->Update();
c->SaveAs((histName + "_" + type + ".png").c_str());
c->SaveAs((histName + "_" + type + ".pdf").c_str());
c->SaveAs((histName + "_" + type + ".eps").c_str());
c->SaveAs((histName + "_" + type + ".root").c_str());
}
//gStyle->SetPalette(kRainbow);
//gStyle->SetPalette(kWaterMelon);
gStyle->SetPalette(kTemperatureMap);
//gStyle->SetPalette(kViridis);
// Loop over the 2D histograms in the map
for (const auto& entry : hist2DMap) {
const std::string& histName = entry.first;
const std::vector<TH2*>& histList = entry.second;
if (histList.empty()) {
std::cerr << "No histograms found for " << histName << std::endl;
continue;
}
TObjArray* array = new TObjArray(histList.size());
for (const auto& histo : histList) {
array->Add(histo);
}
std::pair<Double_t, Double_t> extrema = getExtrema(array);
delete array;
TCanvas* c = new TCanvas((histName + type).c_str(), histName.c_str(), 900 * histList.size(), 800);
c->Divide(histList.size(), 1);
for (size_t i = 0; i < histList.size(); ++i) {
c->cd(i + 1);
auto current_pad = static_cast<TCanvas*>(gPad);
adjustCanvasMargins2D(current_pad);
makeNicePlotStyle(histList[i], kWhite, 0);
histList[i]->SetMinimum(extrema.first);
histList[i]->SetMaximum(extrema.second);
histList[i]->Draw("COLZ");
cmsPrel(current_pad, -0.062);
}
// Update the canvas
c->Update();
c->SaveAs((histName + "_" + type + "_side_by_side.png").c_str());
c->SaveAs((histName + "_" + type + "_side_by_side.pdf").c_str());
c->SaveAs((histName + "_" + type + "_side_by_side.eps").c_str());
c->SaveAs((histName + "_" + type + "_side_by_side.root").c_str());
for (size_t i = 0; i < histList.size(); ++i) {
TCanvas* c2 = new TCanvas((histName + type + labels[i]).c_str(), histName.c_str(), 900, 800);
c2->cd();
auto current_pad = static_cast<TCanvas*>(gPad);
adjustCanvasMargins2D(current_pad);
makeNicePlotStyle(histList[i], kWhite, 0);
histList[i]->Draw("COLZ");
cmsPrel(c2, -0.050);
std::string result = labels[i];
std::replace(result.begin(), result.end(), ' ', '_');
c2->SaveAs((histName + "_" + type + "_" + result + ".png").c_str());
c2->SaveAs((histName + "_" + type + "_" + result + ".pdf").c_str());
c2->SaveAs((histName + "_" + type + "_" + result + ".eps").c_str());
c2->SaveAs((histName + "_" + type + "_" + result + ".root").c_str());
}
}
}
void overlayDiMuonBiases() {
std::vector<std::string> fileNames = {"histos_asInDataTaking_DiMuonAnalysisResults_Run2022D-v1__d0__FINE.root",
"histos_Run3ReReco_DiMuonAnalysisResults_Run2022D-v1__d0.root"};
std::vector<std::string> labels = {"fine", "fast"}; // Add your ROOT file names here
overlayHistograms(fileNames, labels, "d0");
//fileNames.clear();
//fileNames = {"histos_asInDataTaking_DiMuonAnalysisResults_Run2022__dz.root","histos_Run3ReReco_DiMuonAnalysisResults_Run2022__dz.root"};
//overlayHistograms(fileNames,labels,"dz");
}
|