ATLAS Offline Software
PlotManager.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "EventLoop/Worker.h"
6 
8 
9 #include "TH1.h"
10 #include "TH1D.h"
11 #include "TH2D.h"
12 #include "TFile.h"
13 #include "TDirectoryFile.h"
14 
15 #include <iostream>
16 
17 namespace top {
18  PlotManager::PlotManager(const std::string& name, TFile* outputFile, EL::Worker* wk) :
19  m_wk(wk),
20  m_name(name),
21  m_plotDir(nullptr) {
22  if (!m_wk) {
23  m_plotDir = outputFile->GetDirectory(m_name.c_str());
24  if (!m_plotDir) m_plotDir = outputFile->mkdir(m_name.c_str());
25  m_plotDir = outputFile->GetDirectory(m_name.c_str());// this is needed if the name contains a sub-directory
26 
27  m_plotDir->cd();
28  }
29  }
30 
31  PlotManager::PlotManager(const std::string& sample, const std::string& channel, const std::string& syst,
32  TFile* outputFile) :
33  m_wk(nullptr),
34  m_name("SetMe"),
35  m_plotDir(nullptr) {
36  outputFile->cd();
37  gDirectory->cd("/");
38 
39  if (!gDirectory->GetKey(sample.c_str())) {
40  gDirectory->mkdir(sample.c_str());
41  }
42  gDirectory->cd(sample.c_str());
43 
44  if (!gDirectory->GetKey(channel.c_str())) {
45  gDirectory->mkdir(channel.c_str());
46  }
47  gDirectory->cd(channel.c_str());
48 
49  if (!gDirectory->GetKey(syst.c_str())) {
50  gDirectory->mkdir(syst.c_str());
51  }
52  gDirectory->cd(syst.c_str());
53 
54  TString plotDirName = sample + "/" + channel + "/" + syst;
55 
56  m_plotDir = outputFile->GetDirectory(plotDirName);
57  m_plotDir->cd();
58  }
59 
60  void PlotManager::addHist(const std::string& hname, const std::string& title, int bins, double start,
61  double end) const {
62  TH1D* h = nullptr;
63 
64  if (!m_wk) {
65  m_plotDir->cd();
66  h = new TH1D(hname.c_str(), title.c_str(), bins, start, end);
67  } else {
68  std::string pathname = m_name + "/" + hname;
69  h = new TH1D(pathname.c_str(), title.c_str(), bins, start, end);
70  m_wk->addOutput(h);
71  }
72 
74 
75  h->Sumw2();
76  m_histograms.insert(make_pair(hname, h));
77  }
78 
79  void PlotManager::addHist(const std::string& hname, const std::string& title, int bins, double* binArray) const {
80  TH1D* h = nullptr;
81 
82  if (!m_wk) {
83  m_plotDir->cd();
84  h = new TH1D(hname.c_str(), title.c_str(), bins, binArray);
85  } else {
86  std::string pathname = m_name + "/" + hname;
87  h = new TH1D(pathname.c_str(), title.c_str(), bins, binArray);
88  m_wk->addOutput(h);
89  }
90 
92 
93  h->Sumw2();
94  m_histograms.insert(make_pair(hname, h));
95  }
96 
97  void PlotManager::addHist(const std::string& hname, const std::string& title,
98  int xbins, double xstart, double xend,
99  int ybins, double ystart, double yend) const {
100  TH2D* h = nullptr;
101 
102  if (!m_wk) {
103  m_plotDir->cd();
104  h = new TH2D(hname.c_str(), title.c_str(),
105  xbins, xstart, xend,
106  ybins, ystart, yend);
107  } else {
108  std::string pathname = m_name + "/" + hname;
109  h = new TH2D(pathname.c_str(), title.c_str(),
110  xbins, xstart, xend,
111  ybins, ystart, yend);
112  m_wk->addOutput(h);
113  }
114 
116 
117  h->Sumw2();
118  m_histograms.insert(make_pair(hname, h));
119  }
120 
121  void PlotManager::addHist(const std::string& hname, const std::string& title,
122  int xbins, double* xbinArray,
123  int ybins, double* ybinArray) const {
124  TH2D* h = nullptr;
125 
126  if (!m_wk) {
127  m_plotDir->cd();
128  h = new TH2D(hname.c_str(), title.c_str(),
129  xbins, xbinArray,
130  ybins, ybinArray);
131  } else {
132  std::string pathname = m_name + "/" + hname;
133  h = new TH2D(pathname.c_str(), title.c_str(),
134  xbins, xbinArray,
135  ybins, ybinArray);
136  m_wk->addOutput(h);
137  }
138 
140 
141  h->Sumw2();
142  m_histograms.insert(make_pair(hname, h));
143  }
144 
145  void PlotManager::addHist(const std::string& hname, const std::string& title,
146  int xbins, double* xbinArray,
147  int ybins, double ystart, double yend) const {
148  TH2D* h = nullptr;
149 
150  if (!m_wk) {
151  m_plotDir->cd();
152  h = new TH2D(hname.c_str(), title.c_str(),
153  xbins, xbinArray,
154  ybins, ystart, yend);
155  } else {
156  std::string pathname = m_name + "/" + hname;
157  h = new TH2D(pathname.c_str(), title.c_str(),
158  xbins, xbinArray,
159  ybins, ystart, yend);
160  m_wk->addOutput(h);
161  }
162 
164 
165  h->Sumw2();
166  m_histograms.insert(make_pair(hname, h));
167  }
168 
169  TH1* PlotManager::hist(const std::string& name) const {
170  if (m_histograms.find(name) == m_histograms.end()) {
171  throw std::runtime_error("PlotManager::hist: Histogram " + name + " does not exist.");
172  }
173 
174  return m_histograms[name];
175  }
176 
177  void PlotManager::scaleHistograms(double sf) const {
179  it->second->Scale(sf);
180  }
181 
182  void PlotManager::checkDuplicate(const std::string& hname) const {
183  if (m_histograms.find(hname) != m_histograms.end()) {
184  throw std::runtime_error("PlotManager::addHist: Histogram " + hname + " to add already exists.");
185  }
186  }
187 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
PlotManager.h
EL::Worker::addOutput
void addOutput(TObject *output_swallow) final override
effects: add an object to the output.
Definition: Worker.cxx:88
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:28
python.App.bins
bins
Definition: App.py:410
top::PlotManager::m_histograms
std::unordered_map< std::string, TH1 * > m_histograms
A map that's hopefully quick to search because it'll be used a lot per event.
Definition: PlotManager.h:143
dqt_zlumi_pandas.hname
string hname
Definition: dqt_zlumi_pandas.py:272
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
skel.it
it
Definition: skel.GENtoEVGEN.py:423
TH1D
Definition: rootspy.cxx:342
PixelAthClusterMonAlgCfg.ybins
ybins
Definition: PixelAthClusterMonAlgCfg.py:163
top::PlotManager::m_wk
EL::Worker * m_wk
Definition: PlotManager.h:137
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
compareGeometries.outputFile
string outputFile
Definition: compareGeometries.py:25
top::PlotManager::scaleHistograms
void scaleHistograms(double sf=1.) const
Save the histograms in alphabetical order.
Definition: PlotManager.cxx:177
EL::Worker
Definition: Worker.h:25
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:100
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
covarianceTool.title
title
Definition: covarianceTool.py:542
top::PlotManager::hist
TH1 * hist(const std::string &name) const
Recover an existing histogram, to fill it for example.
Definition: PlotManager.cxx:169
TH2D
Definition: rootspy.cxx:430
ConvertOldHistosToNewHistos.binArray
binArray
Definition: ConvertOldHistosToNewHistos.py:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
LArCellBinning.xbins
int xbins
Definition: LArCellBinning.py:163
top::PlotManager::checkDuplicate
void checkDuplicate(const std::string &hname) const
Helper method to check for duplicates when adding histogram.
Definition: PlotManager.cxx:182
top::PlotManager::addHist
void addHist(const std::string &hname, const std::string &title, int bins, double start, double end) const
Add a 1D histogram to the output file.
Definition: PlotManager.cxx:60
h
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
TH1
Definition: rootspy.cxx:268
Worker.h
top::PlotManager::PlotManager
PlotManager(const std::string &name, TFile *outputFile=nullptr, EL::Worker *wk=nullptr)
A name for the selection.
Definition: PlotManager.cxx:18
top::PlotManager::m_name
std::string m_name
Name of the folder to store the plots in, in the output file.
Definition: PlotManager.h:140
top::PlotManager::m_plotDir
TDirectory * m_plotDir
Definition: PlotManager.h:145