Skip to content

Commit f59906c

Browse files
authored
Merge pull request cms-sw#43311 from bsunanda/Run3-sim153
Run3-sim153 Reorganize the codes relevant to forward detectors among Calo and Forward projects in SimG4CMS
2 parents c2ad54f + 963387b commit f59906c

File tree

4 files changed

+252
-1
lines changed

4 files changed

+252
-1
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Usage:
4+
// .L MakeZDCPlots.C+g
5+
//
6+
// To make plot of various quantities which are created by ZDCSimHitStudy
7+
// from upto three different settings
8+
//
9+
// makeHitStudyPlots(file1, tag1, file2, tag2, file3, tag3, todomin,
10+
// todomax, gtitle, ratio, save, dirnm)
11+
//
12+
// where (for makeHitStudyPlots)
13+
// fileN std::string Name of the Nth ROOT file
14+
// The fist must exist; blank means beng ignored
15+
// tag1 std::string Tag for the Nth file
16+
// todomin int Minimum type # of histograms to be plotted [0]
17+
// todomax int Maximum type # of histograms to be plotted [5]
18+
// gtitle std::string Overall Titile [""]
19+
// ratio bool if the ratio to be plotted [true]
20+
// (with respect to the first file)
21+
// save bool If the canvas is to be saved [false]
22+
// dirnm std::string Name of the directory [zdcSimHitStudy]
23+
//
24+
//////////////////////////////////////////////////////////////////////////////
25+
26+
#include <TCanvas.h>
27+
#include <TChain.h>
28+
#include <TFile.h>
29+
#include <TFitResult.h>
30+
#include <TFitResultPtr.h>
31+
#include <TH1D.h>
32+
#include <TH2D.h>
33+
#include <TLegend.h>
34+
#include <TPaveStats.h>
35+
#include <TPaveText.h>
36+
#include <TProfile.h>
37+
#include <TROOT.h>
38+
#include <TStyle.h>
39+
#include <vector>
40+
#include <string>
41+
#include <iomanip>
42+
#include <iostream>
43+
#include <fstream>
44+
45+
void makeHitStudyPlots(std::string file1 = "Forward.root",
46+
std::string tag1 = "New",
47+
std::string file2 = "Standard.root",
48+
std::string tag2 = "Old",
49+
std::string file3 = "",
50+
std::string tag3 = "",
51+
std::string gtitle = "",
52+
int todomin = 0,
53+
int todomax = 5,
54+
bool ratio = true,
55+
bool save = false,
56+
std::string dirnm = "zdcSimHitStudy") {
57+
const int plots = 6;
58+
std::string names[plots] = {"ETot", "ETotT", "Edep", "Hits", "Indx", "Time"};
59+
int logy[plots] = {1, 1, 1, 0, 1, 1};
60+
int rebin[plots] = {10, 10, 10, 2, 1, 10};
61+
int xmax[plots] = {5, 5, 3, 5, 2, 2};
62+
int colors[5] = {1, 2, 4, 6, 46};
63+
int marker[5] = {20, 21, 22, 23, 24};
64+
int styles[5] = {1, 2, 3, 4, 5};
65+
bool debug(false);
66+
67+
gStyle->SetCanvasBorderMode(0);
68+
gStyle->SetCanvasColor(kWhite);
69+
gStyle->SetPadColor(kWhite);
70+
gStyle->SetFillColor(kWhite);
71+
if (ratio) {
72+
gStyle->SetOptStat(10);
73+
gStyle->SetOptFit(1);
74+
} else {
75+
gStyle->SetOptStat(1110);
76+
}
77+
TFile* file[3];
78+
int nfile(0);
79+
std::string tag(""), tags[3];
80+
if (file1 != "") {
81+
file[nfile] = new TFile(file1.c_str());
82+
if (file[nfile]) {
83+
tags[nfile] = tag1;
84+
++nfile;
85+
tag += tag1;
86+
}
87+
}
88+
if (file2 != "") {
89+
file[nfile] = new TFile(file2.c_str());
90+
if (file[nfile]) {
91+
tags[nfile] = tag2;
92+
++nfile;
93+
tag += tag2;
94+
}
95+
}
96+
if (file3 != "") {
97+
file[nfile] = new TFile(file3.c_str());
98+
if (file[nfile]) {
99+
tags[nfile] = tag3;
100+
++nfile;
101+
tag += tag3;
102+
}
103+
}
104+
if ((todomin < 0) || (todomin >= plots))
105+
todomin = 0;
106+
if (todomax < todomin) {
107+
todomax = todomin;
108+
} else if (todomax >= plots) {
109+
todomax = plots - 1;
110+
}
111+
std::cout << "Use " << nfile << " files from " << file1 << "," << file2 << " and " << file3 << " and look for "
112+
<< todomin << ":" << todomax << std::endl;
113+
for (int todo = todomin; todo <= todomax; ++todo) {
114+
double y1(0.90), dy(0.12);
115+
double y2 = y1 - dy * nfile - 0.01;
116+
double y3 = y1 - 0.04 * (nfile - 1);
117+
TLegend* leg = (ratio) ? (new TLegend(0.10, y3, 0.65, y1)) : (new TLegend(0.65, y2 - nfile * 0.04, 0.90, y2));
118+
leg->SetBorderSize(1);
119+
leg->SetFillColor(10);
120+
TH1D* hist0[nfile];
121+
for (int i1 = 0; i1 < nfile; ++i1) {
122+
TDirectory* dir = static_cast<TDirectory*>(file[i1]->FindObjectAny(dirnm.c_str()));
123+
hist0[i1] = static_cast<TH1D*>(dir->FindObjectAny(names[todo].c_str()));
124+
if (debug)
125+
std::cout << "Reads histogram " << hist0[i1]->GetName() << " for " << hist0[i1]->GetTitle() << " from "
126+
<< file[i1] << std::endl;
127+
}
128+
std::string title = hist0[0]->GetTitle();
129+
if (debug)
130+
std::cout << "Histogram title " << title << std::endl;
131+
int istart = (ratio) ? 1 : 0;
132+
char name[100], namec[100];
133+
std::vector<TH1D*> hist1;
134+
for (int i1 = istart; i1 < nfile; ++i1) {
135+
if (ratio) {
136+
int nbin = hist0[0]->GetNbinsX();
137+
int nbinR = nbin / rebin[todo];
138+
double xlow = hist0[0]->GetXaxis()->GetBinLowEdge(1);
139+
double xhigh = hist0[0]->GetXaxis()->GetBinLowEdge(nbin) + hist0[0]->GetXaxis()->GetBinWidth(nbin);
140+
sprintf(name, "%s%sVs%sRatio", names[todo].c_str(), tags[0].c_str(), tags[i1].c_str());
141+
TH1D* histr = new TH1D(name, hist0[0]->GetTitle(), nbinR, xlow, xhigh);
142+
histr->SetTitle(gtitle.c_str());
143+
histr->GetXaxis()->SetTitle(title.c_str());
144+
histr->GetYaxis()->SetTitle(name);
145+
histr->GetXaxis()->SetLabelOffset(0.005);
146+
histr->GetXaxis()->SetTitleOffset(1.00);
147+
histr->GetYaxis()->SetLabelOffset(0.005);
148+
histr->GetYaxis()->SetTitleOffset(1.20);
149+
histr->GetYaxis()->SetRangeUser(0.0, xmax[todo]);
150+
histr->SetLineColor(colors[i1 - 1]);
151+
histr->SetLineStyle(styles[i1 - 1]);
152+
histr->SetMarkerStyle(marker[i1 - 1]);
153+
histr->SetMarkerColor(colors[i1 - 1]);
154+
double sumNum(0), sumDen(0), maxDev(0), xlow1(-1), xhigh1(xlow);
155+
for (int j = 0; j < nbinR; ++j) {
156+
double tnum(0), tden(0), rnum(0), rden(0);
157+
for (int i = 0; i < rebin[todo]; ++i) {
158+
int ib = j * rebin[todo] + i + 1;
159+
tnum += hist0[0]->GetBinContent(ib);
160+
tden += hist0[1]->GetBinContent(ib);
161+
rnum += ((hist0[0]->GetBinError(ib)) * (hist0[0]->GetBinError(ib)));
162+
rden += ((hist0[1]->GetBinError(ib)) * (hist0[1]->GetBinError(ib)));
163+
}
164+
if (tden > 0 && tnum > 0) {
165+
if (xlow1 < 0)
166+
xlow1 = hist0[0]->GetXaxis()->GetBinLowEdge(j * rebin[todo] + 1);
167+
xhigh1 = hist0[0]->GetXaxis()->GetBinLowEdge((j + 1) * rebin[todo]) +
168+
hist0[0]->GetXaxis()->GetBinWidth((j + 1) * rebin[todo]);
169+
double rat = tnum / tden;
170+
double err = rat * sqrt((rnum / (tnum * tnum)) + (rden / (tden * tden)));
171+
if (debug)
172+
std::cout << "Bin " << j << " Ratio " << rat << " +- " << err << std::endl;
173+
histr->SetBinContent(j + 1, rat);
174+
histr->SetBinError(j + 1, err);
175+
double temp1 = (rat > 1.0) ? 1.0 / rat : rat;
176+
double temp2 = (rat > 1.0) ? err / (rat * rat) : err;
177+
sumNum += (fabs(1 - temp1) / (temp2 * temp2));
178+
sumDen += (1.0 / (temp2 * temp2));
179+
if (fabs(1 - temp1) > maxDev)
180+
maxDev = fabs(1 - temp1);
181+
}
182+
}
183+
histr->Fit("pol0", "+QRWLS", "", xlow1, xhigh1);
184+
sumNum = (sumDen > 0) ? (sumNum / sumDen) : 0;
185+
sumDen = (sumDen > 0) ? 1.0 / sqrt(sumDen) : 0;
186+
sprintf(name, "%sVs%sRatio", tags[0].c_str(), tags[i1].c_str());
187+
if (sumNum == 0)
188+
sumDen = 0;
189+
sprintf(name, "%s vs %s (%6.3f +- %6.3f)", tags[0].c_str(), tags[i1].c_str(), sumNum, sumDen);
190+
hist1.push_back(histr);
191+
leg->AddEntry(histr, name, "lp");
192+
} else {
193+
hist1.push_back(hist0[i1]);
194+
hist1.back()->SetTitle(gtitle.c_str());
195+
hist1.back()->GetXaxis()->SetTitle(title.c_str());
196+
hist1.back()->GetYaxis()->SetTitle("Events");
197+
hist1.back()->GetXaxis()->SetLabelOffset(0.005);
198+
hist1.back()->GetXaxis()->SetTitleOffset(1.00);
199+
hist1.back()->GetYaxis()->SetLabelOffset(0.005);
200+
hist1.back()->GetYaxis()->SetTitleOffset(1.40);
201+
hist1.back()->SetLineColor(colors[i1]);
202+
hist1.back()->SetLineStyle(styles[i1]);
203+
leg->AddEntry(hist1.back(), tags[i1].c_str(), "lp");
204+
}
205+
}
206+
sprintf(namec, "c_%s", hist1[0]->GetName());
207+
if (debug)
208+
std::cout << namec << " Canvas made for " << hist1[0]->GetName() << " with " << hist1.size() << " plots"
209+
<< std::endl;
210+
TCanvas* pad = new TCanvas(namec, namec, 500, 500);
211+
pad->SetRightMargin(0.10);
212+
pad->SetTopMargin(0.10);
213+
if ((!ratio) && (logy[todo] > 0))
214+
pad->SetLogy(1);
215+
else
216+
pad->SetLogy(0);
217+
for (unsigned int i1 = 0; i1 < hist1.size(); ++i1) {
218+
if ((!ratio) && (rebin[todo] > 1))
219+
hist1[i1]->Rebin(rebin[todo]);
220+
if (i1 == 0) {
221+
hist1[i1]->Draw();
222+
} else {
223+
hist1[i1]->Draw("sames");
224+
}
225+
if (debug)
226+
std::cout << "Drawing histograms for " << hist1[i1]->GetName() << ":" << i1 << " in canvas " << pad->GetName()
227+
<< std::endl;
228+
pad->Update();
229+
double dy0 = (ratio) ? 0.75 * dy : dy;
230+
TPaveStats* st = ((TPaveStats*)hist1[i1]->GetListOfFunctions()->FindObject("stats"));
231+
if (st != NULL) {
232+
st->SetFillColor(kWhite);
233+
st->SetLineColor(colors[i1]);
234+
st->SetTextColor(colors[i1]);
235+
st->SetY1NDC(y1 - dy0);
236+
st->SetY2NDC(y1);
237+
st->SetX1NDC(0.65);
238+
st->SetX2NDC(0.90);
239+
y1 -= dy0;
240+
}
241+
pad->Modified();
242+
pad->Update();
243+
leg->Draw("same");
244+
pad->Update();
245+
if (save) {
246+
sprintf(name, "%s.pdf", pad->GetName());
247+
pad->Print(name);
248+
}
249+
}
250+
}
251+
}

SimG4CMS/Calo/test/python/runZDCStudy_cfg.py renamed to SimG4CMS/Forward/test/python/runZDCStudy_cfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
fileName = cms.string(outFile)
4747
)
4848

49-
process.load("SimG4CMS.Calo.zdcSimHitStudy_cfi")
49+
process.load("SimG4CMS.Forward.zdcSimHitStudy_cfi")
5050

5151
process.zdcSimHitStudy.Verbose = True
5252

0 commit comments

Comments
 (0)