TTTT Analysis  0.1
/home/caleb/Sources/TTTT/MiniTreeDataSet.hpp
1 #ifndef minitreedataset_h
2 #define minitreedataset_h
3 
4 #include <string>
5 #include <tuple>
6 
7 #include "filval/filval.hpp"
8 #include "filval_root/filval_root.hpp"
9 #include "MiniTree.hpp"
10 
11 using namespace std;
12 using namespace filval;
13 using namespace filval::root;
14 
15 class MiniTreeDataSet : public DataSet,
16  public MiniTree{
17  private:
18  long next_entry;
19  long nentries;
20  bool load_next(){
21  if (next_entry >= nentries) return false;
22  fChain->GetEntry(next_entry);
23  ++next_entry;
24  return true;
25  }
26  int get_events(){
27  return nentries;
28  }
29  int get_current_event(){
30  return next_entry-1;
31  }
32 
33  void enable_branches(){
34  fChain->SetBranchStatus("*", false);
35  track_branch<int>("nJet");
36  track_branch<int>("nLepGood");
37  track_branch_ptr<float>("LepGood_pt");
38  track_branch_ptr<float>("LepGood_eta");
39  track_branch_ptr<float>("LepGood_phi");
40  track_branch_ptr<float>("LepGood_mass");
41  }
42  public:
43  MiniTreeDataSet(TTree *tree)
44  :MiniTree(tree){
45  enable_branches();
46  auto lookup = [](const string& name){ return GenValue::get_value(name);};
47  auto lookup_filter = [](const string& name){ return dynamic_cast<Filter*>(GenValue::get_value(name));};
48  auto mean = [](vector<float> v){
49  int n = 0;
50  float sum = 0;
51  for (float e : v){
52  n++;
53  sum += e;
54  }
55  return n>0 ? sum / n : 0;
56  };
57 
58  auto pt_wrapper = new WrapperVector<float>("LepGood_pt_vec", "nLepGood", "LepGood_pt");
59  auto eta_wrapper = new WrapperVector<float>("LepGood_eta_vec", "nLepGood", "LepGood_eta");
60  auto phi_wrapper = new WrapperVector<float>("LepGood_phi_vec", "nLepGood", "LepGood_phi");
61  auto m_wrapper = new WrapperVector<float>("LepGood_mass_vec", "nLepGood", "LepGood_mass");
62 
63  auto get_energy = [](float pt, float eta, float phi, float m){
64  TLorentzVector t;
65  t.SetPtEtaPhiM(pt, eta, phi, m);
66  return t.E();
67  };
68  new ZipMapFour<float, float>("lepton_energy", get_energy, pt_wrapper, eta_wrapper, phi_wrapper, m_wrapper);
69 
70  new Reduce<float>("avg_lepton_energy", mean , "lepton_energy");
71 
72 
73  new Filter("nLepGood>=3", [nLepGood=lookup("nLepGood")](){
74  return dynamic_cast<Value<int>*>(nLepGood)->get_value() >=3;});
75 
76  new Filter("nLepGood<=4", [nLepGood=lookup("nLepGood")](){
77  return dynamic_cast<Value<int>*>(nLepGood)->get_value() <=4;});
78  new RangeFilter<int>("3<=nLepGood<5", dynamic_cast<Value<int>*>(lookup("nLepGood")), 3, 5);
79 
80 
81  add_container(new ContainerTH1I("nLepGood", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
82 
83  add_container(new ContainerTH1I("nLepGood2", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
84  containers.at("nLepGood2")->add_filter(lookup_filter("3<=nLepGood<5"));
85 
86  add_container(new ContainerTH1I("nLepGood3", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
87  containers.at("nLepGood3")->add_filter(!(*lookup_filter("3<=nLepGood<5")));
88 
89  new Pair<int, int>("(nLepGood,nJet)", "nLepGood", "nJet");
90  add_container(new ContainerTGraph("nLepvsnJet", lookup("(nLepGood,nJet)")));
91 
92  add_container(new ContainerTH1F("avg_lepton_energy", "Average Lepton Energy", 50, 0, 500, lookup("avg_lepton_energy")));
93  add_container(new ContainerTH1F("max_lepton_energy", "Maximum Lepton Energy", 50, 0, 500, lookup("max_lepton_energy")));
94 
95  next_entry = 0;
96  nentries = fChain->GetEntriesFast();
97  }
98 
99  template <typename T>
100  Value<T>* track_branch(const std::string& bname){
101  T* bref = (T*) fChain->GetBranch(bname.c_str())->GetAddress();
102  fChain->SetBranchStatus(bname.c_str(), true);
103  cout << "Registering branch \"" << bname
104  << "\" with address " << bref
105  << " and type " << typeid(bref).name() << endl;
106  return new ObservedValue<T>(bname, bref);
107  }
108 
109  template <typename T>
110  Value<T*>* track_branch_ptr(const std::string& bname){
111  T* bref = (T*) fChain->GetBranch(bname.c_str())->GetAddress();
112  fChain->SetBranchStatus(bname.c_str(), true);
113  cout << "Registering pointer branch \"" << bname
114  << "\" with address " << bref
115  << " and type " << typeid(bref).name() << endl;
116  return new ConstantValue<T*>(bname, bref);
117  }
118 
119  void register_container(GenContainer* container){
120  containers[container->get_name()] = container;
121  }
122  /* void process(){ */
123  /* cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; */
124  /* cout << "In process" << endl; */
125  /* cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; */
126  /* auto x = dynamic_cast<ConstantValue<float*>*>(values["LepGood_pt"]); */
127  /* cout << x->get_value() << endl; */
128  /* /1* foo(); *1/ */
129  /* while( load_next() ){ */
130  /* /1* foo(); *1/ */
131  /* cout << x->get_value() << endl; */
132  /* GenValue::reset(); */
133  /* for(auto con : containers) */
134  /* con.second->fill(); */
135  /* /1* foo(); *1/ */
136  /* } */
137  /* } */
138 
139 };
140 #endif // minitreedataset_h
Definition: container.hpp:10
Definition: filter.hpp:43
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:481
The namespace containing all filval classes and functions.
Definition: container.hpp:7
Definition: MiniTreeDataSet.hpp:15
A std::vector wrapper around a C-style array.
Definition: value.hpp:218
Definition: MiniTree.hpp:18
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:247
Definition: container.hpp:8
Takes a set of four Value<std::vector<T> > objects and a function of four Ts and returns a std::vecto...
Definition: value.hpp:278
Definition: container.hpp:34
Definition: container.hpp:43
Definition: container.hpp:89
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:325
A generic, observed, value.
Definition: value.hpp:144
Definition: filter.hpp:12
Definition: dataset.hpp:8