|
| 1 | +#include <TFile.h> |
| 2 | +#include <TLatex.h> |
| 3 | +#include <TCanvas.h> |
| 4 | +#include <TLegend.h> |
| 5 | +#include <TStyle.h> |
| 6 | +#include <TGaxis.h> |
| 7 | +#include <string> |
| 8 | +#include <map> |
| 9 | +#include <TH1.h> |
| 10 | +#include <TH2.h> |
| 11 | +#include <TKey.h> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +/*--------------------------------------------------------------------*/ |
| 15 | +void cmsPrel(const TCanvas* canvas, float correction = 0.) { |
| 16 | + /*--------------------------------------------------------------------*/ |
| 17 | + |
| 18 | + // Create and draw the CMS text |
| 19 | + TLatex* latexCMS = new TLatex(canvas->GetLeftMargin(), 1.01 - canvas->GetTopMargin(), "#bf{CMS} #it{Preliminary}"); |
| 20 | + latexCMS->SetNDC(kTRUE); |
| 21 | + latexCMS->SetTextFont(42); |
| 22 | + latexCMS->SetTextSize(0.042); |
| 23 | + latexCMS->Draw(); |
| 24 | + |
| 25 | + // Create and draw the Internal (13 TeV) text |
| 26 | + TLatex* latexInternal = new TLatex( |
| 27 | + 1 - canvas->GetRightMargin() - correction, 1.01 - canvas->GetTopMargin(), "pp collisions (2022) 13.6 TeV"); |
| 28 | + latexInternal->SetNDC(kTRUE); |
| 29 | + latexInternal->SetTextAlign(31); |
| 30 | + latexInternal->SetTextFont(42); |
| 31 | + latexInternal->SetTextSize(0.042); |
| 32 | + latexInternal->Draw(); |
| 33 | +} |
| 34 | + |
| 35 | +/*--------------------------------------------------------------------*/ |
| 36 | +void adjustCanvasMargins(TCanvas* canvas) { |
| 37 | + /*--------------------------------------------------------------------*/ |
| 38 | + canvas->SetLeftMargin(0.14); |
| 39 | + canvas->SetRightMargin(0.04); |
| 40 | + canvas->SetTopMargin(0.06); |
| 41 | + canvas->SetBottomMargin(0.12); |
| 42 | +} |
| 43 | + |
| 44 | +/*--------------------------------------------------------------------*/ |
| 45 | +void adjustCanvasMargins2D(TCanvas* canvas) { |
| 46 | + /*--------------------------------------------------------------------*/ |
| 47 | + canvas->SetLeftMargin(0.10); |
| 48 | + canvas->SetRightMargin(0.185); |
| 49 | + canvas->SetTopMargin(0.06); |
| 50 | + canvas->SetBottomMargin(0.12); |
| 51 | +} |
| 52 | + |
| 53 | +// Function to modify axis title if it contains "phi" |
| 54 | +/*--------------------------------------------------------------------*/ |
| 55 | +void modifyAxisTitle(TH1* hist, const TString& axisName) |
| 56 | +/*--------------------------------------------------------------------*/ |
| 57 | +{ |
| 58 | + // Get the current axis title |
| 59 | + TString axisTitle; |
| 60 | + if (axisName == "X") { |
| 61 | + axisTitle = hist->GetXaxis()->GetTitle(); |
| 62 | + } else if (axisName == "Y") { |
| 63 | + axisTitle = hist->GetYaxis()->GetTitle(); |
| 64 | + } else { |
| 65 | + std::cerr << "Invalid axis name: " << axisName << ". Use 'X' or 'Y'." << std::endl; |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Convert to lower case for case-insensitive comparison |
| 70 | + TString axisTitleLower = axisTitle; |
| 71 | + axisTitleLower.ToLower(); |
| 72 | + |
| 73 | + // Check if "phi" is in the axis title |
| 74 | + if (axisTitleLower.Contains("phi")) { |
| 75 | + // Append " [rad]" if "phi" is found |
| 76 | + axisTitle += " [rad]"; |
| 77 | + if (axisName == "X") { |
| 78 | + hist->GetXaxis()->SetTitle(axisTitle); |
| 79 | + } else if (axisName == "Y") { |
| 80 | + hist->GetYaxis()->SetTitle(axisTitle); |
| 81 | + } |
| 82 | + std::cout << "Updated " << axisName << "-axis title to: " << axisTitle << std::endl; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/*--------------------------------------------------------------------*/ |
| 87 | +void makeNicePlotStyle(TH1* hist, int color, int markerStyle) |
| 88 | +/*--------------------------------------------------------------------*/ |
| 89 | +{ |
| 90 | + hist->SetStats(kFALSE); |
| 91 | + hist->SetLineWidth(2); |
| 92 | + hist->SetLineColor(color); |
| 93 | + hist->SetMarkerColor(color); |
| 94 | + hist->SetMarkerStyle(markerStyle); |
| 95 | + //hist->GetXaxis()->CenterTitle(true); |
| 96 | + //hist->GetYaxis()->CenterTitle(true); |
| 97 | + hist->GetXaxis()->SetTitleFont(42); |
| 98 | + hist->GetYaxis()->SetTitleFont(42); |
| 99 | + hist->GetXaxis()->SetTitleSize(0.05); |
| 100 | + hist->GetYaxis()->SetTitleSize(0.05); |
| 101 | + hist->GetZaxis()->SetTitleSize(0.05); |
| 102 | + hist->GetXaxis()->SetTitleOffset(1.2); |
| 103 | + if (hist->InheritsFrom("TH2")) { |
| 104 | + hist->GetYaxis()->SetTitleOffset(1.0); |
| 105 | + } else { |
| 106 | + hist->GetYaxis()->SetTitleOffset(1.3); |
| 107 | + } |
| 108 | + hist->GetZaxis()->SetTitleOffset(1.3); |
| 109 | + hist->GetXaxis()->SetLabelFont(42); |
| 110 | + hist->GetYaxis()->SetLabelFont(42); |
| 111 | + hist->GetYaxis()->SetLabelSize(.05); |
| 112 | + hist->GetXaxis()->SetLabelSize(.05); |
| 113 | + hist->GetZaxis()->SetLabelSize(.05); |
| 114 | + |
| 115 | + // Modify the axis titles if they contain "phi" |
| 116 | + modifyAxisTitle(hist, "X"); |
| 117 | + modifyAxisTitle(hist, "Y"); |
| 118 | + |
| 119 | + TGaxis::SetExponentOffset(-0.1, 0.01, "y"); // Y offset |
| 120 | +} |
| 121 | + |
| 122 | +/*--------------------------------------------------------------------*/ |
| 123 | +std::pair<Double_t, Double_t> getExtrema(TObjArray* array) |
| 124 | +/*--------------------------------------------------------------------*/ |
| 125 | +{ |
| 126 | + Double_t theMaximum = (static_cast<TH1*>(array->At(0)))->GetMaximum(); |
| 127 | + Double_t theMinimum = (static_cast<TH1*>(array->At(0)))->GetMinimum(); |
| 128 | + for (Int_t i = 0; i < array->GetSize(); i++) { |
| 129 | + if ((static_cast<TH1*>(array->At(i)))->GetMaximum() > theMaximum) { |
| 130 | + theMaximum = (static_cast<TH1*>(array->At(i)))->GetMaximum(); |
| 131 | + } |
| 132 | + if ((static_cast<TH1*>(array->At(i)))->GetMinimum() < theMinimum) { |
| 133 | + theMinimum = (static_cast<TH1*>(array->At(i)))->GetMinimum(); |
| 134 | + } |
| 135 | + } |
| 136 | + return std::make_pair(theMinimum, theMaximum); |
| 137 | +} |
| 138 | + |
| 139 | +void overlayHistograms(const std::vector<std::string>& fileNames, |
| 140 | + const std::vector<string>& labels, |
| 141 | + const std::string& type) { |
| 142 | + gStyle->SetOptTitle(0); |
| 143 | + |
| 144 | + // Create a new canvas for each histogram |
| 145 | + TCanvas* c = nullptr; |
| 146 | + |
| 147 | + std::vector<int> colors = {kBlack, kRed, kBlue, kGreen, kAzure, kYellow}; |
| 148 | + std::vector<int> markers = {20, 21, 22, 23, 24, 25}; |
| 149 | + |
| 150 | + // Map to store histograms with the same name |
| 151 | + std::map<std::string, std::vector<TH1*>> histMap; |
| 152 | + std::map<std::string, std::vector<TH2*>> hist2DMap; |
| 153 | + |
| 154 | + // Loop over all the input files |
| 155 | + for (const auto& fileName : fileNames) { |
| 156 | + // Open the input file |
| 157 | + TFile* file = TFile::Open(fileName.c_str()); |
| 158 | + if (!file || file->IsZombie()) { |
| 159 | + std::cerr << "Could not open file " << fileName << std::endl; |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + // Loop over all histograms in the directory |
| 164 | + TIter nexthist(file->GetListOfKeys()); |
| 165 | + TKey* key = nullptr; |
| 166 | + while ((key = static_cast<TKey*>(nexthist()))) { |
| 167 | + TObject* obj = key->ReadObj(); |
| 168 | + if (obj->InheritsFrom(TH1::Class())) { |
| 169 | + TH1* hist = static_cast<TH1*>(obj); |
| 170 | + std::string histName = hist->GetName(); |
| 171 | + std::cout << "pushing back: " << histName << std::endl; |
| 172 | + if (!obj->InheritsFrom(TH2::Class())) { |
| 173 | + histMap[histName].push_back(hist); |
| 174 | + } else { |
| 175 | + TH2* hist = static_cast<TH2*>(obj); |
| 176 | + hist2DMap[histName].push_back(hist); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + // Close the input file |
| 182 | + //file->Close(); |
| 183 | + |
| 184 | + // Loop over the histograms in the map |
| 185 | + for (const auto& histPair : histMap) { |
| 186 | + const std::string& histName = histPair.first; |
| 187 | + const std::vector<TH1*>& histVec = histPair.second; |
| 188 | + |
| 189 | + if (histName.find("delta_iter_") != std::string::npos) |
| 190 | + continue; |
| 191 | + |
| 192 | + // Create a new canvas for the histogram |
| 193 | + c = new TCanvas((histName + type).c_str(), histName.c_str(), 800, 800); |
| 194 | + |
| 195 | + //if(histName.find("Delta") != std::string::npos) { |
| 196 | + // c->SetLogy(); |
| 197 | + //} |
| 198 | + |
| 199 | + adjustCanvasMargins(c); |
| 200 | + |
| 201 | + TObjArray* array = new TObjArray(histVec.size()); |
| 202 | + for (const auto& histo : histVec) { |
| 203 | + array->Add(histo); |
| 204 | + } |
| 205 | + std::pair<Double_t, Double_t> extrema = getExtrema(array); |
| 206 | + const auto& DELTA = std::abs(extrema.second - extrema.first); |
| 207 | + delete array; |
| 208 | + |
| 209 | + // Draw the first histogram |
| 210 | + histVec[0]->Draw(); |
| 211 | + makeNicePlotStyle(histVec[0], kBlack, 20); |
| 212 | + |
| 213 | + if ((histName.find("avg") != std::string::npos)) { |
| 214 | + histVec[0]->SetMinimum(extrema.first - DELTA / 3.); |
| 215 | + histVec[0]->SetMaximum(extrema.second + DELTA / 3.); |
| 216 | + } else if (histName.find("RMS") != std::string::npos) { |
| 217 | + histVec[0]->SetMinimum(0.); |
| 218 | + histVec[0]->SetMaximum(extrema.second + DELTA / 3.); |
| 219 | + } else if (histName.find("Correction") != std::string::npos) { |
| 220 | + histVec[0]->SetMinimum(extrema.first - DELTA / 3.); |
| 221 | + histVec[0]->SetMaximum(extrema.second + DELTA / 3.); |
| 222 | + } else { |
| 223 | + histVec[0]->SetMaximum(extrema.second + DELTA / 3.); |
| 224 | + } |
| 225 | + |
| 226 | + // Loop over the remaining histograms and overlay them |
| 227 | + for (size_t i = 1; i < histVec.size(); ++i) { |
| 228 | + histVec[i]->Draw("SAME"); |
| 229 | + makeNicePlotStyle(histVec[i], colors[i], markers[i]); |
| 230 | + } |
| 231 | + |
| 232 | + // Draw the legend |
| 233 | + TLegend* infoBox = new TLegend(0.44, 0.80, 0.94, 0.93, ""); |
| 234 | + infoBox->SetShadowColor(0); // 0 = transparent |
| 235 | + infoBox->SetBorderSize(0); // 0 = transparent |
| 236 | + infoBox->SetFillColor(kWhite); |
| 237 | + infoBox->SetTextSize(0.035); |
| 238 | + |
| 239 | + for (unsigned int i = 0; i < histVec.size(); i++) { |
| 240 | + infoBox->AddEntry(histVec[i], labels[i].c_str(), "PL"); |
| 241 | + } |
| 242 | + infoBox->Draw("same"); |
| 243 | + |
| 244 | + TLatex* latex = new TLatex(); |
| 245 | + latex->SetTextAlign(22); |
| 246 | + latex->SetTextSize(0.045); |
| 247 | + |
| 248 | + //latex->DrawLatexNDC(0.75, 0.85, "Z^{0} #rightarrow#mu^{+}#mu^{-}"); |
| 249 | + cmsPrel(c); |
| 250 | + |
| 251 | + // Update the canvas |
| 252 | + c->Update(); |
| 253 | + c->SaveAs((histName + "_" + type + ".png").c_str()); |
| 254 | + c->SaveAs((histName + "_" + type + ".pdf").c_str()); |
| 255 | + c->SaveAs((histName + "_" + type + ".eps").c_str()); |
| 256 | + c->SaveAs((histName + "_" + type + ".root").c_str()); |
| 257 | + } |
| 258 | + |
| 259 | + //gStyle->SetPalette(kRainbow); |
| 260 | + //gStyle->SetPalette(kWaterMelon); |
| 261 | + gStyle->SetPalette(kTemperatureMap); |
| 262 | + //gStyle->SetPalette(kViridis); |
| 263 | + |
| 264 | + // Loop over the 2D histograms in the map |
| 265 | + for (const auto& entry : hist2DMap) { |
| 266 | + const std::string& histName = entry.first; |
| 267 | + const std::vector<TH2*>& histList = entry.second; |
| 268 | + |
| 269 | + if (histList.empty()) { |
| 270 | + std::cerr << "No histograms found for " << histName << std::endl; |
| 271 | + continue; |
| 272 | + } |
| 273 | + |
| 274 | + TObjArray* array = new TObjArray(histList.size()); |
| 275 | + for (const auto& histo : histList) { |
| 276 | + array->Add(histo); |
| 277 | + } |
| 278 | + |
| 279 | + std::pair<Double_t, Double_t> extrema = getExtrema(array); |
| 280 | + delete array; |
| 281 | + |
| 282 | + TCanvas* c = new TCanvas((histName + type).c_str(), histName.c_str(), 900 * histList.size(), 800); |
| 283 | + c->Divide(histList.size(), 1); |
| 284 | + |
| 285 | + for (size_t i = 0; i < histList.size(); ++i) { |
| 286 | + c->cd(i + 1); |
| 287 | + auto current_pad = static_cast<TCanvas*>(gPad); |
| 288 | + adjustCanvasMargins2D(current_pad); |
| 289 | + makeNicePlotStyle(histList[i], kWhite, 0); |
| 290 | + histList[i]->SetMinimum(extrema.first); |
| 291 | + histList[i]->SetMaximum(extrema.second); |
| 292 | + histList[i]->Draw("COLZ"); |
| 293 | + cmsPrel(current_pad, -0.062); |
| 294 | + } |
| 295 | + |
| 296 | + // Update the canvas |
| 297 | + c->Update(); |
| 298 | + c->SaveAs((histName + "_" + type + "_side_by_side.png").c_str()); |
| 299 | + c->SaveAs((histName + "_" + type + "_side_by_side.pdf").c_str()); |
| 300 | + c->SaveAs((histName + "_" + type + "_side_by_side.eps").c_str()); |
| 301 | + c->SaveAs((histName + "_" + type + "_side_by_side.root").c_str()); |
| 302 | + |
| 303 | + for (size_t i = 0; i < histList.size(); ++i) { |
| 304 | + TCanvas* c2 = new TCanvas((histName + type + labels[i]).c_str(), histName.c_str(), 900, 800); |
| 305 | + c2->cd(); |
| 306 | + auto current_pad = static_cast<TCanvas*>(gPad); |
| 307 | + adjustCanvasMargins2D(current_pad); |
| 308 | + makeNicePlotStyle(histList[i], kWhite, 0); |
| 309 | + histList[i]->Draw("COLZ"); |
| 310 | + cmsPrel(c2, -0.050); |
| 311 | + std::string result = labels[i]; |
| 312 | + std::replace(result.begin(), result.end(), ' ', '_'); |
| 313 | + c2->SaveAs((histName + "_" + type + "_" + result + ".png").c_str()); |
| 314 | + c2->SaveAs((histName + "_" + type + "_" + result + ".pdf").c_str()); |
| 315 | + c2->SaveAs((histName + "_" + type + "_" + result + ".eps").c_str()); |
| 316 | + c2->SaveAs((histName + "_" + type + "_" + result + ".root").c_str()); |
| 317 | + } |
| 318 | + } |
| 319 | +} |
| 320 | + |
| 321 | +void overlayDiMuonBiases() { |
| 322 | + std::vector<std::string> fileNames = {"histos_asInDataTaking_DiMuonAnalysisResults_Run2022D-v1__d0__FINE.root", |
| 323 | + "histos_Run3ReReco_DiMuonAnalysisResults_Run2022D-v1__d0.root"}; |
| 324 | + std::vector<std::string> labels = {"fine", "fast"}; // Add your ROOT file names here |
| 325 | + |
| 326 | + overlayHistograms(fileNames, labels, "d0"); |
| 327 | + |
| 328 | + //fileNames.clear(); |
| 329 | + //fileNames = {"histos_asInDataTaking_DiMuonAnalysisResults_Run2022__dz.root","histos_Run3ReReco_DiMuonAnalysisResults_Run2022__dz.root"}; |
| 330 | + //overlayHistograms(fileNames,labels,"dz"); |
| 331 | +} |
0 commit comments