Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:32:30

0001 // Usage:
0002 // .L plotHcalSimHit.C+g
0003 //             For comparing basic histograms from Sim step between 2 sources
0004 //  plotCompare(infile1, infile2, text1, text2, type, save);
0005 //
0006 //  infile1, infile2  std::string  The ROOT files of the two input sources
0007 //  text1, text2      std::string  Character strings describing the two inputs
0008 //  type              int          Detector type (0:HB; 1:HE; 2:HO; 3:HF)
0009 //  save              bool         Flag to indicate the canvas to be saved
0010 //                                 as a pdf file (true) or not (false)
0011 //
0012 //////////////////////////////////////////////////////////////////////////////
0013 
0014 #include "TCanvas.h"
0015 #include "TDirectory.h"
0016 #include "TF1.h"
0017 #include "TFile.h"
0018 #include "TFitResult.h"
0019 #include "TGraph.h"
0020 #include "TGraphAsymmErrors.h"
0021 #include "TH1D.h"
0022 #include "TH2D.h"
0023 #include "THStack.h"
0024 #include "TLegend.h"
0025 #include "TMath.h"
0026 #include "TProfile.h"
0027 #include "TPaveStats.h"
0028 #include "TPaveText.h"
0029 #include "TROOT.h"
0030 #include "TString.h"
0031 #include "TStyle.h"
0032 
0033 #include <iostream>
0034 #include <iomanip>
0035 #include <vector>
0036 #include <string>
0037 
0038 void setTDRStyle() {
0039   TStyle *tdrStyle = new TStyle("tdrStyle", "Style for P-TDR");
0040 
0041   // For the canvas:
0042   tdrStyle->SetCanvasBorderMode(0);
0043   tdrStyle->SetCanvasColor(kWhite);
0044   tdrStyle->SetCanvasDefH(600);  //Height of canvas
0045   tdrStyle->SetCanvasDefW(600);  //Width of canvas
0046   tdrStyle->SetCanvasDefX(0);    //POsition on screen
0047   tdrStyle->SetCanvasDefY(0);
0048 
0049   // For the Pad:
0050   tdrStyle->SetPadBorderMode(0);
0051   // tdrStyle->SetPadBorderSize(Width_t size = 1);
0052   tdrStyle->SetPadColor(kWhite);
0053   tdrStyle->SetPadGridX(false);
0054   tdrStyle->SetPadGridY(false);
0055   tdrStyle->SetGridColor(0);
0056   tdrStyle->SetGridStyle(3);
0057   tdrStyle->SetGridWidth(1);
0058 
0059   // For the frame:
0060   tdrStyle->SetFrameBorderMode(0);
0061   tdrStyle->SetFrameBorderSize(1);
0062   tdrStyle->SetFrameFillColor(0);
0063   tdrStyle->SetFrameFillStyle(0);
0064   tdrStyle->SetFrameLineColor(1);
0065   tdrStyle->SetFrameLineStyle(1);
0066   tdrStyle->SetFrameLineWidth(1);
0067 
0068   //For the date:
0069   tdrStyle->SetOptDate(0);
0070   // tdrStyle->SetDateX(Float_t x = 0.01);
0071   // tdrStyle->SetDateY(Float_t y = 0.01);
0072 
0073   // For the statistics box:
0074   tdrStyle->SetOptFile(0);
0075   tdrStyle->SetOptStat("mr");
0076 
0077   // For the Global title:
0078 
0079   tdrStyle->SetOptTitle(0);
0080   tdrStyle->SetTitleFont(42);
0081   tdrStyle->SetTitleColor(1);
0082   tdrStyle->SetTitleTextColor(1);
0083   tdrStyle->SetTitleFillColor(10);
0084   tdrStyle->SetTitleFontSize(0.05);
0085 
0086   // For the axis titles:
0087   tdrStyle->SetTitleColor(1, "XYZ");
0088   tdrStyle->SetTitleFont(42, "XYZ");
0089   tdrStyle->SetTitleSize(0.06, "XYZ");
0090   // tdrStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
0091   // tdrStyle->SetTitleYSize(Float_t size = 0.02);
0092   tdrStyle->SetTitleXOffset(0.7);
0093   tdrStyle->SetTitleYOffset(0.7);
0094   // tdrStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
0095 
0096   // For the axis labels:
0097   tdrStyle->SetLabelColor(1, "XYZ");
0098   tdrStyle->SetLabelFont(42, "XYZ");
0099   tdrStyle->SetLabelOffset(0.007, "XYZ");
0100   tdrStyle->SetLabelSize(0.03, "XYZ");
0101 
0102   // For the axis:
0103   tdrStyle->SetAxisColor(1, "XYZ");
0104   tdrStyle->SetStripDecimals(kTRUE);
0105   tdrStyle->SetTickLength(0.03, "XYZ");
0106   tdrStyle->SetNdivisions(510, "XYZ");
0107   tdrStyle->SetPadTickX(1);  // To get tick marks on the opposite side of the frame
0108   tdrStyle->SetPadTickY(1);
0109 
0110   tdrStyle->cd();
0111 }
0112 
0113 void plotCompare(std::string infile1, std::string infile2, std::string text1, std::string text2, int type, bool save) {
0114   const int ndets = 4;
0115   const int nhist = 5;
0116   std::string names[ndets][nhist] = {{"Hit05", "Hit17", "Hit21", "Hit25", "Hit29"},
0117                                      {"Hit06", "Hit18", "Hit22", "Hit26", "Hit30"},
0118                                      {"Hit07", "Hit19", "Hit23", "Hit27", "Hit31"},
0119                                      {"Hit08", "Hit20", "Hit24", "Hit28", "Hit32"}};
0120   std::string xtitl[nhist] = {"Hits", "Depth", "i#eta", "i#phi", "Energy (MeV)"};
0121   std::string dets[ndets] = {"HB", "HE", "HO", "HF"};
0122   int nbins[nhist] = {10, 1, 1, 1, 1};
0123   bool logs[nhist] = {0, 0, 0, 0, 1};
0124   double xmax[nhist] = {2000, -1, -1, -1, 2.0};
0125   int colors[2] = {2, 4};
0126   int style[2] = {1, 2};
0127 
0128   if (type < 0 || type >= ndets)
0129     type = 0;
0130   setTDRStyle();
0131   TFile *file1 = new TFile(infile1.c_str());
0132   TFile *file2 = new TFile(infile2.c_str());
0133   if ((file1 != nullptr) && (file2 != nullptr)) {
0134     char name[20], cname[50];
0135     for (int k = 0; k < nhist; ++k) {
0136       sprintf(name, "%s", names[type][k].c_str());
0137       sprintf(cname, "c_%s", names[type][k].c_str());
0138       TH1D *hist[2];
0139       hist[0] = (TH1D *)(file1->FindObjectAny(name));
0140       hist[1] = (TH1D *)(file2->FindObjectAny(name));
0141       if ((hist[0] != nullptr) && (hist[1] != nullptr)) {
0142         TCanvas *pad = new TCanvas(cname, cname, 800, 600);
0143         pad->SetRightMargin(0.10);
0144         pad->SetTopMargin(0.10);
0145         if (logs[k] > 0)
0146           pad->SetLogy();
0147         TLegend *leg = new TLegend(0.65, 0.69, 0.89, 0.77);
0148         leg->SetFillColor(kWhite);
0149         double ymax(0.90);
0150         for (int i = 0; i < 2; ++i) {
0151           hist[i]->SetLineStyle(style[i]);
0152           hist[i]->SetLineColor(colors[i]);
0153           hist[i]->SetLineWidth(2);
0154           hist[i]->Rebin(nbins[k]);
0155           hist[i]->GetXaxis()->SetTitle(xtitl[k].c_str());
0156           if (xmax[k] > 0)
0157             hist[i]->GetXaxis()->SetRangeUser(0, xmax[k]);
0158           if (i == 0)
0159             hist[i]->Draw("HIST");
0160           else
0161             hist[i]->Draw("HIST sames");
0162           if (i == 0)
0163             sprintf(name, "%s", text1.c_str());
0164           else
0165             sprintf(name, "%s", text2.c_str());
0166           leg->AddEntry(hist[i], name, "lp");
0167           pad->Update();
0168           TPaveStats *st1 = (TPaveStats *)hist[i]->GetListOfFunctions()->FindObject("stats");
0169           if (st1 != nullptr) {
0170             double ymin = ymax - 0.06;
0171             st1->SetFillColor(kWhite);
0172             st1->SetLineColor(colors[i]);
0173             st1->SetTextColor(colors[i]);
0174             st1->SetY1NDC(ymin);
0175             st1->SetY2NDC(ymax);
0176             st1->SetX1NDC(0.70);
0177             st1->SetX2NDC(0.90);
0178             ymax = ymin;
0179           }
0180         }
0181         leg->Draw("same");
0182         pad->Update();
0183         TPaveText *txt1 = new TPaveText(0.65, 0.63, 0.89, 0.685, "blNDC");
0184         txt1->SetFillColor(kWhite);
0185         sprintf(name, "%s", dets[type].c_str());
0186         txt1->AddText(name);
0187         txt1->Draw("same");
0188         pad->Update();
0189         if (save) {
0190           sprintf(cname, "%s.pdf", pad->GetName());
0191           pad->Print(cname);
0192         }
0193       }
0194     }
0195   }
0196 }