NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
PlotGroup.cxx
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#include "NAGASH/PlotGroup.h"
7
8using namespace NAGASH;
9
75PlotGroup::PlotGroup(std::shared_ptr<MSGTool> MSG, std::shared_ptr<ConfigTool> c, const TString &rname, const TString &fname) : Result(MSG, c, rname, fname), maptool(MSG)
76{
77}
78
80void PlotGroup::Combine(std::shared_ptr<Result> result)
81{
82 MSGUser()->StartTitle("PlotGroup::Combine");
83 auto subPlotGroup = std::dynamic_pointer_cast<std::remove_pointer<decltype(this)>::type>(result);
84 if (subPlotGroup != nullptr)
85 {
86 for (auto &sub : subPlotGroup->PlotMap)
87 {
88 auto findresult = this->PlotMap.find(sub.first);
89 if (findresult != this->PlotMap.end())
90 {
91 findresult->second->Combine(sub.second);
92 }
93 else
94 {
95 auto subname = sub.first;
96 auto newPlot = sub.second->CloneVirtual(this->GetResultName() + " " + subname);
97 this->PlotMap.emplace(std::pair<TString, std::shared_ptr<HistBase>>(subname, newPlot));
98 }
99 }
100 }
101 MSGUser()->EndTitle();
102}
103
106{
107 for (auto &t : PlotVector)
108 t->Process();
109}
110
113void PlotGroup::ForEachPlot(std::function<void(std::shared_ptr<HistBase>)> func)
114{
115 for (auto &t : PlotVector)
116 func(t);
117}
118
121{
122 for (auto &t : PlotVector)
123 t->SetUnprocessed();
124}
125
128void PlotGroup::Scale(double sf)
129{
130 for (auto &t : PlotVector)
131 t->Scale(sf);
132}
133
137void PlotGroup::ScaleVariation(const TString &varname, double sf)
138{
139 auto titleguard = MSGUser()->StartTitleWithGuard("PlotGroup::ScaleVariation");
140 for (auto &t : PlotVector)
141 {
142 if (auto h = t->GetVariationVirtual(varname); h)
143 h->Scale(sf);
144 else
145 MSGUser()->MSG_WARNING("plot ", t->GetResultName(), " does not have variation ", varname, ", will not scale this variation");
146 }
147}
148
150void PlotGroup::SetLinkType(std::function<double(const std::vector<double> &)> cf, std::function<double(const std::vector<double> &, const std::vector<double> &)> ef)
151{
152 for (auto &t : PlotVector)
153 t->SetLinkType(cf, ef);
154}
155
157void PlotGroup::SetLinkType(const TString &type)
158{
159 for (auto &t : PlotVector)
160 t->SetLinkType(type);
161}
162
164void PlotGroup::SetLinkType(std::function<void(const std::vector<TH1 *> &, TH1 *)> hf)
165{
166 for (auto &t : PlotVector)
167 t->SetLinkType(hf);
168}
169
171void PlotGroup::SetLinkPlotGroup(uint64_t index, std::shared_ptr<PlotGroup> pg)
172{
173 for (auto &t : pg->PlotMap)
174 {
175 auto findresult = this->PlotMap.find(t.first);
176 if (findresult != this->PlotMap.end())
177 findresult->second->SetLinkPlot(index, t.second);
178 }
179}
180
183{
184 for (auto &t : PlotVector)
185 {
186 t->ClearLinkPlot();
187 }
188}
189
192{
193 auto titleguard = MSGUser()->StartTitleWithGuard("Plot1D::WriteToFile");
194 Process();
195 if (GetOutputFileName() == "")
196 {
197 MSGUser()->MSG(MSGLevel::WARNING, "input an empty output file name, will be ignored");
198 return;
199 }
200
201 TFile *file = new TFile(GetOutputFileName(), "UPDATE");
202 file->cd();
203 for (auto &t : PlotVector)
204 t->Write();
205
206 file->Close();
207}
208
211{
212 auto titlegurad = MSGUser()->StartTitleWithGuard("PlotGroup::Reset");
213 for (auto &t : PlotVector)
214 t->Reset();
215}
216
219void PlotGroup::Recover(const TString &filename)
220{
221 auto titlegurad = MSGUser()->StartTitleWithGuard("PlotGroup::Recover");
222 TFile *file = new TFile(filename.TString::Data());
223 if (file == nullptr)
224 {
225 MSGUser()->MSG_ERROR("ROOT file ", filename, " does not exist");
226 return;
227 }
228
229 MSGUser()->MSG_DEBUG("Recover ", GetResultName(), " from file ", filename);
230 Recover(file);
231 file->Close();
232}
233
236void PlotGroup::Recover(TFile *file)
237{
238 auto titlegurad = MSGUser()->StartTitleWithGuard("PlotGroup::Recover");
239 if (file == nullptr)
240 {
241 MSGUser()->MSG_ERROR("ROOT file does not exist");
242 return;
243 }
244
245 for (auto &t : PlotVector)
246 t->Recover(file);
247}
248
251void PlotGroup::Recover(std::shared_ptr<TFileHelper> file)
252{
253 auto titlegurad = MSGUser()->StartTitleWithGuard("PlotGroup::Recover");
254 for (auto &t : PlotVector)
255 t->Recover(file);
256}
257
260std::shared_ptr<PlotGroup> PlotGroup::Clone(const TString &name)
261{
262 std::shared_ptr<PlotGroup> newPlotGroup = std::make_shared<PlotGroup>(MSGUser(), ConfigUser(), name, GetOutputFileName());
263 for (auto &t : PlotMap)
264 {
265 auto newplot = t.second->CloneVirtual(name + "_" + t.first);
266 newPlotGroup->PlotMap.insert(std::pair<TString, std::shared_ptr<HistBase>>(t.first, newplot));
267 PlotVector.emplace_back(newplot);
268 }
269
270 return newPlotGroup;
271}
272
275{
276 return PlotVector.size();
277}
278
283void PlotGroup::BookSystematicVariation(const TString &name, double corr_factor, HistBase::SystPolicy policy)
284{
285 for (auto &t : PlotVector)
286 t->BookSystematicVariation(name, corr_factor, policy);
287}
288
294void PlotGroup::BookSystematicVariation(const TString &name1, const TString &name2, double corr_factor, HistBase::SystPolicy policy)
295{
296 for (auto &t : PlotVector)
297 t->BookSystematicVariation(name1, name2, corr_factor, policy);
298}
299
304void PlotGroup::BookSystematicVariation(const std::vector<TString> &names, double corr_factor, HistBase::SystPolicy policy)
305{
306 for (auto &t : PlotVector)
307 t->BookSystematicVariation(names, corr_factor, policy);
308}
309
311void PlotGroup::RegroupSystematicVariation(const std::vector<TString> &names, HistBase::SystPolicy policy)
312{
313 for (auto &t : PlotVector)
314 t->RegroupSystematicVariation(names, policy);
315}
316
319{
320 for (auto &t : PlotVector)
321 t->RemoveSystematicVariation(name);
322}
323
325void PlotGroup::RenameSystematicVariation(const TString &name_old, const TString &name_new)
326{
327 for (auto &t : PlotVector)
328 t->RenameSystematicVariation(name_old, name_new);
329}
330
333void PlotGroup::SetSystematicVariation(const TString &name)
334{
335 for (auto &t : PlotVector)
336 t->SetSystematicVariation(name);
337}
338
342{
343 for (auto &t : PlotVector)
344 t->SetSystematicVariation(index);
345}
SystPolicy
Policies of dealing with systematic uncertainties.
Definition HistBase.h:33
void SetLinkPlotGroup(uint64_t, std::shared_ptr< PlotGroup >)
Set the linked plotgroup, should be the same structure of this one.
void WriteToFile() override
Write all histograms inside this PlotGroup to a file.
void BookSystematicVariation(const TString &name, double corr_factor=0, HistBase::SystPolicy policy=HistBase::SystPolicy::Maximum)
Book systematic variations for each histogram inside this PlotGroup.
void Reset()
Call NGHist::Reset() for each histogram inside this PlotGroup.
void Scale(double)
Call NGHist::Scale() for each histogram inside this PlotGroup.
void SetSystematicVariation(const TString &name="Nominal")
Set the systematic variation for each histogram inside this PlotGroup.
std::map< TString, std::shared_ptr< HistBase > > PlotMap
Definition PlotGroup.h:20
void Process()
Call NGHist::Process() for each histogram inside this PlotGroup.
void ClearLinkPlotGroup()
Clear the linked plotgroup.
void Recover(const TString &filename)
Retrieve histograms from the input file.
std::vector< std::shared_ptr< HistBase > > PlotVector
Definition PlotGroup.h:21
void SetLinkType(const TString &)
Set the type of the link for each histogram inside this PlotGroup.
void Combine(std::shared_ptr< Result > result) override
Combine two PlotGroups.
Definition PlotGroup.cxx:80
void RegroupSystematicVariation(const std::vector< TString > &names, HistBase::SystPolicy policy)
Call NGHist::RegroupSystematicVariation for each histogram inside this PlotGroup.
void RemoveSystematicVariation(const TString &name)
Call NGHist::RemoveSystematicVariation for each histogram inside this PlotGroup.
void RenameSystematicVariation(const TString &name_old, const TString &name_new)
Call NGHist::RenameSystematicVariation for each histogram inside this PlotGroup.
std::shared_ptr< PlotGroup > Clone(const TString &name)
Clone this PlotGroup.
void ScaleVariation(const TString &, double)
Scale a variation of each histogram inside this PlotGroup.
int GetNPlots()
Get the number of histograms stored inside this PlotGroup.
PlotGroup(std::shared_ptr< MSGTool > MSG, std::shared_ptr< ConfigTool > c, const TString &rname, const TString &fname="")
Constructor, same as that of Result.
Definition PlotGroup.cxx:75
void ForEachPlot(std::function< void(std::shared_ptr< HistBase >)>)
Apply the given function to each histogram inside this PlotGroup.
void SetUnprocessed()
Call NGHist::SetUnprocessed() for each histogram inside this PlotGroup.
Provide virtual interface to manipulate all the results inside NAGASH.
Definition Result.h:68
const TString & GetOutputFileName()
Return the name of the result.
Definition Result.h:71
const TString & GetResultName()
Definition Result.h:70
std::shared_ptr< ConfigTool > ConfigUser()
Return the internal ConfigTool.>
Definition Result.h:108
std::shared_ptr< MSGTool > MSGUser()
Return the internal MSGTool.
Definition Result.h:106