TTTT_Analysis.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "log.hpp"
  10. #include "MiniTreeDataSet.hpp"
  11. using namespace std;
  12. using namespace filval;
  13. using namespace filval::root;
  14. void print_pair(Pair<double, double> dp){
  15. pair<double, double> p = dp.get_value();
  16. cout << "(" << p.first << ", " << p.second << ")\n";
  17. }
  18. void test1(){
  19. double x = 12;
  20. double y = 12;
  21. ObservedValue<double> x_val("x", &x);
  22. ObservedValue<double> y_val("y", &y);
  23. Pair<double, double> dp(&x_val, &y_val);
  24. print_pair(dp);
  25. x = 2;
  26. y = 2;
  27. print_pair(dp);
  28. ContainerVector<double> cont("cont", &x_val);
  29. x = 12;
  30. cont.fill();
  31. x = 2;
  32. cont.fill();
  33. auto *container = cont.get_container();
  34. for( auto v: *container )
  35. cout << v << ", ";
  36. cout << endl;
  37. }
  38. void test2(){
  39. double x = 12;
  40. ObservedValue<double> x_val("x", &x);
  41. ContainerTH1D hist("h1", "Hist", &x_val, 20, 0, 20);
  42. hist.fill();
  43. hist.fill();
  44. hist.fill();
  45. x = 11;
  46. hist.fill();
  47. hist.fill();
  48. hist.fill();
  49. hist.fill();
  50. hist.fill();
  51. hist.fill();
  52. TH1D* h = (TH1D*) hist.get_container();
  53. TCanvas can("c1");
  54. h->Draw();
  55. can.Draw();
  56. can.SaveAs("outfile.png");
  57. }
  58. void test3(){
  59. TFile *f = TFile::Open("./data/TTTT_ext_treeProducerSusyMultilepton_tree.root");
  60. TTree *tree = (TTree*) f->Get("tree");
  61. MiniTreeDataSet mtds(tree);
  62. mtds.process();
  63. TCanvas can("c1");
  64. can.Clear();
  65. TH1* hist = ((ContainerTH1I*)mtds.get_container("nLepGood"))->get_container();
  66. hist->Draw();
  67. can.Draw();
  68. can.SaveAs("outfile.png");
  69. can.Clear();
  70. hist = ((ContainerTH1I*)mtds.get_container("nLepGood2"))->get_container();
  71. hist->Draw();
  72. can.Draw();
  73. can.SaveAs("outfile2.png");
  74. can.Clear();
  75. hist = ((ContainerTH1I*)mtds.get_container("nLepGood3"))->get_container();
  76. hist->Draw();
  77. can.Draw();
  78. can.SaveAs("outfile3.png");
  79. can.Clear();
  80. hist = ((ContainerTH1I*)mtds.get_container("avg_lepton_energy"))->get_container();
  81. hist->Draw();
  82. can.Draw();
  83. can.SaveAs("lepton_energy.png");
  84. can.Clear();
  85. TGraph* graph= ((ContainerTGraph*)mtds.get_container("nLepvsnJet"))->get_container();
  86. graph->Draw("A*");
  87. can.Draw();
  88. can.SaveAs("outfileGraph.png");
  89. delete tree;
  90. f->Close();
  91. delete f;
  92. }
  93. GenValue* lookup(const string& name){
  94. return GenValue::get_value(name);
  95. }
  96. Filter* lookup_filter(const string& name){
  97. return dynamic_cast<Filter*>(GenValue::get_value(name));
  98. }
  99. void enable_branches(MiniTreeDataSet* mt){
  100. mt->fChain->SetBranchStatus("*", false);
  101. mt->track_branch<int>("nJet");
  102. mt->track_branch<int>("nLepGood");
  103. mt->track_branch_ptr<float>("LepGood_pt");
  104. mt->track_branch_ptr<float>("LepGood_eta");
  105. mt->track_branch_ptr<float>("LepGood_phi");
  106. mt->track_branch_ptr<float>("LepGood_mass");
  107. }
  108. void declare_values(MiniTreeDataSet* mt){
  109. auto mean = Function<float(vector<float>)>("mean", [](vector<float> v){
  110. int n = 0;
  111. float sum = 0;
  112. for (float e : v){
  113. n++;
  114. sum += e;
  115. }
  116. return n>0 ? sum / n : 0;
  117. });
  118. auto pt_wrapper = new WrapperVector<float>("nLepGood", "LepGood_pt");
  119. auto eta_wrapper = new WrapperVector<float>("nLepGood", "LepGood_eta");
  120. auto phi_wrapper = new WrapperVector<float>("nLepGood", "LepGood_phi");
  121. auto m_wrapper = new WrapperVector<float>("nLepGood", "LepGood_mass");
  122. auto get_energy = Function<float(float,float,float,float)>("get_energy", [](float pt, float eta, float phi, float m){
  123. TLorentzVector t;
  124. t.SetPtEtaPhiM(pt, eta, phi, m);
  125. return t.E();
  126. });
  127. auto lepton_energy = new ZipMapFour<float, float>(get_energy, pt_wrapper, eta_wrapper, phi_wrapper, m_wrapper);
  128. GenValue::alias("lepton_energy", lepton_energy);
  129. GenValue::alias("avg_lepton_energy", new Reduce<float>(mean , "lepton_energy"));
  130. GenValue::alias("max_lepton_energy", new Max<float>("lepton_energy"));
  131. new Filter("nLepGood>=3", [nLepGood=lookup("nLepGood")](){
  132. return dynamic_cast<Value<int>*>(nLepGood)->get_value() >=3;});
  133. new Filter("nLepGood<=4", [nLepGood=lookup("nLepGood")](){
  134. return dynamic_cast<Value<int>*>(nLepGood)->get_value() <=4;});
  135. new RangeFilter<int>("3<=nLepGood<5", dynamic_cast<Value<int>*>(lookup("nLepGood")), 3, 5);
  136. }
  137. void declare_containers(MiniTreeDataSet* mt){
  138. mt->add_container(new ContainerTH1I("nLepGood", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  139. mt->add_container(new ContainerTH1I("nLepGood2", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  140. mt->get_container("nLepGood2")->add_filter(lookup_filter("3<=nLepGood<5"));
  141. mt->add_container(new ContainerTH1I("nLepGood3", "Lepton Multiplicity", lookup("nLepGood"), 10, 0, 10));
  142. mt->get_container("nLepGood3")->add_filter(!(*lookup_filter("3<=nLepGood<5")));
  143. mt->add_container(new ContainerTGraph("nLepvsnJet", new Pair<int, int>("nLepGood", "nJet") ));
  144. mt->add_container(new ContainerTH1F("avg_lepton_energy", "Average Lepton Energy", lookup("avg_lepton_energy"), 50, 0, 500));
  145. mt->add_container(new ContainerTH1F("max_lepton_energy", "Maximum Lepton Energy", lookup("max_lepton_energy"), 50, 0, 500));
  146. }
  147. void run_analysis(){
  148. TFile *f = TFile::Open("./data/TTTT_ext_treeProducerSusyMultilepton_tree.root");
  149. TTree *tree = (TTree*) f->Get("tree");
  150. MiniTreeDataSet mt(tree);
  151. enable_branches(&mt);
  152. declare_values(&mt);
  153. declare_containers(&mt);
  154. mt.process();
  155. }
  156. int main(int argc, const char* argv[]){
  157. clog.rdbuf(new Log("mylog.txt"));
  158. /* run_analysis(); */
  159. clog << "hello log again" << endl;
  160. }