obj_types.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * Define some common data structures used throughout the analysis and methods
  33. * to make them available.
  34. */
  35. #ifndef OBJ_TYPES_HPP
  36. #define OBJ_TYPES_HPP
  37. #include "TLorentzVector.h"
  38. #include "filval/filval.hpp"
  39. #include "filval/root/filval.hpp"
  40. #include "analysis/MiniTree.h"
  41. struct Jet{
  42. float b_cmva;
  43. int mcMatchFlav;
  44. };
  45. struct Lepton {
  46. int pdg_id;
  47. int mcMatchPdgId;
  48. int charge;
  49. float relIso;
  50. };
  51. struct GenPart{
  52. int pdgId;
  53. int motherIndex;
  54. int motherId;
  55. int status;
  56. };
  57. struct Particle{
  58. enum{JET,
  59. LEPTON,
  60. GENPART,
  61. VOID,
  62. } tag;
  63. int idx;
  64. TLorentzVector v;
  65. union
  66. {
  67. Jet jet;
  68. Lepton lepton;
  69. GenPart genpart;
  70. };
  71. static Particle
  72. Jet(int idx, TLorentzVector v, Jet&& jet){
  73. Particle p = {Particle::JET, idx, v, {}};
  74. p.jet = jet;
  75. return p;
  76. }
  77. static Particle
  78. Lepton(int idx, TLorentzVector v, Lepton&& lepton){
  79. Particle p = {Particle::LEPTON, idx, v, {}};
  80. p.lepton = lepton;
  81. return p;
  82. }
  83. static Particle
  84. GenPart(int idx, TLorentzVector v, GenPart&& genpart){
  85. Particle p = {Particle::GENPART, idx, v, {}};
  86. p.genpart = genpart;
  87. return p;
  88. }
  89. static Particle
  90. Void(){
  91. Particle p;
  92. p.tag = Particle::VOID;
  93. return p;
  94. }
  95. };
  96. Function<Particle(std::vector<Particle>)>& leading_particle = GenFunction::reg_func<Particle(std::vector<Particle>)>("leading_particle",
  97. FUNC(([](const std::vector<Particle>& particles){
  98. if(particles.size() == 0) return Particle::Void();
  99. Particle leading_particle = particles[0];
  100. for (auto particle : particles){
  101. if(particle.tag != Particle::VOID && particle.v.Pt() > leading_particle.v.Pt()){
  102. leading_particle = particle;
  103. }
  104. }
  105. return leading_particle;
  106. })));
  107. Function<float(Particle)>& particle_pt = GenFunction::reg_func<float(Particle)>("particle_pt",
  108. FUNC(([](const Particle& p){
  109. return p.v.Pt();
  110. })));
  111. Function<float(Particle)>& particle_eta = GenFunction::reg_func<float(Particle)>("particle_eta",
  112. FUNC(([](const Particle& p){
  113. return p.v.Eta();
  114. })));
  115. Function<float(Particle)>& lepton_relIso = GenFunction::reg_func<float(Particle)>("lepton_relIso",
  116. FUNC(([](const Particle& p){
  117. return p.lepton.relIso;
  118. })));
  119. decltype(auto)
  120. construct_jets_value(TreeDataSet<MiniTree>& mt){
  121. mt.track_branch<int>("nJet");
  122. mt.track_branch_vec<float>("nJet", "Jet_pt");
  123. mt.track_branch_vec<float>("nJet", "Jet_eta");
  124. mt.track_branch_vec<float>("nJet", "Jet_phi");
  125. mt.track_branch_vec<float>("nJet", "Jet_mass");
  126. auto Jet_4v = lorentz_vectors("Jet_pt", "Jet_eta", "Jet_phi", "Jet_mass", "Jet_4v");
  127. auto jets = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  128. std::vector<float>,
  129. std::vector< int >
  130. )>("build_reco_jets",
  131. FUNC(([](const std::vector<TLorentzVector>& vs,
  132. const std::vector<float>& b_cmvas,
  133. const std::vector< int >& mcMatchFlavs
  134. ){
  135. std::vector<Particle> jets;
  136. for(int i=0; i<vs.size(); i++){
  137. Particle p = Particle::Jet(i, vs[i], {b_cmvas[i],
  138. mcMatchFlavs[i]
  139. });
  140. jets.push_back(p);
  141. }
  142. return jets;
  143. }))), fv::tuple(Jet_4v,
  144. mt.track_branch_vec<float>("nJet", "Jet_btagCMVA"),
  145. mt.track_branch_vec< int >("nJet", "Jet_mcMatchFlav")
  146. ), "jets");
  147. return jets;
  148. }
  149. decltype(auto)
  150. construct_b_jets_value(TreeDataSet<MiniTree>& mt){
  151. auto b_jets = fv::filter(GenFunction::reg_func<bool(Particle)>("is_b_jet",
  152. FUNC(([](const Particle& p){
  153. if(p.tag != Particle::JET) return false;
  154. return p.jet.b_cmva > 0;
  155. }))), lookup<std::vector<Particle>>("jets"), "b_jets");
  156. return b_jets;
  157. }
  158. decltype(auto)
  159. construct_leptons_value(TreeDataSet<MiniTree>& mt){
  160. mt.track_branch<int>("nLepGood");
  161. mt.track_branch_vec<float>("nLepGood", "LepGood_pt");
  162. mt.track_branch_vec<float>("nLepGood", "LepGood_eta");
  163. mt.track_branch_vec<float>("nLepGood", "LepGood_phi");
  164. mt.track_branch_vec<float>("nLepGood", "LepGood_mass");
  165. auto LepGood_4v = lorentz_vectors("LepGood_pt", "LepGood_eta", "LepGood_phi", "LepGood_mass", "LepGood_4v");
  166. auto leptons = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  167. std::vector<int>,
  168. std::vector<int>,
  169. std::vector<int>,
  170. std::vector<float>)>("build_reco_leptons",
  171. FUNC(([](const std::vector<TLorentzVector>& vs,
  172. const std::vector<int>& pdgIds,
  173. const std::vector<int>& mcMatchPdgIds,
  174. const std::vector<int>& charges,
  175. const std::vector<float>& relIso
  176. ){
  177. std::vector<Particle> leptons;
  178. for(int i=0; i<vs.size(); i++){
  179. Particle p = Particle::Lepton(i, vs[i], {pdgIds[i],
  180. mcMatchPdgIds[i],
  181. charges[i],
  182. relIso[i]});
  183. leptons.push_back(p);
  184. }
  185. return leptons;
  186. }))), fv::tuple(LepGood_4v,
  187. mt.track_branch_vec< int >("nLepGood", "LepGood_pdgId"),
  188. mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchPdgId"),
  189. mt.track_branch_vec< int >("nLepGood", "LepGood_charge"),
  190. mt.track_branch_vec<float>("nLepGood", "LepGood_miniRelIso")
  191. ), "leptons");
  192. return leptons;
  193. }
  194. decltype(auto)
  195. construct_mc_jets_value(TreeDataSet<MiniTree>& mt){
  196. mt.track_branch<int>("nGenPart");
  197. mt.track_branch_vec<float>("nGenPart", "GenPart_pt");
  198. mt.track_branch_vec<float>("nGenPart", "GenPart_eta");
  199. mt.track_branch_vec<float>("nGenPart", "GenPart_phi");
  200. mt.track_branch_vec<float>("nGenPart", "GenPart_mass");
  201. auto Jet_4v = lorentz_vectors("GenPart_pt", "GenPart_eta", "GenPart_phi", "GenPart_mass", "GenPart_4v");
  202. energies(Jet_4v, "GenPart_energy");
  203. auto mc_jets = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  204. std::vector<int>,
  205. std::vector<int>,
  206. std::vector<int>,
  207. std::vector<int>)>("build_mc_jets",
  208. FUNC(([](const std::vector<TLorentzVector>& vs,
  209. const std::vector<int>& pdgIds,
  210. const std::vector<int>& motherIndices,
  211. const std::vector<int>& motherIds,
  212. const std::vector<int>& statuses){
  213. std::vector<Particle> mc_jets;
  214. for(int i=0; i<vs.size(); i++){
  215. Particle p = Particle::GenPart(i, vs[i], {pdgIds[i],
  216. motherIndices[i],
  217. motherIds[i],
  218. statuses[i]});
  219. mc_jets.push_back(p);
  220. }
  221. return mc_jets;
  222. }))), fv::tuple(Jet_4v,
  223. mt.track_branch_vec<int>("nGenPart", "GenPart_pdgId"),
  224. mt.track_branch_vec<int>("nGenPart", "GenPart_motherIndex"),
  225. mt.track_branch_vec<int>("nGenPart", "GenPart_motherId"),
  226. mt.track_branch_vec<int>("nGenPart", "GenPart_status")
  227. ),"mc_jets");
  228. return mc_jets;
  229. }
  230. void create_all_common_values(TreeDataSet<MiniTree>& mt){
  231. construct_jets_value(mt);
  232. construct_b_jets_value(mt);
  233. construct_mc_jets_value(mt);
  234. construct_leptons_value(mt);
  235. }
  236. #endif