MiniTreeDataSet.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef minitreedataset_h
  2. #define minitreedataset_h
  3. #include <string>
  4. #include <tuple>
  5. #include "filval/filval.hpp"
  6. #include "filval_root/filval_root.hpp"
  7. #include "MiniTree.hpp"
  8. using namespace std;
  9. using namespace filval;
  10. using namespace filval::root;
  11. class MiniTreeDataSet : public DataSet,
  12. public MiniTree{
  13. private:
  14. long next_entry;
  15. long nentries;
  16. bool load_next(){
  17. if (next_entry >= nentries) return false;
  18. fChain->GetEntry(next_entry);
  19. ++next_entry;
  20. return true;
  21. }
  22. int get_events(){
  23. return nentries;
  24. }
  25. int get_current_event(){
  26. return next_entry-1;
  27. }
  28. void enable_branches(){
  29. fChain->SetBranchStatus("*", false);
  30. track_branch<int>("nJet");
  31. track_branch<int>("nLepGood");
  32. track_branch_ptr<float>("LepGood_pt");
  33. track_branch_ptr<float>("LepGood_eta");
  34. track_branch_ptr<float>("LepGood_phi");
  35. track_branch_ptr<float>("LepGood_mass");
  36. }
  37. public:
  38. MiniTreeDataSet(TTree *tree)
  39. :MiniTree(tree){
  40. enable_branches();
  41. auto lookup = [](const string& name){ return GenValue::get_value(name);};
  42. auto lookup_filter = [](const string& name){ return dynamic_cast<Filter*>(GenValue::get_value(name));};
  43. auto mean = [](vector<float> v){
  44. int n = 0;
  45. float sum = 0;
  46. for (float e : v){
  47. n++;
  48. sum += e;
  49. }
  50. return n>0 ? sum / n : 0;
  51. };
  52. auto pt_wrapper = new WrapperVector<float>("LepGood_pt_vec", "nLepGood", "LepGood_pt");
  53. auto eta_wrapper = new WrapperVector<float>("LepGood_eta_vec", "nLepGood", "LepGood_eta");
  54. auto phi_wrapper = new WrapperVector<float>("LepGood_phi_vec", "nLepGood", "LepGood_phi");
  55. auto m_wrapper = new WrapperVector<float>("LepGood_mass_vec", "nLepGood", "LepGood_mass");
  56. auto get_energy = [](float pt, float eta, float phi, float m){
  57. TLorentzVector t;
  58. t.SetPtEtaPhiM(pt, eta, phi, m);
  59. return t.E();
  60. };
  61. new ZipMapFour<float, float>("lepton_energy", get_energy, pt_wrapper, eta_wrapper, phi_wrapper, m_wrapper);
  62. new Reduce<float>("avg_lepton_energy", mean , "lepton_energy");
  63. new Filter("nLepGood>=3", [nLepGood=lookup("nLepGood")](){
  64. return dynamic_cast<Value<int>*>(nLepGood)->get_value() >=3;});
  65. new Filter("nLepGood<=4", [nLepGood=lookup("nLepGood")](){
  66. return dynamic_cast<Value<int>*>(nLepGood)->get_value() <=4;});
  67. new RangeFilter<int>("3<=nLepGood<5", dynamic_cast<Value<int>*>(lookup("nLepGood")), 3, 5);
  68. add_container(new ContainerTH1I("nLepGood", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
  69. add_container(new ContainerTH1I("nLepGood2", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
  70. containers.at("nLepGood2")->add_filter(lookup_filter("3<=nLepGood<5"));
  71. add_container(new ContainerTH1I("nLepGood3", "Lepton Multiplicity", 10, 0, 10, lookup("nLepGood")));
  72. containers.at("nLepGood3")->add_filter(!(*lookup_filter("3<=nLepGood<5")));
  73. new Pair<int, int>("(nLepGood,nJet)", "nLepGood", "nJet");
  74. add_container(new ContainerTGraph("nLepvsnJet", lookup("(nLepGood,nJet)")));
  75. add_container(new ContainerTH1F("avg_lepton_energy", "Average Lepton Energy", 50, 0, 500, lookup("avg_lepton_energy")));
  76. add_container(new ContainerTH1F("max_lepton_energy", "Maximum Lepton Energy", 50, 0, 500, lookup("max_lepton_energy")));
  77. next_entry = 0;
  78. nentries = fChain->GetEntriesFast();
  79. }
  80. template <typename T>
  81. Value<T>* track_branch(const std::string& bname){
  82. T* bref = (T*) fChain->GetBranch(bname.c_str())->GetAddress();
  83. fChain->SetBranchStatus(bname.c_str(), true);
  84. cout << "Registering branch \"" << bname
  85. << "\" with address " << bref
  86. << " and type " << typeid(bref).name() << endl;
  87. return new ObservedValue<T>(bname, bref);
  88. }
  89. template <typename T>
  90. Value<T*>* track_branch_ptr(const std::string& bname){
  91. T* bref = (T*) fChain->GetBranch(bname.c_str())->GetAddress();
  92. fChain->SetBranchStatus(bname.c_str(), true);
  93. cout << "Registering pointer branch \"" << bname
  94. << "\" with address " << bref
  95. << " and type " << typeid(bref).name() << endl;
  96. return new ConstantValue<T*>(bname, bref);
  97. }
  98. void register_container(GenContainer* container){
  99. containers[container->get_name()] = container;
  100. }
  101. /* void process(){ */
  102. /* cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; */
  103. /* cout << "In process" << endl; */
  104. /* cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; */
  105. /* auto x = dynamic_cast<ConstantValue<float*>*>(values["LepGood_pt"]); */
  106. /* cout << x->get_value() << endl; */
  107. /* /1* foo(); *1/ */
  108. /* while( load_next() ){ */
  109. /* /1* foo(); *1/ */
  110. /* cout << x->get_value() << endl; */
  111. /* GenValue::reset(); */
  112. /* for(auto con : containers) */
  113. /* con.second->fill(); */
  114. /* /1* foo(); *1/ */
  115. /* } */
  116. /* } */
  117. };
  118. #endif // minitreedataset_h