TTTT Analysis  0.1
TTTT_Analysis.cpp
Go to the documentation of this file.
1 
37 #include <iostream>
38 #include <vector>
39 #include <utility>
40 #include <numeric>
41 #include <limits>
42 
43 #include "filval/filval.hpp"
44 #include "filval_root/filval_root.hpp"
45 
46 #include "analysis/common/obj_types.hpp"
47 #include "analysis/common/constants.hpp"
48 #include "analysis/selection.hpp"
49 
50 #include "MiniTreeDataSet.hpp"
51 #include <TSystem.h>
52 
53 using namespace std;
54 using namespace fv;
55 using namespace fv::root;
56 
57 void enable_extra_branches(MiniTreeDataSet& mt){
58  mt.track_branch<int>("nBJetLoose40");
59  mt.track_branch<int>("nBJetMedium40");
60  mt.track_branch<int>("nBJetTight40");
61 
62 
63  mt.track_branch< int >("run" );
64  mt.track_branch< int >("lumi");
65  mt.track_branch< int >("evt" );
66  mt.track_branch<float>("xsec");
67 }
68 
69 
70 void declare_values(MiniTreeDataSet& mt){
71 
72  // Define a couple selections to be used in the top-mass reconstruction.
73  auto& b_mva_filter = GenFunction::register_function<bool(Particle)>("b_mva_filter",
74  FUNC(([cut=B_JET_WP](const Particle& j){
75  return j.jet.b_cmva > cut;
76  })));
77  auto& b_pdgid_filter = GenFunction::register_function<bool(Particle)>("b_pdgid_filter",
78  FUNC(([](const Particle& j){
79  return j.genpart.pdgId == 5 || j.genpart.pdgId==-5;
80  })));
81  auto& w_mass_filter = GenFunction::register_function<bool(Particle, Particle)>("w_mass_filter",
82  FUNC(([win_l=W_MASS-10, win_h=W_MASS+10](const Particle& j1, const Particle& j2){
83  float inv_mass = (j1.v + j2.v).M();
84  return inv_mass > win_l && inv_mass < win_h;
85  })));
86  auto& dup_filter = GenFunction::register_function<bool(std::tuple<Particle,Particle>,Particle)>("dup_filter",
87  FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
88  int j0 = b.idx;
89  int j1 = std::get<0>(w).idx;
90  int j2 = std::get<1>(w).idx;
91  return (j0 != j1) && (j0 != j2) && (j1 != j2);
92  })));
93  auto& qg_id_filter = GenFunction::register_function<bool(Particle, Particle)>("qg_id_filter",
94  FUNC(([](const Particle& j1, const Particle& j2){
95  // require both particles be either quarks(not Top) or gluons
96  int id1 = abs(j1.genpart.pdgId);
97  int id2 = abs(j2.genpart.pdgId);
98  return ((id1 >=1 && id1 <= 5) || id1 == 21) &&
99  ((id2 >=1 && id2 <= 5) || id2 == 21);
100  })));
101 
102  // Here is the calculation of the Top Reconstructed Mass from Particle
103  auto jets = lookup<std::vector<Particle>>("jets");
104 
105  auto b_jets = filter(b_mva_filter, jets, "reco_b_jets");
106  auto w_dijets = tup_filter<Particle,Particle>(w_mass_filter, combinations<Particle,2>(jets, "reco_dijets"));
107 
108  auto top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
109 
110  top_cands = tup_filter(dup_filter, top_cands);
111 
112  auto& t_mass = GenFunction::register_function<float(std::tuple<Particle,Particle>,Particle)>("t_mass",
113  FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
114  return (std::get<0>(w).v+std::get<1>(w).v+b.v).M();
115  })));
116 
117  fv::map(t_mass, top_cands, "reco_top_mass");
118 
119  // Here is the calculation of the Top Reconstructed Mass from Generator-Level objects
120  jets = lookup<std::vector<Particle>>("mc_jets");
121 
122  b_jets = filter(b_pdgid_filter, jets);
123 
124  w_dijets = tup_filter(qg_id_filter, combinations<Particle,2>(jets));
125  w_dijets = tup_filter(w_mass_filter, w_dijets);
126 
127  top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
128 
129  top_cands = tup_filter(dup_filter, top_cands);
130 
131 
132  fv::map(t_mass, top_cands, "mc_top_mass");
133 
134 
135  // calculation of di-jet inv-mass spectrum
136  auto& inv_mass2 = GenFunction::register_function<float(Particle, Particle)>("inv_mass2",
137  FUNC(([] (const Particle& j1, const Particle& j2){
138  TLorentzVector sum = j1.v + j2.v;
139  return (float)sum.M();
140  })));
141  fv::map(inv_mass2, lookup<std::vector<std::tuple<Particle,Particle>>>("reco_dijets"), "dijet_inv_mass");
142 
143 
144 
145  count<float>(GenFunction::register_function<bool(float)>("bJet_Selection",
146  FUNC(([](float x){
147  return x>0;
148  }))), "Jet_btagCMVA", "b_jet_count");
149 
150  auto &is_electron = GenFunction::register_function<bool(int)>("is_electron",
151  FUNC(([](int pdgId) {
152  return abs(pdgId) == 11;
153  })));
154  auto &is_muon = GenFunction::register_function<bool(int)>("is_muon",
155  FUNC(([](int pdgId) {
156  return abs(pdgId) == 13;
157  })));
158  auto &is_lepton = GenFunction::register_function<bool(int)>("is_lepton",
159  FUNC(([ie=&is_electron, im=&is_muon](int pdgId) {
160  return (*ie)(pdgId) || (*im)(pdgId);
161  })));
162 
163  count<int>(is_electron, "GenPart_pdgId", "genEle_count");
164  count<int>(is_muon, "GenPart_pdgId", "genMu_count");
165  count<int>(is_lepton, "GenPart_pdgId", "genLep_count");
166 
167  count<int>(is_electron, "LepGood_pdgId", "recEle_count");
168  count<int>(is_muon, "LepGood_pdgId", "recMu_count");
169  count<int>(is_lepton, "LepGood_pdgId", "recLep_count");
170 
171 
172  fv::pair<int, int>("genEle_count", "recEle_count", "genEle_count_v_recEle_count");
173  fv::pair<int, int>("genMu_count", "recMu_count", "genMu_count_v_recMu_count");
174  fv::pair<int, int>("genLep_count", "recLep_count", "genLep_count_v_recLep_count");
175 
176  /* auto& sum = GenFunction::register_function<float(std::vector<float>)>("sum", */
177  /* FUNC(([](const std::vector<float>& v){ */
178  /* return std::accumulate(v.begin(), v.end(), 0); */
179  /* }))); */
180 
181  /* auto sum_jet_pt = fv::apply(sum, lookup<std::vector<float>>("Jet_pt")); */
182 
183 
184  obs_filter("trilepton", FUNC(([nLepGood=lookup<int>("nLepGood")]()
185  {
186  return dynamic_cast<Value<int>*>(nLepGood)->get_value() == 3;
187  })));
188 
189  obs_filter("os-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
190  {
191  auto& charges = LepGood_charge->get_value();
192  return charges.size()==2 && (charges[0] != charges[1]);
193  })));
194 
195  obs_filter("ss-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
196  {
197  auto& charges = LepGood_charge->get_value();
198  return charges.size()==2 && (charges[0] == charges[1]);
199  })));
200 
201 }
202 
203 void declare_containers(MiniTreeDataSet& mt){
204 
205  mt.register_container<ContainerTH1<int>>("lepton_count", "Lepton Multiplicity", lookup<int>("nLepGood"), 8, 0, 8);
206 
207  mt.register_container<ContainerTH1Many<float>>("Jet_pt_dist", "Jet P_T",
208  lookup<vector<float>>("Jet_pt"), 50, 0, 500,
209  "Jet P_T");
210  mt.register_container<ContainerTH1Many<float>>("Jet_eta_dist", "Jet Eta",
211  lookup<vector<float>>("Jet_eta"), 50, -3, 3,
212  "Jet Eta");
213  mt.register_container<ContainerTH1Many<float>>("Jet_phi_dist", "Jet Phi",
214  lookup<vector<float>>("Jet_phi"), 20, -PI, PI,
215  "Jet Phi");
216  mt.register_container<ContainerTH1Many<float>>("Jet_mass_dist", "Jet Mass",
217  lookup<vector<float>>("Jet_mass"), 50, 0, 200,
218  "Jet Mass");
219 
220  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass", "Di-Jet Inv. Mass - All",
221  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
222  "Di-Jet Mass");
223  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_osdilepton", "Di-Jet Inv. Mass - OS Dilepton",
224  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
225  "Di-Jet Mass")->add_filter(lookup_obs_filter("os-dilepton"));
226  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_ssdilepton", "Di-Jet Inv. Mass - SS Dilepton",
227  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
228  "Di-Jet Mass")->add_filter(lookup_obs_filter("ss-dilepton"));
229  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_trilepton", "Di-Jet Inv. Mass - Trilepton",
230  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
231  "Di-Jet Mass")->add_filter(lookup_obs_filter("trilepton"));
232 
233  mt.register_container<ContainerTH1Many<float>>("reco_top_mass", "Reconstructed Top mass",
234  lookup<vector<float>>("reco_top_mass"), 100, 0, 500,
235  "Tri-jet invarient mass");
236  /* mt.register_container<ContainerTH1Many<float>>("reco_top_pt", "Reconstructed Top Pt", */
237  /* lookup<vector<float>>("reco_top_pt"), 100, 0, 500, */
238  /* "Tri-jet Pt"); */
239 
240  mt.register_container<ContainerTH1Many<float>>("mc_top_mass", "Reconstructed MC Top mass",
241  lookup<vector<float>>("mc_top_mass"), 100, 0, 500,
242  "Tri-jet invarient mass");
243 
244 
245  mt.register_container<ContainerTH2<int>>("nLepvsnJet", "Number of Leptons vs Number of Jets",
246  fv::pair<int, int>("nLepGood", "nJet"),
247  7, 0, 7, 20, 0, 20,
248  "Number of Leptons", "Number of Jets");
249 
250 
251  mt.register_container<ContainerTH2<int>>("genEle_count_v_recEle_count", "Number of Generated Electrons v. Number of Reconstructed Electrons",
252  lookup<std::pair<int,int>>("genEle_count_v_recEle_count"),
253  10, 0, 10, 10, 0, 10,
254  "Generated Electrons","Reconstructed Electrons");
255 
256  mt.register_container<ContainerTH2<int>>("genMu_count_v_recMu_count", "Number of Generated Muons v. Number of Reconstructed Muons",
257  lookup<std::pair<int,int>>("genMu_count_v_recMu_count"),
258  10, 0, 10, 10, 0, 10,
259  "Generated Muons","Reconstructed Muons");
260 
261  mt.register_container<ContainerTH2<int>>("genLep_count_v_recLep_count", "Number of Generated Leptons v. Number of Reconstructed Leptons (e & mu only)",
262  lookup<std::pair<int,int>>("genLep_count_v_recLep_count"),
263  10, 0, 10, 10, 0, 10,
264  "Generated Leptons","Reconstructed Leptons");
265 
266  mt.register_container<ContainerTH1<int>>("b_jet_count", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10);
267 
268 
269  mt.register_container<ContainerTH1<int>>("jet_count_os_dilepton", "Jet Multiplicity - OS Dilepton Events",
270  lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("os-dilepton"));
271  mt.register_container<ContainerTH1<int>>("jet_count_ss_dilepton", "Jet Multiplicity - SS Dilepton Events",
272  lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("ss-dilepton"));
273  mt.register_container<ContainerTH1<int>>("jet_count_trilepton", "Jet Multiplicity - Trilepton Events",
274  lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("trilepton"));
275 
276 
277  mt.register_container<CounterMany<int>>("GenPart_pdgId_counter", lookup<vector<int>>("GenPart_pdgId"));
278 
279 
280  mt.register_container<Vector<std::vector< int >>>("GenPart_pdgId", lookup<std::vector< int >>("GenPart_pdgId"));
281  mt.register_container<Vector<std::vector< int >>>("GenPart_motherIndex", lookup<std::vector< int >>("GenPart_motherIndex"));
282  mt.register_container<Vector<std::vector< int >>>("GenPart_motherId", lookup<std::vector< int >>("GenPart_motherId"));
283  mt.register_container<Vector<std::vector<float>>>("GenPart_pt", lookup<std::vector<float>>("GenPart_pt"));
284  mt.register_container<Vector<std::vector<float>>>("GenPart_eta", lookup<std::vector<float>>("GenPart_eta"));
285  mt.register_container<Vector<std::vector<float>>>("GenPart_phi", lookup<std::vector<float>>("GenPart_phi"));
286  mt.register_container<Vector<std::vector<float>>>("GenPart_mass", lookup<std::vector<float>>("GenPart_mass"));
287  mt.register_container<Vector<std::vector<float>>>("GenPart_energy", lookup<std::vector<float>>("GenPart_energy"));
288  mt.register_container<Vector<std::vector< int >>>("GenPart_status", lookup<std::vector< int >>("GenPart_status"));
289 
290  mt.register_container<Vector<vector< int >>>("LepGood_mcMatchPdgId", lookup<vector< int >>("LepGood_mcMatchPdgId"));
291 
292  mt.register_container<Vector< int >>("run", lookup< int >("run") );
293  mt.register_container<Vector< int >>("lumi", lookup< int >("lumi"));
294  mt.register_container<Vector< int >>("evt", lookup< int >("evt") );
295  mt.register_container<Vector<float>>("xsec", lookup<float>("xsec"));
296 
297  mt.register_container<Vector<std::vector< int >>>("Jet_mcMatchFlav", lookup<std::vector< int >>("Jet_mcMatchFlav"));
298 
299  mt.register_container<Vector<std::vector<float>>>("Jet_pt", lookup<std::vector<float>>("Jet_pt"));
300  mt.register_container<Vector<std::vector<float>>>("Jet_eta", lookup<std::vector<float>>("Jet_eta"));
301  mt.register_container<Vector<std::vector<float>>>("Jet_phi", lookup<std::vector<float>>("Jet_phi"));
302  mt.register_container<Vector<std::vector<float>>>("Jet_mass", lookup<std::vector<float>>("Jet_mass"));
303 
304 }
305 
306 
307 void run_analysis(const std::string& input_filename, bool silent){
308  gSystem->Load("libfilval.so");
309  auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
310  return input.substr(0, input.find_last_of(".")) + new_suffix;
311  };
312  string log_filename = replace_suffix(input_filename, "_result.log");
313  fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
314 
315  string output_filename = replace_suffix(input_filename, "_result.root");
316  MiniTreeDataSet mt(output_filename, input_filename);
317 
318  create_all_common_values(mt);
319  enable_extra_branches(mt);
320  declare_values(mt);
321  declare_containers(mt);
322 
323  mt.process(silent);
324  mt.save_all();
325 }
326 
327 int main(int argc, char * argv[])
328 {
329  fv::util::ArgParser args(argc, argv);
330  if(!args.cmdOptionExists("-f")) {
331  cout << "Usage: ./main (-s) -f input_minitree.root" << endl;
332  return -1;
333  }
334  bool silent = args.cmdOptionExists("-s");
335  string input_filename = args.getCmdOption("-f");
336  run_analysis(input_filename, silent);
337 }
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:299
STL namespace.
The namespace containing all filval classes and functions.
Definition: api.hpp:46
Definition: api.hpp:8