TTTT_Analysis.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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>("nLepGood");
  24. mt.track_branch_ptr<float>("LepGood_pt");
  25. mt.track_branch_ptr<float>("LepGood_eta");
  26. mt.track_branch_ptr<float>("LepGood_phi");
  27. mt.track_branch_ptr<float>("LepGood_mass");
  28. mt.track_branch_ptr<float>("LepGood_mcPt");
  29. mt.track_branch_ptr<int>("LepGood_charge");
  30. mt.track_branch<int>("nJet");
  31. mt.track_branch_ptr<float>("Jet_pt");
  32. mt.track_branch_ptr<float>("Jet_eta");
  33. mt.track_branch_ptr<float>("Jet_phi");
  34. mt.track_branch_ptr<float>("Jet_mass");
  35. mt.track_branch_ptr<float>("Jet_btagCMVA");
  36. mt.track_branch<int>("nGenTop");
  37. mt.track_branch<int>("ngenLep");
  38. mt.track_branch_ptr<int>("genLep_sourceId");
  39. mt.track_branch_ptr<float>("genLep_pt");
  40. }
  41. void declare_values(MiniTreeDataSet& mt){
  42. auto& mean = GenFunction::register_function<float(vector<float>)>("mean", FUNC(([](vector<float> v){
  43. int n = 0;
  44. float sum = 0;
  45. for (float e : v){
  46. n++;
  47. sum += e;
  48. }
  49. return n>0 ? sum / n : 0;
  50. })));
  51. new WrapperVector<float>("nLepGood", "LepGood_pt", "LepGood_pt");
  52. new WrapperVector<float>("nLepGood", "LepGood_eta", "LepGood_eta");
  53. new WrapperVector<float>("nLepGood", "LepGood_phi", "LepGood_phi");
  54. new WrapperVector<float>("nLepGood", "LepGood_mass", "LepGood_mass");
  55. auto& get_energy = GenFunction::register_function<float(float,float,float,float)>("get_energy",
  56. FUNC(([](float pt, float eta, float phi, float m){
  57. TLorentzVector t;
  58. t.SetPtEtaPhiM(pt, eta, phi, m);
  59. return t.E();
  60. })));
  61. auto lepton_energy = new ZipMapFour<float, float>(get_energy, "LepGood_pt", "LepGood_eta", "LepGood_phi", "LepGood_mass");
  62. GenValue::alias("lepton_energy", lepton_energy);
  63. GenValue::alias("avg_lepton_energy", new Reduce<float>(mean , "lepton_energy"));
  64. GenValue::alias("max_lepton_energy", new Max<float>("lepton_energy"));
  65. new Filter("nLepGood>=3", [nLepGood=lookup("nLepGood")](){
  66. return dynamic_cast<Value<int>*>(nLepGood)->get_value() >=3;});
  67. new Filter("nLepGood<=4", [nLepGood=lookup("nLepGood")](){
  68. return dynamic_cast<Value<int>*>(nLepGood)->get_value() <=4;});
  69. new RangeFilter<int>("3<=nLepGood<5", dynamic_cast<Value<int>*>(lookup("nLepGood")), 3, 5);
  70. }
  71. void declare_containers(MiniTreeDataSet& mt){
  72. mt.register_container(new ContainerTH1I("lepton_count", "Lepton Multiplicity", lookup("nLepGood"), 8, 0, 8));
  73. mt.register_container(new ContainerTH1I("top_quark_count", "Top Quark Multiplicity", lookup("nGenTop"), 8, 0, 8));
  74. mt.register_container(new ContainerTH1FMany("lepton_pt_all", "Lepton Pt - All", lookup("LepGood_pt"), 50, 0, 500));
  75. mt.register_container(new ContainerTH1I("nLepGood2", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  76. mt.get_container("nLepGood2")->add_filter(lookup_filter("3<=nLepGood<5"));
  77. mt.register_container(new ContainerTH1I("nLepGood3", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  78. mt.get_container("nLepGood3")->add_filter(!(*lookup_filter("3<=nLepGood<5")));
  79. mt.register_container(new ContainerTGraph("nLepvsnJet", new Pair<int, int>("nLepGood", "nJet") ));
  80. mt.register_container(new ContainerTH1F("avg_lepton_energy", "Average Lepton Energy", lookup("avg_lepton_energy"), 50, 0, 500));
  81. mt.register_container(new ContainerTH1F("max_lepton_energy", "Maximum Lepton Energy", lookup("max_lepton_energy"), 50, 0, 500));
  82. }
  83. void run_analysis(const std::string& filename){
  84. TFile *f = TFile::Open(filename.c_str());
  85. TTree *tree = (TTree*) f->Get("tree");
  86. MiniTreeDataSet mt(tree);
  87. enable_branches(mt);
  88. declare_values(mt);
  89. declare_containers(mt);
  90. mt.process();
  91. mt.save_all();
  92. }
  93. int main(int argc, char * argv[])
  94. {
  95. Log::init_logger("log.txt", LogPriority::kLogDebug);
  96. ArgParser args(argc, argv);
  97. if(args.cmdOptionExists("-f")) {
  98. run_analysis(args.getCmdOption("-f"));
  99. }
  100. else {
  101. cout << "Usage: ./main -f input_minitree.root" << endl;
  102. }
  103. return 0;
  104. }