HackAnalysis  2
BaseAnalysis.h
1 
2 /*
3 
4 // My implementation of an analysis container. Written from scratch.
5 // Functions similarly to MadAnalysis analyses, so we have to define
6 // void init(), void Execute(std::mt19937 &engine), void Finalise() for a
7 // new analysis *but* I also require a constructor to be defined (with some standard stuff).
8 //
9 //
10 // Signal regions defined via:
11 // AddRegionSelection("region name");
12 // or
13 // AddRegionSelection(<vector of names>);
14 
15 // Cuts are defined via
16 // AddCut("cut name",<signal region>);
17 // or
18 // AddCut("cut name",<vector of region names>);
19 
20 // Cuts are applied by
21 // ApplyCut(bool condition,const std::string& cutname);
22 
23 // Histogramming is done here via Yoda, so AddYodaHist1D in the init, FillYodaHist1D in the analysis
24 // etc.
25 
26 
27 */
28 
29 
30 
31 #pragma once
32 
33 #include "heputil.h"
34 #include <iostream>
35 #include <stdio.h>
36 #include <string.h>
37 #include <map>
38 #include <random>
39 #include <iomanip>
40 // Define cutflow objects and helper functions
41 #include "useYoda.h"
42 #include <cstdarg>
43 
44 // include the various headers
45 #include <algorithm>
46 #include "smearing.h"
47 #include "isolation.h"
48 #include "kinematics.h"
49 #include "lester_mt2_bisect.h"
50 
51 
52 using namespace std;
53 
54 
55 class cut {
56  public:
57  double weight_left; // to deal with central cut
58  double weight_square_left;
59  // new multiweights starting here
60  vector<double> weights_left;
61  vector<double> weights_squared_left;
62  int number_left;
63  string name;
64  bool passed;
65  cut();
66 
67  cut(std::string cutname);
68  ~cut();
69 };
70 
71 class cutflow {
72  private:
73 
74  std::map<std::string, int> cutmap;
75  //double initial_weight;
76  //int intial events;
77 
78  public:
79  std::string _name;
80  std::vector<cut*> cuts;
81  bool passed;
82 
83  cutflow();
84  ~cutflow();
85  cutflow(const std::string& name);
86 
87  void addcut(const std::string& cutname);
88 
89  void applycut(const std::string& cutname,bool &condition, double &weight);
90 
91  void applycut(const std::string& cutname,bool &condition,double &centralweight, vector<double> &weights);
92 
93  void geteff_and_err(double &eff,double &err);
94  void geteff_and_err(const std::string& cutname,double &eff,double &err);
95  void geteff_and_all_errs(const std::string& cutname,vector<vector<int>> &weight_ensembles,double &eff,double &staterr,double &syserrplus, double &syserrminus);
96 
97  void print(ostream &ss,vector<vector<int>> &weight_ensembles);
98 
99  void print(ostream &ss);
100 
101  void print();
102 };
103 
104 
105 
107 
108 private:
109  //vector<YODA::AnaylsisObject*> YodaObjects;
110  map<string, YODA::Histo1D*> _YodaHisto1Ds;
111  map<string, YODA::Histo2D*> _YodaHisto2Ds;
112  map<string, YODA::Profile1D*> _YodaProfile1Ds;
113 
114 
115 public:
116  bool _isMaster=false;
117 
118  std::string analysisname;
119  double _totalweight;
120  vector<double> _totalweights; // now multiweights ...
121  vector<vector<int>> _weight_ensembles;
122  double _xsection;
123  int _totalevents;
124  HEP::Event *Event;
125 
126  std::vector<cutflow*> cutflows;
127  std::map<std::string, std::vector<cutflow*>> regionmap; // maps from the name of the cut to a list of the relevant cutflows
128  std::map<std::string, cutflow*> regions; // maps from the region names to the regions themselves
129  double eventweight;
130 
131  vector<double> _eventweights; // now multiweights ...
132 
133  //std::string PythiaFillFunction;
134 
135  std::string DetectorFunction;
136 
137 
138 
139  virtual ~BaseAnalysis() {}; // complains if we don't define this.
140 
141  virtual void init() {}; // need to add the brackets for all virtual functions
142 
143  virtual void Execute(std::mt19937 &engine) {};
144 
145  virtual void Finalise() {};
146 
147 
148  // To do: take this out of the init so that it only displays once per analysis ...
149  //virtual void splash ();
150 
151  //void SetDetector(const std::string& detector);
152 
153  //void CheckDetector(const std::vector<const std::string>& detectorlist);
154 
155  void cleanup();
156 
157  void setup();
158  void setup(bool ismaster);
159 
160  bool ProcessEvent(HEP::Event *evnt);
161 
162  void AddRegionSelection(const std::string &region_name);
163 
164  void AddCut(const std::string& cutname,const std::string& region_name);
165 
166  void AddCut(const std::string& cutname,const std::vector<std::string> region_names);
167 
168  void ApplyCut(bool condition,const std::string& cutname);
169 
170  //void AddYodaHisto1D(const std::string& objectname, ...);
171  void AddYodaHisto1D(const std::string& objectname);
172  void AddYodaHisto1D(const std::string& objectname, size_t nbins, double lower, double upper);
173  void AddYodaHisto1D(const std::string& objectname, const std::vector<double>& binedges);
174  void FillYodaHisto1D(const std::string& objectname, double x, double fraction=1.0);
175  YODA::Histo1D* GetYodaHisto1D(const std::string& objectname);
176 
177  void AddYodaHisto2D(const std::string& objectname);
178  void AddYodaHisto2D(const std::string& objectname, size_t nbinsX,size_t nbinsY, double lowerX, double upperX, double lowerY, double upperY);
179  void AddYodaHisto2D(const std::string& objectname, const std::vector<double>& xedges, const std::vector<double>& yedges);
180  void FillYodaHisto2D(const std::string& objectname, double x, double y, double fraction=1.0);
181  YODA::Histo2D* GetYodaHisto2D(const std::string& objectname);
182 
183  void AddYodaProfile1D(const std::string& objectname);
184  void AddYodaProfile1D(const std::string& objectname, size_t nbins, double lower, double upper);
185  void AddYodaProfile1D(const std::string& objectname, const std::vector<double>& binedges);
186  void FillYodaProfile1D(const std::string& objectname, double x, double y, double fraction=1.0);
187  YODA::Profile1D* GetYodaProfile1D(const std::string& objectname);
188 
189  void WriteHistos(ostream &os);
190 
191 
192  void set_weight(double weight);
193  void set_weights(vector<double> &weights);
194 
195  void Reweight(double weight_multiplier);
196  double get_weight();
197 
198  void print_cutflows();
199  void set_xsection(double xs);
200 
201 
202  void print_cutflows(ostream &os);
203  void write_results(ostream &ss);
204  void write_json(ostream &ss);
205  bool CheckConvergence(double margin);
206 
207  //BaseAnalysis& operator = (const BaseAnalysis& B);
208 
209  //BaseAnalysis& operator += (const BaseAnalysis& B);
210  void add( BaseAnalysis& B);
211 };
Definition: BaseAnalysis.h:106
Simple event class, separating particles into classes.
Definition: heputil.h:227
Definition: BaseAnalysis.h:55
Definition: BaseAnalysis.h:71