CMS 3D CMS Logo

offsetStack.cc
Go to the documentation of this file.
1 #include "TString.h"
2 #include "TFile.h"
3 #include "TProfile.h"
4 #include "TH1.h"
5 #include "THStack.h"
6 #include "TCanvas.h"
7 #include "TStyle.h"
8 #include "TLegend.h"
9 #include "TLatex.h"
10 
11 #include <iostream>
12 #include <map>
13 
14 bool getHists(std::map<TString, TH1D*>& hists, TFile*& file, TString var, int var_val, float r);
15 void setStyle();
16 void setStack(THStack*& stack, std::map<TString, TH1D*>& hists);
17 void split(const TString& str, TString& sub1, TString& sub2);
18 
19 int main(int argc, char* argv[]) {
20  if (argc < 5) {
21  std::cout << "Please provide a label:filename, variable, deltaR value, and an out directory." << std::endl;
22  std::cout << "You may also include a second file at the end." << std::endl;
23  std::cout << "Example: offsetStack label1:file1.root npv 0.4 plots" << std::endl;
24  std::cout << "Example: offsetStack label1:file1.root npv 0.4 plots label2:file2.root" << std::endl;
25  return -1;
26  }
27 
28  TString label1, fname1;
29  split(argv[1], label1, fname1);
30  TString label = label1;
31 
32  TString var = argv[2];
33  float r = std::stof(argv[3]);
34  TString outdir = argv[4];
35 
36  TFile* file1 = TFile::Open(fname1);
37  if (!file1) {
38  std::cout << "Invalid file1: " << fname1 << std::endl;
39  return -1;
40  }
41 
42  TH1F* h = (TH1F*)file1->FindObjectAny(var);
43  if (!h) {
44  std::cout << "Could not find " + var + " hist" << std::endl;
45  return -1;
46  }
47  int avg = h->GetMean() + 0.5;
48 
49  std::map<TString, TH1D*> hists1 = {{"chm", 0}, {"chu", 0}, {"nh", 0}, {"ne", 0}, {"hfh", 0}, {"hfe", 0}, {"lep", 0}};
50  if (!getHists(hists1, file1, var, avg, r))
51  return -1;
52 
53  setStyle();
54  TCanvas c("c", "c", 600, 600);
55 
56  THStack* stack1 = new THStack(
57  "stack1", Form(";#eta;<Offset Energy_{T}(#pi%.1f^{2})>/<%s> [GeV]", r, var == "npv" ? "N_{PV}" : "#mu"));
58  setStack(stack1, hists1);
59  stack1->Draw("hist");
60  TString legPF = "F";
61 
62  TLegend* leg = new TLegend(.4, .67, .65, .92);
63 
64  //file2 included//
65  if (argc > 5) {
66  TString label2, fname2;
67  split(argv[5], label2, fname2);
68 
69  TFile* file2 = TFile::Open(fname2);
70  if (!file2) {
71  std::cout << "Invalid file2: " << fname2 << std::endl;
72  return -1;
73  }
74 
75  TH1F* h2 = (TH1F*)file2->FindObjectAny(var);
76  if (!h2) {
77  std::cout << "Could not find " + var + " hist in file2" << std::endl;
78  return -1;
79  }
80  int avg2 = h2->GetMean() + 0.5;
81 
82  std::map<TString, TH1D*> hists2 = hists1;
83  if (!getHists(hists2, file2, var, avg2, r))
84  return -1;
85 
86  THStack* stack2 = new THStack("stack2", "stack2");
87  setStack(stack2, hists2);
88  stack2->Draw("samepe");
89 
90  legPF = "PF";
91  leg->SetHeader(Form("#bf{Markers: %s, Histograms: %s}", label2.Data(), label1.Data()));
92  label = label1 + "_vs_" + label2;
93 
94  //Draw Markers for EM Deposits and Hadronic Deposits in two separate regions//
95  TH1D* hfe_clone = (TH1D*)hists2["hfe"]->Clone("hfe_clone");
96  TH1D* hfh_clone = (TH1D*)hists2["hfh"]->Clone("hfh_clone");
97 
98  THStack* cloneStack = new THStack("cloneStack", "cloneStack");
99  cloneStack->Add(hists2["ne"]);
100  cloneStack->Add(hfe_clone);
101  cloneStack->Add(hists2["nh"]);
102  cloneStack->Add(hfh_clone);
103  cloneStack->Draw("samepe");
104 
105  hists2["ne"]->SetAxisRange(-2.9, 2.9);
106  hists2["hfe"]->SetAxisRange(-5, -2.6);
107  hists2["nh"]->SetAxisRange(-2.9, 2.9);
108  hists2["hfh"]->SetAxisRange(-5, -2.6);
109  hists2["chu"]->SetAxisRange(-2.9, 2.9);
110  hists2["chm"]->SetAxisRange(-2.9, 2.9);
111 
112  hfe_clone->SetAxisRange(2.6, 5);
113  hfh_clone->SetAxisRange(2.6, 5);
114  }
115 
116  leg->SetBorderSize(0);
117  leg->SetFillColor(0);
118  leg->SetFillStyle(0);
119  leg->SetTextSize(0.04);
120  leg->SetTextFont(42);
121 
122  leg->AddEntry(hists1["ne"], "Photons", legPF);
123  leg->AddEntry(hists1["hfe"], "EM Deposits", legPF);
124  leg->AddEntry(hists1["nh"], "Neutral Hadrons", legPF);
125  leg->AddEntry(hists1["hfh"], "Hadronic Deposits", legPF);
126  leg->AddEntry(hists1["chu"], "Unassoc. Charged Hadrons", legPF);
127  leg->AddEntry(hists1["chm"], "Assoc. Charged Hadrons", legPF);
128 
129  leg->Draw();
130 
131  TLatex text;
132  text.SetNDC();
133 
134  text.SetTextSize(0.065);
135  text.SetTextFont(61);
136  text.DrawLatex(0.2, 0.87, "CMS");
137 
138  text.SetTextSize(0.045);
139  text.SetTextFont(42);
140  text.DrawLatex(1 - label.Length() / 41., 0.96, label);
141 
142  c.Print(outdir + "/stack_" + label + ".pdf");
143 }
144 
145 bool getHists(std::map<TString, TH1D*>& hists, TFile*& file, TString var, int var_val, float r) {
146  for (auto& pair : hists) {
147  TString name = Form("p_offset_eta_%s%i_%s", var.Data(), var_val, pair.first.Data());
148  TProfile* p = (TProfile*)file->FindObjectAny(name);
149  if (!p) {
150  std::cout << "Could not find " + name << std::endl;
151  return false;
152  }
153  pair.second = p->ProjectionX(pair.first);
154  pair.second->Scale(r * r / 2 / var_val);
155 
156  const double* xbins = p->GetXaxis()->GetXbins()->GetArray();
157  for (int i = 1, n = p->GetNbinsX(); i <= n; i++) {
158  pair.second->SetBinContent(i, pair.second->GetBinContent(i) / (xbins[i] - xbins[i - 1]));
159  pair.second->SetBinError(i, pair.second->GetBinError(i) / (xbins[i] - xbins[i - 1]));
160  }
161  }
162  return true;
163 }
164 
165 void setStyle() {
166  gStyle->SetPadTopMargin(0.05);
167  gStyle->SetPadBottomMargin(0.1);
168  gStyle->SetPadLeftMargin(0.16);
169  gStyle->SetPadRightMargin(0.02);
170 
171  gStyle->SetOptStat(0);
172  gStyle->SetOptTitle(0);
173 
174  gStyle->SetTitleFont(42, "XYZ");
175  gStyle->SetTitleSize(0.05, "XYZ");
176  gStyle->SetTitleXOffset(0.9);
177  gStyle->SetTitleYOffset(1.4);
178 
179  gStyle->SetLabelFont(42, "XYZ");
180  gStyle->SetLabelOffset(0.007, "XYZ");
181  gStyle->SetLabelSize(0.04, "XYZ");
182 
183  gStyle->SetPadTickX(1);
184  gStyle->SetPadTickY(1);
185 }
186 
187 void setStack(THStack*& stack, std::map<TString, TH1D*>& hists) {
188  stack->Add(hists["ne"]);
189  stack->Add(hists["hfe"]);
190  stack->Add(hists["nh"]);
191  stack->Add(hists["hfh"]);
192  stack->Add(hists["chu"]);
193  stack->Add(hists["chm"]);
194 
195  hists["ne"]->SetMarkerStyle(kMultiply);
196  hists["hfe"]->SetMarkerStyle(kOpenStar);
197  hists["nh"]->SetMarkerStyle(kOpenDiamond);
198  hists["hfh"]->SetMarkerStyle(kOpenTriangleUp);
199  hists["chu"]->SetMarkerStyle(kOpenCircle);
200  hists["chm"]->SetMarkerStyle(kOpenCircle);
201 
202  hists["ne"]->SetFillColor(kBlue);
203  hists["hfe"]->SetFillColor(kViolet + 2);
204  hists["nh"]->SetFillColor(kGreen);
205  hists["hfh"]->SetFillColor(kPink + 6);
206  hists["chu"]->SetFillColor(kRed - 9);
207  hists["chm"]->SetFillColor(kRed);
208 
209  hists["ne"]->SetLineColor(kBlack);
210  hists["hfe"]->SetLineColor(kBlack);
211  hists["nh"]->SetLineColor(kBlack);
212  hists["hfh"]->SetLineColor(kBlack);
213  hists["chu"]->SetLineColor(kBlack);
214  hists["chm"]->SetLineColor(kBlack);
215 }
216 
217 void split(const TString& str, TString& sub1, TString& sub2) {
218  int pos = str.First(':');
219  sub1 = str(0, pos);
220  sub2 = str(pos + 1, str.Length());
221 }
cmsBatch.argv
argv
Definition: cmsBatch.py:279
fw3dlego::xbins
const double xbins[]
Definition: fw3dlego_xbins.cc:16
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
dir2webdir.argc
argc
Definition: dir2webdir.py:39
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pos
Definition: PixelAliasList.h:18
compare.hists
hists
Definition: compare.py:319
edmOneToOneComparison.file2
file2
Definition: edmOneToOneComparison.py:107
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
bTagCommon_cff.label2
label2
Definition: bTagCommon_cff.py:168
str
#define str(s)
Definition: TestProcessor.cc:51
h
alignmentValidation.outdir
outdir
Definition: alignmentValidation.py:154
svgfig.stack
stack
Definition: svgfig.py:559
getHists
bool getHists(std::map< TString, TH1D * > &hists, TFile *&file, TString var, int var_val, float r)
Definition: offsetStack.cc:145
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
setStyle
void setStyle()
Definition: offsetStack.cc:165
alignCSCRings.r
r
Definition: alignCSCRings.py:93
timingPdfMaker.file1
file1
Definition: timingPdfMaker.py:79
bTagCommon_cff.label1
label1
Definition: bTagCommon_cff.py:167
split
void split(const TString &str, TString &sub1, TString &sub2)
Definition: offsetStack.cc:217
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
main
int main(int argc, char *argv[])
Definition: offsetStack.cc:19
setStack
void setStack(THStack *&stack, std::map< TString, TH1D * > &hists)
Definition: offsetStack.cc:187
runonSM.text
text
Definition: runonSM.py:43
label
const char * label
Definition: PFTauDecayModeTools.cc:11