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_pdgid_filter = GenFunction::register_function<bool(Particle)>("b_pdgid_filter",
74  FUNC(([](const Particle& j){
75  return j.genpart.pdgId == 5 || j.genpart.pdgId==-5;
76  })));
77  auto& w_mass_filter = GenFunction::register_function<bool(Particle, Particle)>("w_mass_filter",
78  FUNC(([win_l=W_MASS-10, win_h=W_MASS+10](const Particle& j1, const Particle& j2){
79  float inv_mass = (j1.v + j2.v).M();
80  return inv_mass > win_l && inv_mass < win_h;
81  })));
82  auto& dup_filter = GenFunction::register_function<bool(std::tuple<Particle,Particle>,Particle)>("dup_filter",
83  FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
84  int j0 = b.idx;
85  int j1 = std::get<0>(w).idx;
86  int j2 = std::get<1>(w).idx;
87  return (j0 != j1) && (j0 != j2) && (j1 != j2);
88  })));
89  auto& qg_id_filter = GenFunction::register_function<bool(Particle, Particle)>("qg_id_filter",
90  FUNC(([](const Particle& j1, const Particle& j2){
91  // require both particles be either quarks(not Top) or gluons
92  int id1 = abs(j1.genpart.pdgId);
93  int id2 = abs(j2.genpart.pdgId);
94  return ((id1 >=1 && id1 <= 5) || id1 == 21) &&
95  ((id2 >=1 && id2 <= 5) || id2 == 21);
96  })));
97 
98 
99 
100  auto jets = lookup<std::vector<Particle>>("jets");
101  auto leptons = lookup<std::vector<Particle>>("leptons");
102  auto leading_jet = fv::reduce(leading_particle, jets, "leading_jet");
103  fv::apply(particle_pt, fv::tuple(leading_jet), "leading_jet_pt");
104 
105  auto leading_lepton = fv::reduce(leading_particle, leptons);
106  fv::apply(particle_pt, fv::tuple(leading_lepton), "leading_lepton_pt");
107  fv::apply(lepton_relIso, fv::tuple(leading_lepton), "leading_lepton_relIso");
108 
109  auto b_jets = lookup<std::vector<Particle>>("b_jets");
110 
111  // Here is the calculation of the Top Reconstructed Mass from Particle
112  auto w_dijets = tup_filter<Particle,Particle>(w_mass_filter, combinations<Particle,2>(jets, "reco_dijets"));
113 
114  auto top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
115 
116  top_cands = tup_filter(dup_filter, top_cands);
117 
118  auto& t_mass = GenFunction::register_function<float(std::tuple<Particle,Particle>,Particle)>("t_mass",
119  FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
120  return (std::get<0>(w).v+std::get<1>(w).v+b.v).M();
121  })));
122 
123  fv::map(t_mass, top_cands, "reco_top_mass");
124 
125  // Here is the calculation of the Top Reconstructed Mass from Generator-Level objects
126  jets = lookup<std::vector<Particle>>("mc_jets");
127 
128  b_jets = filter(b_pdgid_filter, jets);
129 
130  w_dijets = tup_filter(qg_id_filter, combinations<Particle,2>(jets));
131  w_dijets = tup_filter(w_mass_filter, w_dijets);
132 
133  top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
134 
135  top_cands = tup_filter(dup_filter, top_cands);
136 
137 
138  fv::map(t_mass, top_cands, "mc_top_mass");
139 
140 
141  // calculation of di-jet inv-mass spectrum
142  auto& inv_mass2 = GenFunction::register_function<float(Particle, Particle)>("inv_mass2",
143  FUNC(([] (const Particle& j1, const Particle& j2){
144  TLorentzVector sum = j1.v + j2.v;
145  return (float)sum.M();
146  })));
147  fv::map(inv_mass2, lookup<std::vector<std::tuple<Particle,Particle>>>("reco_dijets"), "dijet_inv_mass");
148 
149 
150 
151  count<float>(GenFunction::register_function<bool(float)>("bJet_Selection",
152  FUNC(([](float x){
153  return x>0;
154  }))), "Jet_btagCMVA", "b_jet_count");
155 
156  auto &is_electron = GenFunction::register_function<bool(int)>("is_electron",
157  FUNC(([](int pdgId) {
158  return abs(pdgId) == 11;
159  })));
160  auto &is_muon = GenFunction::register_function<bool(int)>("is_muon",
161  FUNC(([](int pdgId) {
162  return abs(pdgId) == 13;
163  })));
164  auto &is_lepton = GenFunction::register_function<bool(int)>("is_lepton",
165  FUNC(([ie=&is_electron, im=&is_muon](int pdgId) {
166  return (*ie)(pdgId) || (*im)(pdgId);
167  })));
168 
169  count<int>(is_electron, "GenPart_pdgId", "genEle_count");
170  count<int>(is_muon, "GenPart_pdgId", "genMu_count");
171  count<int>(is_lepton, "GenPart_pdgId", "genLep_count");
172 
173  count<int>(is_electron, "LepGood_pdgId", "recEle_count");
174  count<int>(is_muon, "LepGood_pdgId", "recMu_count");
175  count<int>(is_lepton, "LepGood_pdgId", "recLep_count");
176 
177 
178  fv::pair<int, int>("genEle_count", "recEle_count", "genEle_count_v_recEle_count");
179  fv::pair<int, int>("genMu_count", "recMu_count", "genMu_count_v_recMu_count");
180  fv::pair<int, int>("genLep_count", "recLep_count", "genLep_count_v_recLep_count");
181 
182  /* auto& sum = GenFunction::register_function<float(std::vector<float>)>("sum", */
183  /* FUNC(([](const std::vector<float>& v){ */
184  /* return std::accumulate(v.begin(), v.end(), 0); */
185  /* }))); */
186 
187  /* auto sum_jet_pt = fv::apply(sum, lookup<std::vector<float>>("Jet_pt")); */
188 
189 
190  obs_filter("trilepton", FUNC(([nLepGood=lookup<int>("nLepGood")]()
191  {
192  return dynamic_cast<Value<int>*>(nLepGood)->get_value() == 3;
193  })));
194 
195  obs_filter("os-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  obs_filter("ss-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
202  {
203  auto& charges = LepGood_charge->get_value();
204  return charges.size()==2 && (charges[0] == charges[1]);
205  })));
206 
207 }
208 
209 void declare_containers(MiniTreeDataSet& mt){
210 
211  mt.register_container<ContainerTH1<int>>("lepton_count", "Lepton Multiplicity", lookup<int>("nLepGood"), 8, 0, 8);
212 
213  mt.register_container<ContainerTH1Many<float>>("Jet_pt_dist", "Jet P_T",
214  lookup<vector<float>>("Jet_pt"), 50, 0, 500,
215  "Jet P_T");
216  mt.register_container<ContainerTH1Many<float>>("Jet_eta_dist", "Jet Eta",
217  lookup<vector<float>>("Jet_eta"), 50, -3, 3,
218  "Jet Eta");
219  mt.register_container<ContainerTH1Many<float>>("Jet_phi_dist", "Jet Phi",
220  lookup<vector<float>>("Jet_phi"), 20, -PI, PI,
221  "Jet Phi");
222  mt.register_container<ContainerTH1Many<float>>("Jet_mass_dist", "Jet Mass",
223  lookup<vector<float>>("Jet_mass"), 50, 0, 200,
224  "Jet Mass");
225 
226  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass", "Di-Jet Inv. Mass - All",
227  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
228  "Di-Jet Mass");
229  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_osdilepton", "Di-Jet Inv. Mass - OS Dilepton",
230  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
231  "Di-Jet Mass")->add_filter(lookup_obs_filter("os-dilepton"));
232  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_ssdilepton", "Di-Jet Inv. Mass - SS Dilepton",
233  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
234  "Di-Jet Mass")->add_filter(lookup_obs_filter("ss-dilepton"));
235  mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_trilepton", "Di-Jet Inv. Mass - Trilepton",
236  lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
237  "Di-Jet Mass")->add_filter(lookup_obs_filter("trilepton"));
238 
239  mt.register_container<ContainerTH1Many<float>>("reco_top_mass", "Reconstructed Top mass",
240  lookup<vector<float>>("reco_top_mass"), 100, 0, 500,
241  "Tri-jet invarient mass");
242  /* mt.register_container<ContainerTH1Many<float>>("reco_top_pt", "Reconstructed Top Pt", */
243  /* lookup<vector<float>>("reco_top_pt"), 100, 0, 500, */
244  /* "Tri-jet Pt"); */
245 
246  mt.register_container<ContainerTH1Many<float>>("mc_top_mass", "Reconstructed MC Top mass",
247  lookup<vector<float>>("mc_top_mass"), 100, 0, 500,
248  "Tri-jet invarient mass");
249 
250 
251  mt.register_container<ContainerTH2<int>>("nLepvsnJet", "Number of Leptons vs Number of Jets",
252  fv::pair<int, int>("nLepGood", "nJet"),
253  7, 0, 7, 20, 0, 20,
254  "Number of Leptons", "Number of Jets");
255 
256 
257  mt.register_container<ContainerTH2<int>>("genEle_count_v_recEle_count", "Number of Generated Electrons v. Number of Reconstructed Electrons",
258  lookup<std::pair<int,int>>("genEle_count_v_recEle_count"),
259  10, 0, 10, 10, 0, 10,
260  "Generated Electrons","Reconstructed Electrons");
261 
262  mt.register_container<ContainerTH2<int>>("genMu_count_v_recMu_count", "Number of Generated Muons v. Number of Reconstructed Muons",
263  lookup<std::pair<int,int>>("genMu_count_v_recMu_count"),
264  10, 0, 10, 10, 0, 10,
265  "Generated Muons","Reconstructed Muons");
266 
267  mt.register_container<ContainerTH2<int>>("genLep_count_v_recLep_count", "Number of Generated Leptons v. Number of Reconstructed Leptons (e & mu only)",
268  lookup<std::pair<int,int>>("genLep_count_v_recLep_count"),
269  10, 0, 10, 10, 0, 10,
270  "Generated Leptons","Reconstructed Leptons");
271 
272  auto jet_count = mt.register_container<ContainerTH1<int>>("jet_count", "B-Jet Multiplicity", lookup<int>("nJet"), 15, 0, 15);
273  mt.cut_set(jet_count,{
274  {event_selection.trilepton, "jet_count_trilepton"},
275  {event_selection.b_jet3, "jet_count_b_jet3"},
276  {event_selection.z_mass_veto, "jet_count_z_mass_veto"},
277  {event_selection.base_sel, "jet_count_base_selection"},
278  });
279  mt.register_container<ContainerTH1<int>>("b_jet_count", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10);
280 
281 
282  mt.register_container<ContainerTH1<int>>("b_jet_count_base_selection", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10)->add_filter(event_selection.base_sel);
283 
284  mt.register_container<ContainerTH1<int>>("jet_count_os_dilepton", "Jet Multiplicity - OS Dilepton Events",
285  lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("os-dilepton"));
286  mt.register_container<ContainerTH1<int>>("jet_count_ss_dilepton", "Jet Multiplicity - SS Dilepton Events",
287  lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("ss-dilepton"));
288 
289 
290  mt.register_container<CounterMany<int>>("GenPart_pdgId_counter", lookup<vector<int>>("GenPart_pdgId"));
291 
292 
293  mt.register_container<Vector<std::vector< int >>>("GenPart_pdgId", lookup<std::vector< int >>("GenPart_pdgId"));
294  mt.register_container<Vector<std::vector< int >>>("GenPart_motherIndex", lookup<std::vector< int >>("GenPart_motherIndex"));
295  mt.register_container<Vector<std::vector< int >>>("GenPart_motherId", lookup<std::vector< int >>("GenPart_motherId"));
296  mt.register_container<Vector<std::vector<float>>>("GenPart_pt", lookup<std::vector<float>>("GenPart_pt"));
297  mt.register_container<Vector<std::vector<float>>>("GenPart_eta", lookup<std::vector<float>>("GenPart_eta"));
298  mt.register_container<Vector<std::vector<float>>>("GenPart_phi", lookup<std::vector<float>>("GenPart_phi"));
299  mt.register_container<Vector<std::vector<float>>>("GenPart_mass", lookup<std::vector<float>>("GenPart_mass"));
300  mt.register_container<Vector<std::vector<float>>>("GenPart_energy", lookup<std::vector<float>>("GenPart_energy"));
301  mt.register_container<Vector<std::vector< int >>>("GenPart_status", lookup<std::vector< int >>("GenPart_status"));
302 
303  mt.register_container<Vector<vector< int >>>("LepGood_mcMatchPdgId", lookup<vector< int >>("LepGood_mcMatchPdgId"));
304  mt.register_container<Vector<vector< int >>>("LepGood_pdgId", lookup<vector< int >>("LepGood_pdgId"));
305 
306  mt.register_container<Vector< int >>("run", lookup< int >("run") );
307  mt.register_container<Vector< int >>("lumi", lookup< int >("lumi"));
308  mt.register_container<Vector< int >>("evt", lookup< int >("evt") );
309  mt.register_container<Vector<float>>("xsec", lookup<float>("xsec"));
310 
311  mt.register_container<Vector<std::vector< int >>>("Jet_mcMatchFlav", lookup<std::vector< int >>("Jet_mcMatchFlav"));
312 
313  mt.register_container<Vector<std::vector<float>>>("Jet_pt", lookup<std::vector<float>>("Jet_pt"));
314  mt.register_container<Vector<std::vector<float>>>("Jet_eta", lookup<std::vector<float>>("Jet_eta"));
315  mt.register_container<Vector<std::vector<float>>>("Jet_phi", lookup<std::vector<float>>("Jet_phi"));
316  mt.register_container<Vector<std::vector<float>>>("Jet_mass", lookup<std::vector<float>>("Jet_mass"));
317 
318  mt.register_container<ContainerTH1<float>>("leading_jet_pt", "Leading Jet Pt", lookup<float>("leading_jet_pt"), 100, 0, 500);
319  mt.register_container<ContainerTH1<float>>("leading_lepton_pt", "Leading Lepton Pt", lookup<float>("leading_lepton_pt"), 100, 0, 500);
320  mt.register_container<ContainerTH1<float>>("leading_lepton_relIso", "Leading Lepton Relative Isolation", lookup<float>("leading_lepton_relIso"), 100, 0, 0.05);
321  mt.register_container<Vector<float>>("leading_lepton_relIso_all", lookup<float>("leading_lepton_relIso"));
322 
323  mt.register_container<PassCount>("trilepton_count", event_selection.trilepton);
324  mt.register_container<PassCount>("b_jet3_count", event_selection.b_jet3);
325  mt.register_container<PassCount>("z_mass_veto_count", event_selection.z_mass_veto);
326 
327  mt.register_container<PassCount>("J4_count", event_selection.J4);
328  mt.register_container<PassCount>("J5_count", event_selection.J5);
329  mt.register_container<PassCount>("J6_count", event_selection.J6);
330 
331  mt.register_container<PassCount>("SR4j_count", event_selection.SR4j);
332  mt.register_container<PassCount>("SR5j_count", event_selection.SR5j);
333  mt.register_container<PassCount>("SR6j_count", event_selection.SR6j);
334 }
335 
336 
337 void run_analysis(const std::string& input_filename, const std::string& data_label, bool silent){
338  gSystem->Load("libfilval.so");
339  auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
340  return input.substr(0, input.find_last_of(".")) + new_suffix;
341  };
342  string log_filename = replace_suffix(input_filename, "_result.log");
343  fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
344 
345  string output_filename = replace_suffix(input_filename, "_result.root");
346  MiniTreeDataSet mt(output_filename, input_filename, data_label);
347 
348  create_all_common_values(mt);
349  enable_extra_branches(mt);
350  declare_values(mt);
351  init_selection();
352  declare_containers(mt);
353 
354  mt.process(silent);
355  mt.save_all();
356 }
357 
358 int main(int argc, char * argv[])
359 {
360  fv::util::ArgParser args(argc, argv);
361  if(!args.cmdOptionExists("-l") || !args.cmdOptionExists("-f")) {
362  cout << "Usage: ./main (-s) -l DATA_LABEL -f input_minitree.root" << endl;
363  return -1;
364  }
365  bool silent = args.cmdOptionExists("-s");
366  string input_filename = args.getCmdOption("-f");
367  string data_label = args.getCmdOption("-l");
368  run_analysis(input_filename, data_label, silent);
369 }
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:330
STL namespace.
The namespace containing all filval classes and functions.
Definition: api.hpp:46
Definition: api.hpp:8