make_predictions_systs_fakedata.C
Go to the documentation of this file.
1 //analysis will pick up the list of variables and cuts, and extrapoltion types
2 //option will pick up dataset (e.g. concats, syst shifted), and list of systematics
3 
6 #include "CAFAna/Core/Loaders.h"
7 #include "CAFAna/Cuts/Cuts.h"
10 #include "CAFAna/Cuts/SpillCuts.h"
15 #include "CAFAna/Systs/Systs.h"
17 #include "CAFAna/Systs/BeamSysts.h"
19 #include "CAFAna/Vars/HistAxes.h"
21 
22 #include <TFile.h>
23 #include <TString.h>
24 #include <vector>
25 #include <iostream>
26 //TODO test w $DEVSREPO
27 #include "/nova/app/users/ecatanom/nova_code/shared_thesis/devsrepo/thesis_S17-08-22/nue/systs2017/syst_variations.h"
28 
29 using namespace ana;
30 
31 
32 void make_predictions_systs_fakedata(const TString option, const TString analysis = "allxp_nue", const TString period = "full", const TString outdir="./")
33 {
34 
35  auto loaders = GetLoaders2017FakeData(option, period);
36  loaders->SetSpillCut(kStandardSpillCuts);
37 
38  auto shifts = GetShifts2017 (option);
39 
40  auto xpdefs = GetExtrapolationDefs(analysis, period);
41 
42  bool run_nxp = analysis.Contains("nxp_only");
43  bool run_xp_prop = analysis.Contains("xp_prop")|| analysis.Contains ("allxp_nue");
44  bool run_xp_combo = analysis.Contains("xp_combo") || analysis.Contains ("allxp_nue");
45  bool run_xp_numu = analysis.Contains("xp_numu") || analysis.Contains ("allxp_numu");
46 
47  if(run_nxp) {
48  std::cerr << "No extrapolation means no fake data. Use your other predicion nxp" << std::endl;
49  return;
50  }
51 
52  if(analysis.Contains("allxp_nue") && analysis.Contains ("allxp_numu")){
53  std::cerr << "All nue and numu xp? Ambitious much?" << std::endl;
54  return;
55  }
56 
58 
59  struct PredDef{
60  const IPrediction * pred;
61  const TString xp_type;
62  const TString syst_name;
63  const TString sigma_name;
64  const TString edef_name;
65  };
66 
67  struct GenDefFake{
68  const IPredictionGenerator *gen;
69  const TString xp_type;
70  const TString edef_name;
71  const TString syst_name;
72  const TString sigma_name;
73  };
74 
75  std::vector < std::vector <GenDefFake> > gens;
76  std::vector < std::vector <PredDef> > preds;
77 
78  //I'm gonna append fake in the name so there's no confusion, code needs to break
79  //Remember: fake data xp needs to be compared to shifted fdmc, NOT nominal
80  for(auto const &edef:xpdefs) {
81  std::vector <GenDefFake> temp_gens;
82  for (auto const &shift:shifts) {
83  if(run_xp_prop)
84  temp_gens.push_back({new NuePropExtrapGenerator(edef.axis,
86  edef.fdcut,
87  edef.nuendcut,
88  edef.numundcut,
89  shift.shift, //shift fake data
90  weight),
91  "pred_fake_xp_prop", edef.name, shift.syst_name, shift.sigma_name});
92 
93  if(run_xp_combo)
94  temp_gens.push_back({new NueComboExtrapGenerator(edef.axis,
96  edef.fdcut,
97  edef.nuendcut,
98  edef.numundcut,
99  shift.shift,
100  weight) ,
101  "pred_fake_xp_combo",edef.name, shift.syst_name, shift.sigma_name});
102 
103  if(run_xp_numu)
104  temp_gens.push_back({new NumuExtrapGenerator(edef.axis,
105  edef.fdcut,
106  edef.numundcut,
107  shift.shift,
108  weight),
109  "pred_fake_xp_numu",edef.name, shift.syst_name, shift.sigma_name});
110 
111  } //end shifts
112  gens.emplace_back(temp_gens);
113  } //end edef
114 
115 
116  for (auto const &temp_gens:gens){
117  std::vector <PredDef> temp_preds;
118  for (auto const &gen:temp_gens){
119  auto pred = gen.gen->Generate(*loaders,
120  kNoShift); // nominal MC
121  temp_preds.push_back({pred.release(),gen.xp_type,
122  gen.syst_name,gen.sigma_name,gen.edef_name});
123  }
124  preds.emplace_back(temp_preds);
125  }//end var/cut/shift combos
126 
127  loaders->Go();
128 
129 
130  //mkdir is so dumb
131  //hadd is the worst. had to split predictions a little
132 
133  for(uint gIdx=0; gIdx < gens.size(); ++gIdx){
134  TString filename = (outdir + gens[gIdx][0].xp_type + "_" + gens[gIdx][0].edef_name + "_" +
135  period + "_" + option + ".root");
136 
137  auto file = new TFile (filename,"recreate");
138 
139  for(uint sIdx = 0; sIdx < shifts.size(); ++sIdx){
140  if(preds[gIdx][sIdx].syst_name != shifts[sIdx].syst_name ||
141  preds[gIdx][sIdx].xp_type != gens[gIdx][sIdx].xp_type) {
142  std::cerr << "ERROR: Loops - you're doing it wrong \n"
143  << "Expected gen" << gIdx << " " << gens[gIdx][sIdx].xp_type
144  << " and syst " << sIdx << " " << shifts[sIdx].syst_name
145  << "; but got pred " << preds[gIdx][sIdx].xp_type << " " << preds[gIdx][sIdx].syst_name
146  << std::endl;
147  return;
148  }//end check
149  auto dir = file->GetDirectory(preds[gIdx][sIdx].xp_type+"_"+preds[gIdx][sIdx].syst_name);
150  if(!dir) dir = file->mkdir(preds[gIdx][sIdx].xp_type+"_"+preds[gIdx][sIdx].syst_name);
151 
152  auto dir2 = dir->GetDirectory(preds[gIdx][sIdx].sigma_name);
153  if(!dir2) dir2 = dir->mkdir(preds[gIdx][sIdx].sigma_name);
154 
155  preds[gIdx][sIdx].pred->SaveTo(dir2, preds[gIdx][sIdx].edef_name);
156  } //end shifts
157  delete file;
158  std::cout << "Saved predictions to " << filename
159  << " (" << gIdx+1 << "/" << gens.size() << ")"
160  << std::endl;
161  } //end generators
162 
163  std::cout << "Done" << std::endl;
164 }
ana::Loaders * GetLoaders2017FakeData(const TString option, const TString period="full")
std::map< TString, IPredictionGenerator * > gens
Definition: syst_header.h:387
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const Var kPPFXFluxCVWgt
Definition: PPFXWeights.h:16
const Var weight
Struct to hold prediction information.
OStream cerr
Definition: OStream.cxx:7
string filename
Definition: shutoffs.py:106
std::vector< ShiDef > GetShifts2017(const TString option)
int gIdx
Definition: show_event.C:12
Generates extrapolated Numu predictions.
Generates extrapolated Nue predictions using ProportionalDecomp.
const HistAxis axis_numu_for_nuesig
std::vector< ExtrapDef > GetExtrapolationDefs(const TString analysis, const TString period)
const SystShifts kNoShift
Definition: SystShifts.cxx:21
OStream cout
Definition: OStream.cxx:6
void make_predictions_systs_fakedata(const TString option, const TString analysis="allxp_nue", const TString period="full", const TString outdir="./")
TDirectory * dir
Definition: macro.C:5
std::vector< Loaders * > loaders
Definition: syst_header.h:386
Generates extrapolated Nue predictions using Michel+BEN decomposition.
TFile * file
Definition: cellShifts.C:17
Standard interface to all prediction techniques.
Definition: IPrediction.h:57
const SpillCut kStandardSpillCuts
Apply this unless you&#39;re doing something special.
Definition: SpillCuts.h:49
Given loaders and an MC shift, Generate() generates an IPrediction.
const Var kXSecCVWgt2017
Definition: XsecTunes.h:37
const std::string outdir
unsigned int uint