NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
PlotGroup.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9#include "NAGASH/MapTool.h"
10#include "NAGASH/Plot1D.h"
11#include "NAGASH/Plot2D.h"
12#include "NAGASH/Plot3D.h"
13#include "NAGASH/Result.h"
14
15namespace NAGASH
16{
17 class PlotGroup : public Result
18 {
19 private:
20 std::map<TString, std::shared_ptr<HistBase>> PlotMap;
21 std::vector<std::shared_ptr<HistBase>> PlotVector;
22
24
25 public:
26 PlotGroup(std::shared_ptr<MSGTool> MSG, std::shared_ptr<ConfigTool> c, const TString &rname, const TString &fname = "");
27
28 template <typename... Args>
29 std::shared_ptr<Plot1D> BookPlot1D(const TString &name, Args &&...args);
30 template <typename... Args>
31 std::shared_ptr<Plot2D> BookPlot2D(const TString &name, Args &&...args);
32 template <typename... Args>
33 std::shared_ptr<Plot3D> BookPlot3D(const TString &name, Args &&...args);
34
35 std::shared_ptr<Plot1D> GetPlot1D(const TString &name);
36 std::shared_ptr<Plot2D> GetPlot2D(const TString &name);
37 std::shared_ptr<Plot3D> GetPlot3D(const TString &name);
38
39 void AddPlot1D(const TString &name, std::shared_ptr<Plot1D> plot);
40 void AddPlot2D(const TString &name, std::shared_ptr<Plot2D> plot);
41 void AddPlot3D(const TString &name, std::shared_ptr<Plot3D> plot);
42
43 // for general purpose
44 template <typename HistType, typename... Args>
45 std::shared_ptr<NGHist<HistType>> BookNGHist(const TString &name, Args &&...args);
46 template <typename HistType>
47 std::shared_ptr<NGHist<HistType>> GetNGHist(const TString &name);
48 template <typename HistType>
49 void AddNGHist(const TString &name, std::shared_ptr<NGHist<HistType>> plot);
50
51 int GetNPlots();
52 void Process();
53 void ForEachPlot(std::function<void(std::shared_ptr<HistBase>)>);
54 void SetUnprocessed();
55 void Scale(double);
56 void ScaleVariation(const TString &, double);
57 void SetLinkType(const TString &);
58 void SetLinkType(std::function<double(const std::vector<double> &)>, std::function<double(const std::vector<double> &, const std::vector<double> &)>);
59 void SetLinkType(std::function<void(const std::vector<TH1 *> &, TH1 *)>);
60 void SetLinkPlotGroup(uint64_t, std::shared_ptr<PlotGroup>);
61 void ClearLinkPlotGroup();
62 void Reset();
63 void Recover(const TString &filename);
64 void Recover(TFile *file);
65 void Recover(std::shared_ptr<TFileHelper>);
66 std::shared_ptr<PlotGroup> Clone(const TString &name);
67 void BookSystematicVariation(const TString &name, double corr_factor = 0, HistBase::SystPolicy policy = HistBase::SystPolicy::Maximum);
68 void BookSystematicVariation(const TString &name1, const TString &name2, double corr_factor = 0, HistBase::SystPolicy policy = HistBase::SystPolicy::Maximum);
69 void BookSystematicVariation(const std::vector<TString> &names, double corr_factor = 0, HistBase::SystPolicy policy = HistBase::SystPolicy::Maximum);
70 void RegroupSystematicVariation(const std::vector<TString> &names, HistBase::SystPolicy policy);
71 void RemoveSystematicVariation(const TString &name);
72 void RenameSystematicVariation(const TString &name_old, const TString &name_new);
73 void SetSystematicVariation(const TString &name = "Nominal");
74 void SetSystematicVariation(int index);
75 void Combine(std::shared_ptr<Result> result) override;
76 void WriteToFile() override;
77 virtual ~PlotGroup() = default;
78 }; // class PlotGroup
79
86 template <typename HistType, typename... Args>
87 inline std::shared_ptr<NGHist<HistType>> PlotGroup::BookNGHist(const TString &name, Args &&...args)
88 {
89 if (PlotMap.find(name) == PlotMap.end())
90 {
91 auto tempplot = std::make_shared<NGHist<HistType>>(MSGUser(), ConfigUser(), GetResultName() + "_" + name, "", std::forward<Args>(args)...);
92 PlotMap.emplace(std::pair<TString, std::shared_ptr<HistBase>>(name, tempplot));
93 PlotVector.emplace_back(tempplot);
94 return tempplot;
95 }
96 else
97 {
98 auto guard = MSGUser()->StartTitleWithGuard("PlotGroup::BookNGHist");
99 MSGUser()->MSG_WARNING("Plot ", name, " has already been booked, returning nullptr");
100 return nullptr;
101 }
102 }
103
108 template <typename HistType>
109 inline std::shared_ptr<NGHist<HistType>> PlotGroup::GetNGHist(const TString &name)
110 {
111 auto guard = MSGUser()->StartTitleWithGuard("PlotGroup::GetNGHist");
112 std::shared_ptr<NGHist<HistType>> plot = nullptr;
113 auto findresult = maptool.FindMostSimilar(PlotMap, name);
114 if (findresult != PlotMap.end())
115 plot = std::dynamic_pointer_cast<NGHist<HistType>>(findresult->second);
116
117 if (plot != nullptr)
118 return plot;
119 else
120 {
121 MSGUser()->MSG_WARNING("Plot ", name, " does not exist, returning nullptr");
122 return nullptr;
123 }
124 }
125
130 template <typename HistType>
131 inline void PlotGroup::AddNGHist(const TString &name, std::shared_ptr<NGHist<HistType>> plot)
132 {
133 PlotMap.emplace(std::pair<TString, std::shared_ptr<HistBase>>(name, plot));
134 PlotVector.emplace_back(plot);
135 }
136
137 // legacy
138
144 template <typename... Args>
145 inline std::shared_ptr<Plot1D> PlotGroup::BookPlot1D(const TString &name, Args &&...args)
146 {
147 return BookNGHist<TH1D>(name, std::forward<Args>(args)...);
148 }
149
155 template <typename... Args>
156 inline std::shared_ptr<Plot2D> PlotGroup::BookPlot2D(const TString &name, Args &&...args)
157 {
158 return BookNGHist<TH2D>(name, std::forward<Args>(args)...);
159 }
160
166 template <typename... Args>
167 inline std::shared_ptr<Plot3D> PlotGroup::BookPlot3D(const TString &name, Args &&...args)
168 {
169 return BookNGHist<TH3D>(name, std::forward<Args>(args)...);
170 }
171
175 inline std::shared_ptr<Plot1D> PlotGroup::GetPlot1D(const TString &name)
176 {
177 return GetNGHist<TH1D>(name);
178 }
179
183 inline std::shared_ptr<Plot2D> PlotGroup::GetPlot2D(const TString &name)
184 {
185 return GetNGHist<TH2D>(name);
186 }
187
191 inline std::shared_ptr<Plot3D> PlotGroup::GetPlot3D(const TString &name)
192 {
193 return GetNGHist<TH3D>(name);
194 }
195
199 inline void PlotGroup::AddPlot1D(const TString &name, std::shared_ptr<Plot1D> plot)
200 {
201 AddNGHist<TH1D>(name, plot);
202 }
203
207 inline void PlotGroup::AddPlot2D(const TString &name, std::shared_ptr<Plot2D> plot)
208 {
209 AddNGHist<TH2D>(name, plot);
210 }
211
215 inline void PlotGroup::AddPlot3D(const TString &name, std::shared_ptr<Plot3D> plot)
216 {
217 AddNGHist<TH3D>(name, plot);
218 }
219
220} // namespace NAGASH
Some global definitions.
SystPolicy
Policies of dealing with systematic uncertainties.
Definition HistBase.h:33
@ Maximum
take the maximum difference in the same group.
Helper functions for std::map.
Definition MapTool.h:17
std::map< TString, T >::iterator FindMostSimilar(std::map< TString, T > &mymap, const TString &key)
Find value to the most similar key in a map.
Definition MapTool.h:43
NAGASH interface for using ROOT histograms.
Definition NGHist.h:18
Provide a base class for manipulating a group of histograms at the same time.
Definition PlotGroup.h:18
std::shared_ptr< Plot2D > BookPlot2D(const TString &name, Args &&...args)
Book NGHist<TH2D>
Definition PlotGroup.h:156
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 AddNGHist(const TString &name, std::shared_ptr< NGHist< HistType > > plot)
Append a NGHist to this PlotGroup.
Definition PlotGroup.h:131
std::shared_ptr< NGHist< HistType > > GetNGHist(const TString &name)
Get booked NGHist with given name.
Definition PlotGroup.h:109
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
std::shared_ptr< NGHist< HistType > > BookNGHist(const TString &name, Args &&...args)
Book NGHist and store in this PlotGroup.
Definition PlotGroup.h:87
std::shared_ptr< Plot3D > GetPlot3D(const TString &name)
Get NGHist<TH3D>
Definition PlotGroup.h:191
void AddPlot2D(const TString &name, std::shared_ptr< Plot2D > plot)
Append a NGHist<TH2D> to this PlotGroup.
Definition PlotGroup.h:207
std::shared_ptr< Plot1D > GetPlot1D(const TString &name)
Get NGHist<TH1D>
Definition PlotGroup.h:175
std::shared_ptr< Plot3D > BookPlot3D(const TString &name, Args &&...args)
Book NGHist<TH3D>
Definition PlotGroup.h:167
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.
std::shared_ptr< Plot1D > BookPlot1D(const TString &name, Args &&...args)
Book NGHist<TH1D>
Definition PlotGroup.h:145
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 AddPlot3D(const TString &name, std::shared_ptr< Plot3D > plot)
Append a NGHist<TH3D> to this PlotGroup.
Definition PlotGroup.h:215
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.
std::shared_ptr< Plot2D > GetPlot2D(const TString &name)
Get NGHist<TH2D>
Definition PlotGroup.h:183
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.
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.
virtual ~PlotGroup()=default
void AddPlot1D(const TString &name, std::shared_ptr< Plot1D > plot)
Append a NGHist<TH1D> to this PlotGroup.
Definition PlotGroup.h:199
Provide virtual interface to manipulate all the results inside NAGASH.
Definition Result.h:68
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