9 #ifndef CROMBIE_PLOTTOOLS_PLOTPREPARER_H 10 #define CROMBIE_PLOTTOOLS_PLOTPREPARER_H 1 19 #include "TTreeFormula.h" 48 PlotInfo(TH1D*
hist, Double_t& expr, Double_t& cut, Double_t& weight, Double_t eventsper)
49 : hist{hist}, expr{expr}, cut{cut}, weight{weight}, eventsper{eventsper} {}
51 ~PlotInfo() {
for (
auto* p : envs)
delete p; envs.clear(); }
58 std::vector<EnvelopeInfo*>
envs;
74 void AddHist ( TString FileBase, Int_t NumXBins, Double_t *XBins, TString dataExpr, TString mcExpr, TString cut, TString dataWeight, TString mcWeight );
76 void AddHist ( TString FileBase, Int_t NumXBins, Double_t MinX, Double_t MaxX, TString dataExpr, TString mcExpr, TString cut, TString dataWeight, TString mcWeight );
80 void AddEnvelopeWeight ( TString weight, Int_t index = 1 ) { env_branches.push_back(make_pair(weight, index)); }
86 std::vector<TH1D*> GetHistList ( TString FileBase,
FileType type );
89 bool fPrepared {
false};
93 std::map<TString, std::map<FileType, std::vector<TH1D*>>>
fHistograms;
95 std::map<TString, std::map<TString, std::pair<TTreeFormula*, Double_t>>>
fFormulas;
98 std::map<TString, std::vector<PlotInfo*>>
fPlots;
101 std::map<TString, std::map<TString, std::vector<PlotInfo*>>>
fOutPlots;
110 void RunFile (
FileInfo& info)
override;
129 for (
auto&
file : fPlots) {
130 for (
auto plot :
file.second)
133 file.second.resize(0);
139 for (
auto formulas : fFormulas)
140 for (
auto form : formulas.second)
141 delete form.second.first;
148 PlotPreparer::AddHist(TString FileBase, Int_t NumXBins, Double_t MinX, Double_t MaxX, TString dataExpr, TString mcExpr, TString cut, TString dataWeight, TString mcWeight)
150 bool reset_per = (fEventsPer == 0);
152 SetEventsPer((MaxX - MinX)/NumXBins);
154 Double_t XBins[NumXBins+1];
155 ConvertToArray(NumXBins, MinX, MaxX, XBins);
156 AddHist(FileBase, NumXBins, XBins, dataExpr, mcExpr, cut, dataWeight, mcWeight);
163 PlotPreparer::AddHist(TString FileBase, Int_t NumXBins, Double_t* XBins, TString dataExpr, TString mcExpr, TString cut, TString dataWeight, TString mcWeight)
165 DisplayFunc(__func__);
166 Message(eDebug,
"File Name:", FileBase);
167 Message(eDebug,
"Data expr:", dataExpr);
168 Message(eDebug,
"MC expr:", mcExpr);
169 Message(eDebug,
"Cut:", cut);
170 Message(eDebug,
"Data cut:", dataWeight);
171 Message(eDebug,
"MC cut:", mcWeight);
173 if (fHistograms.find(FileBase) == fHistograms.end())
174 fHistograms[FileBase] = {};
176 auto& hists = fHistograms[FileBase];
179 if (env_branches.size())
180 output_is_up[FileBase] = env_up;
183 if (hists.find(type) == hists.end())
186 auto& hist_list = hists[type];
188 auto exprs = (type == FileType::kData) ?
189 std::vector<TString>{dataExpr, cut, dataWeight} : std::vector<TString>{mcExpr, cut, mcWeight};
190 auto expr = exprs[0];
191 auto weight = exprs[2];
194 for (
auto env : env_branches)
195 exprs.push_back(
env.first);
197 const auto& infos = *(GetFileInfo(type));
198 for (
auto info : infos) {
200 auto& outplots = fOutPlots[FileBase][info->fTreeName];
201 auto& inputname = info->fFileName;
202 auto& plots = fPlots[inputname];
203 auto& formulas = fFormulas[inputname];
205 for (
auto& expr : exprs) {
206 if (formulas.find(expr) == formulas.end()) {
207 Message(eDebug, inputname,
"Adding formula expression:", expr);
208 formulas[expr] = std::make_pair<TTreeFormula*, Double_t>(
nullptr, {});
212 auto tempname = TempHistName();
213 PlotInfo* tempinfo =
new PlotInfo(
new TH1D(tempname, tempname, NumXBins, XBins), formulas[expr].second, formulas[cut].second, formulas[weight].second, fEventsPer);
214 for (
auto env : env_branches) {
215 auto tempname = TempHistName();
216 tempinfo->
envs.push_back(
new EnvelopeInfo(
new TProfile(tempname, tempname, NumXBins, XBins), formulas[
env.first].second,
env.second));
219 plots.push_back(tempinfo);
220 hist_list.push_back(tempinfo->
hist);
221 outplots.push_back(tempinfo);
224 env_branches.clear();
230 Message(eDebug,
"Getting hists for FileName", FileBase,
"and type", type);
231 return fHistograms.at(FileBase).at(type);
244 for (
auto&
env : output_is_up) {
248 for (
auto& procs : fOutPlots[
env.first]) {
253 std::vector<TProfile*> profiles;
254 for (
auto *prof_info : fOutPlots[
env.first][procs.first][0]->envs) {
255 TProfile* temp_scale =
static_cast<TProfile*
>(prof_info->hist->Clone(TempHistName()));
256 temp_scale->Reset(
"M");
257 profiles.push_back(temp_scale);
259 for (
auto*
hist : procs.second) {
260 for (
unsigned i_prof = 0; i_prof <
hist->envs.size(); ++i_prof)
261 profiles[i_prof]->Add(
hist->envs[i_prof]->hist);
264 for (
int i_bin = 1; i_bin <= profiles[0]->GetNbinsX(); ++i_bin) {
266 for (
auto* prof : profiles) {
267 auto check = prof->GetBinContent(i_bin);
268 if ((check > scale) ==
env.second)
272 for (
auto*
hist : procs.second) {
273 auto content =
hist->hist->GetBinContent(i_bin);
274 auto error =
hist->hist->GetBinError(i_bin);
275 hist->hist->SetBinContent(i_bin, content * scale);
276 hist->hist->SetBinError(i_bin, error * scale);
279 for (
auto* prof : profiles)
291 auto* inputfile = TFile::Open(
filename);
292 TTree* inputtree = fTreeName.Contains(
"/") ?
static_cast<TTree*
>(inputfile->Get(fTreeName)) : static_cast<TTree*>(inputfile->FindObjectAny(fTreeName));
295 std::set<TString> needed;
297 auto& formulas = fFormulas[
filename];
298 for (
auto& formula : formulas) {
300 delete formula.second.first;
301 formula.second.first =
new TTreeFormula(formula.first, formula.first, inputtree);
306 inputtree->SetBranchStatus(
"*", 0);
307 for (
auto need : needed)
308 inputtree->SetBranchStatus(need, 1);
310 auto nentries = inputtree->GetEntries();
313 for (decltype(nentries) i_entry = 0; i_entry < nentries; ++i_entry) {
314 inputtree->GetEntry(i_entry);
316 for (
auto& formula : formulas)
317 if (formula.second.first) {
318 formula.second.second = formula.second.first->EvalInstance();
321 for (
auto plot : plots) {
323 if (std::isnan(plot->weight)) {
325 Message(eError,
"For file",
filename,
"row", i_entry,
"NaN weight", plot->weight,
"and expr", plot->expr);
328 plot->hist->Fill(plot->expr, plot->weight);
329 for (
auto*
env : plot->envs)
330 env->hist->Fill(plot->expr,
env->env_expr, plot->weight);
341 DisplayFunc(__func__);
343 const auto& infos = *(GetFileInfo(type));
344 for (
auto* info : infos) {
346 Message(eDebug,
"Scaling plots from", info->fFileName);
347 auto& plots = fPlots[info->fFileName];
349 for (
auto plot : plots) {
350 auto*
hist = plot->hist;
351 auto tempname = TempHistName();
362 if (info->fXSec > 0) {
363 hist->Scale(info->fXSecWeight);
364 for (
auto*
env : plot->envs) {
365 auto denom = info->fRenormHistogram.GetBinContent(std::min(
env->renorm_idx,
366 info->fRenormHistogram.GetNbinsX()));
368 env->hist->Scale(info->fRenormHistogram.GetBinContent(1)/denom);