TTTT_Analysis.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include "TFile.h"
  5. #include "TTree.h"
  6. #include "TCanvas.h"
  7. #include "filval/filval.hpp"
  8. #include "filval_root/filval_root.hpp"
  9. #include "filval/log.hpp"
  10. #include "MiniTreeDataSet.hpp"
  11. using namespace std;
  12. using namespace fv;
  13. using namespace fv::root;
  14. using namespace fv::util;
  15. GenValue* lookup(const string& name){
  16. return GenValue::get_value(name);
  17. }
  18. Filter* lookup_filter(const string& name){
  19. return dynamic_cast<Filter*>(GenValue::get_value(name));
  20. }
  21. void enable_branches(MiniTreeDataSet& mt){
  22. mt.fChain->SetBranchStatus("*", false);
  23. mt.track_branch<int>("nJet");
  24. mt.track_branch<int>("nLepGood");
  25. mt.track_branch_ptr<float>("LepGood_pt");
  26. mt.track_branch_ptr<float>("LepGood_eta");
  27. mt.track_branch_ptr<float>("LepGood_phi");
  28. mt.track_branch_ptr<float>("LepGood_mass");
  29. }
  30. void declare_values(MiniTreeDataSet& mt){
  31. auto& mean = GenFunction::register_function<float(vector<float>)>("mean", FUNC(([](vector<float> v){
  32. int n = 0;
  33. float sum = 0;
  34. for (float e : v){
  35. n++;
  36. sum += e;
  37. }
  38. return n>0 ? sum / n : 0;
  39. })));
  40. auto pt_wrapper = new WrapperVector<float>("nLepGood", "LepGood_pt");
  41. auto eta_wrapper = new WrapperVector<float>("nLepGood", "LepGood_eta");
  42. auto phi_wrapper = new WrapperVector<float>("nLepGood", "LepGood_phi");
  43. auto m_wrapper = new WrapperVector<float>("nLepGood", "LepGood_mass");
  44. /* auto dxy_wrapper = new WrapperVector<float>("nLepGood", "LepGood_dxy"); */
  45. auto& get_energy = GenFunction::register_function<float(float,float,float,float)>("get_energy", FUNC(([](float pt, float eta, float phi, float m){
  46. TLorentzVector t;
  47. t.SetPtEtaPhiM(pt, eta, phi, m);
  48. return t.E();
  49. })));
  50. auto lepton_energy = new ZipMapFour<float, float>(get_energy, pt_wrapper, eta_wrapper, phi_wrapper, m_wrapper);
  51. GenValue::alias("lepton_energy", lepton_energy);
  52. GenValue::alias("avg_lepton_energy", new Reduce<float>(mean , "lepton_energy"));
  53. GenValue::alias("max_lepton_energy", new Max<float>("lepton_energy"));
  54. new Filter("nLepGood>=3", [nLepGood=lookup("nLepGood")](){
  55. return dynamic_cast<Value<int>*>(nLepGood)->get_value() >=3;});
  56. new Filter("nLepGood<=4", [nLepGood=lookup("nLepGood")](){
  57. return dynamic_cast<Value<int>*>(nLepGood)->get_value() <=4;});
  58. new RangeFilter<int>("3<=nLepGood<5", dynamic_cast<Value<int>*>(lookup("nLepGood")), 3, 5);
  59. }
  60. void declare_containers(MiniTreeDataSet& mt){
  61. mt.register_container(new ContainerTH1I("nLepGood", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  62. mt.register_container(new ContainerTH1I("nLepGood2", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  63. mt.get_container("nLepGood2")->add_filter(lookup_filter("3<=nLepGood<5"));
  64. mt.register_container(new ContainerTH1I("nLepGood3", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  65. mt.get_container("nLepGood3")->add_filter(!(*lookup_filter("3<=nLepGood<5")));
  66. /* mt.register_container(new ContainerTGraph("nLepvsnJet", new Pair<int, int>("nLepGood", "nJet") )); */
  67. mt.register_container(new ContainerTH1F("avg_lepton_energy", "Average Lepton Energy", lookup("avg_lepton_energy"), 50, 0, 500));
  68. mt.register_container(new ContainerTH1F("max_lepton_energy", "Maximum Lepton Energy", lookup("max_lepton_energy"), 50, 0, 500));
  69. }
  70. void run_analysis(const std::string& filename){
  71. TFile *f = TFile::Open(filename.c_str());
  72. TTree *tree = (TTree*) f->Get("tree");
  73. MiniTreeDataSet mt(tree);
  74. enable_branches(mt);
  75. declare_values(mt);
  76. declare_containers(mt);
  77. mt.process();
  78. mt.save_all();
  79. /* save_containers(mt); */
  80. }
  81. int main(int argc, char * argv[])
  82. {
  83. Log::init_logger("log.txt", LogPriority::kLogDebug);
  84. ArgParser args(argc, argv);
  85. if(args.cmdOptionExists("-f")) {
  86. run_analysis(args.getCmdOption("-f"));
  87. }
  88. else {
  89. cout << "Usage: ./main -f input_minitree.root" << endl;
  90. }
  91. return 0;
  92. }