HistCollection.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef histcollection_h
  2. #define histcollection_h
  3. #include <string>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <map>
  7. #include <cmath>
  8. #include <TCanvas.h>
  9. #include <TH1.h>
  10. #include <TH2.h>
  11. #define PI 3.14159
  12. #define PDG_ELECTRON = 11
  13. #define PDG_MUON = 13
  14. struct HistWithPlottingInfo{
  15. TH1* hist;
  16. std::string draw_string;
  17. HistWithPlottingInfo()
  18. :hist(0),draw_string(""){};
  19. HistWithPlottingInfo(TH1* hist, std::string draw_string)
  20. :hist(hist),draw_string(draw_string){};
  21. };
  22. class HistCollection{
  23. private:
  24. map<std::string, HistWithPlottingInfo> histograms;
  25. std::string sample_name;
  26. std::string hist_id(const std::string &id){
  27. return sample_name+"_"+id;
  28. }
  29. TH1D* book_histogram_1d(const std::string &id, const std::string &title,
  30. int nbins, double min, double max,
  31. const std::string draw_string = ""){
  32. std::string full_id = hist_id(id);
  33. TH1D *hist = new TH1D(full_id.c_str(), title.c_str(), nbins, min, max);
  34. histograms[id] = HistWithPlottingInfo(hist, draw_string);
  35. return hist;
  36. };
  37. TH2D* book_histogram_2d(const std::string id, const std::string title,
  38. int nbins_x, double min_x, double max_x,
  39. int nbins_y, double min_y, double max_y){
  40. std::string full_id = hist_id(id);
  41. TH2D *hist = new TH2D(full_id.c_str(), title.c_str(), nbins_x, min_x, max_x, nbins_y, min_y, max_y);
  42. histograms[id] = HistWithPlottingInfo(hist, "COLZ");
  43. return hist;
  44. };
  45. public:
  46. // List of included histogram objects
  47. TH1D *lepton_count;
  48. TH1D *lepton_count_pass_miniiso;
  49. TH1D *delta_pt;
  50. TH2D *lepton_count_vs_delta_pt;
  51. TH1D *top_quark_count;
  52. TH1D *jet_count_trilepton;
  53. TH1D *jet_count_ss_dilepton;
  54. TH1D *jet_count_os_dilepton;
  55. TH1D *jet_count;
  56. TH1D *b_jet_discriminator;
  57. TH1D *b_jet_count;
  58. TH1D *b_jet_pt_all;
  59. TH1D *b_jet_pt_3_or_more;
  60. TH1D *trilepton_iso_1;
  61. TH1D *trilepton_iso_2;
  62. TH1D *trilepton_iso_3;
  63. HistCollection(const std::string &sample_name)
  64. : sample_name(sample_name)
  65. {
  66. // ---------------------------------------------
  67. lepton_count = book_histogram_1d("lepton_count", "Lepton Multiplicity",
  68. 8, 0, 8);
  69. // ---------------------------------------------
  70. lepton_count_pass_miniiso = book_histogram_1d("lepton_count_pass_miniiso",
  71. "Number of Leptons passing mini-isolation",
  72. 8, 0, 8);
  73. // ---------------------------------------------
  74. delta_pt = book_histogram_1d("delta_pt", "{\\Delta P_T}",
  75. 100, -50, 50);
  76. // ---------------------------------------------
  77. lepton_count_vs_delta_pt = book_histogram_2d("lepton_count_vs_delta_pt",
  78. "Good Lepton Count Vs {\\Delta P_T}",
  79. 10, 0, 10, 100, -50, 50);
  80. // ---------------------------------------------
  81. top_quark_count = book_histogram_1d("top_quark_count", "Top Quark Multiplicity",
  82. 4, 0, 4);
  83. // ---------------------------------------------
  84. jet_count_trilepton = book_histogram_1d("jet_count_trilepton",
  85. "Jet Multiplicity - Trilepton",
  86. 13, 0, 13);
  87. jet_count_ss_dilepton = book_histogram_1d("jet_count_ss_dilepton",
  88. "Jet Multiplicity - Same-Sign Dilepton",
  89. 13, 0, 13);
  90. jet_count_os_dilepton = book_histogram_1d("jet_count_os_dilepton",
  91. "Jet Multiplicity - Opposite-Sign Dilepton",
  92. 13, 0, 13);
  93. // ---------------------------------------------
  94. jet_count = book_histogram_1d("jet_count", "Jet Multiplicity",
  95. 13, 0, 13);
  96. // ---------------------------------------------
  97. b_jet_discriminator = book_histogram_1d("b_jet_discriminator", "B-Jet Discriminator",
  98. 40, -1, 1);
  99. // ---------------------------------------------
  100. b_jet_count = book_histogram_1d("b_jet_count", "B-Jet Multiplicity",
  101. 10, 0, 10);
  102. // ---------------------------------------------
  103. b_jet_pt_all = book_histogram_1d("b_jet_pt_all", "B-Jet {P_T} - All",
  104. 50, 0, 2000);
  105. // ---------------------------------------------
  106. b_jet_pt_3_or_more = book_histogram_1d("b_jet_pt_3_or_more", "B-Jet {P_T} - 3 or more",
  107. 50, 0, 2000);
  108. // ---------------------------------------------
  109. trilepton_iso_1 = book_histogram_1d("trilepton_iso_1", "Trilepton Isolation - First",
  110. 50, 0, 2*PI);
  111. // ---------------------------------------------
  112. trilepton_iso_2 = book_histogram_1d("trilepton_iso_2", "Trilepton Isolation - Second",
  113. 50, 0, 2*PI);
  114. // ---------------------------------------------
  115. trilepton_iso_3 = book_histogram_1d("trilepton_iso_3", "Trilepton Isolation - Third",
  116. 50, 0, 2*PI);
  117. // ---------------------------------------------
  118. }
  119. HistCollection(): HistCollection("") {};
  120. ~HistCollection(){
  121. for(auto& p: histograms)
  122. delete p.second.hist;
  123. }
  124. void draw(TCanvas* canvas,
  125. std::pair<int, int> shape = {0,0}){
  126. std::pair<int, int> null_shape(0,0);
  127. if (shape == null_shape){
  128. int n = (int)ceil(sqrt(histograms.size()));
  129. shape.first = n;
  130. shape.second = n;
  131. }
  132. canvas->Clear();
  133. canvas->Divide(shape.first, shape.second);
  134. int i = 1;
  135. for(auto& p: histograms){
  136. canvas->cd(i++);
  137. p.second.hist->SetStats(false);
  138. p.second.hist->Draw(p.second.draw_string.c_str());
  139. }
  140. }
  141. std::string get_sample_name(){
  142. return sample_name;
  143. }
  144. vector<std::string> get_ids(){
  145. vector<std::string> vec;
  146. for(auto& p: histograms)
  147. vec.push_back(p.first);
  148. return vec;
  149. }
  150. TH1* get_by_id(const std::string &id){
  151. TH1* hist = histograms[id].hist;
  152. return hist;
  153. }
  154. };
  155. #endif // histcollection_h