gen_studies.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <numeric>
  5. #include <TSystem.h>
  6. #include <Math/VectorUtil.h>
  7. #include "filval.hpp"
  8. #include "root_filval.hpp"
  9. #include "common.hpp"
  10. #include "analysis/TrackingNtupleObjs.hpp"
  11. const double pi = 3.1415926535897932384626;
  12. const double pi2 = 2*pi;
  13. std::map<int, std::string> algoName = {
  14. {0, "undefAlgorithm" },
  15. {1, "ctf" },
  16. {2, "duplicateMerge" },
  17. {3, "cosmics" },
  18. {4, "initialStep" },
  19. {5, "lowPtTripletStep" },
  20. {6, "pixelPairStep" },
  21. {7, "detachedTripletStep" },
  22. {8, "mixedTripletStep" },
  23. {9, "pixelLessStep" },
  24. {10, "tobTecStep" },
  25. {11, "jetCoreRegionalStep" },
  26. {12, "conversionStep" },
  27. {13, "muonSeededStepInOut" },
  28. {14, "muonSeededStepOutIn" },
  29. {15, "outInEcalSeededConv" },
  30. {16, "inOutEcalSeededConv" },
  31. {17, "nuclInter" },
  32. {18, "standAloneMuon" },
  33. {19, "globalMuon" },
  34. {20, "cosmicStandAloneMuon" },
  35. {21, "cosmicGlobalMuon" },
  36. {22, "highPtTripletStep" },
  37. {23, "lowPtQuadStep" },
  38. {24, "detachedQuadStep" },
  39. {25, "reservedForUpgrades1" },
  40. {26, "reservedForUpgrades2" },
  41. {27, "bTagGhostTracks" },
  42. {28, "beamhalo" },
  43. {29, "gsf" },
  44. {30, "hltPixel" },
  45. {31, "hltIter0" },
  46. {32, "hltIter1" },
  47. {33, "hltIter2" },
  48. {34, "hltIter3" },
  49. {35, "hltIter4" },
  50. {36, "hltIterX" },
  51. {37, "hiRegitMuInitialStep" },
  52. {38, "hiRegitMuLowPtTripletStep" },
  53. {39, "hiRegitMuPixelPairStep" },
  54. {40, "hiRegitMuDetachedTripletStep" },
  55. {41, "hiRegitMuMixedTripletStep" },
  56. {42, "hiRegitMuPixelLessStep" },
  57. {43, "hiRegitMuTobTecStep" },
  58. {44, "hiRegitMuMuonSeededStepInOut" },
  59. {45, "hiRegitMuMuonSeededStepOutIn" }
  60. };
  61. typedef ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float>> Vec4;
  62. SeedCollection seeds;
  63. GenCollection gens;
  64. SimTrackCollection sim_tracks;
  65. SimVertexCollection sim_vertices;
  66. TrackCollection gsf_tracks;
  67. SuperClusterCollection scls;
  68. std::vector<SimTrack> sim_els;
  69. Value<float>* truePileup;
  70. Value<int>* inTimePileup;
  71. Value<unsigned long>* event;
  72. /* Value<vector<int>>* outOfTimePileup; */
  73. /* Value<vector<unsigned int>>* totalSeedsAlgo; */
  74. Value<unsigned int>* totalSeeds;
  75. void register_objects(TrackingDataSet& tds){
  76. seeds.init(&tds);
  77. gens.init(&tds);
  78. /* sim_tracks.init(&tds); */
  79. /* sim_vertices.init(&tds); */
  80. gsf_tracks.init(&tds);
  81. /* scls.init(&tds); */
  82. }
  83. struct {
  84. Value<float>* x;
  85. Value<float>* y;
  86. Value<float>* z;
  87. Value<float>* sigma_x;
  88. Value<float>* sigma_y;
  89. Value<float>* sigma_z;
  90. } bs;
  91. template <typename T>
  92. struct KinColl {
  93. T* pt;
  94. T* eta;
  95. T* phi;
  96. };
  97. bool in_lum_region(const SimVertex& vertex) {
  98. /* Here approximate the luminous region as a cylinder with length 5*\sigma_z and radius
  99. * sqrt(\simga_x^2 + \sigma_y^2) centered as beamspot position.
  100. */
  101. float dx = vertex.x() - bs.x->get();
  102. float dy = vertex.y() - bs.y->get();
  103. float dz = vertex.z() - bs.z->get();
  104. auto radius = static_cast<float>(5 * sqrt(pow(bs.sigma_x->get(), 2) + pow(bs.sigma_y->get(), 2)));
  105. float half_len = 5*bs.sigma_z->get();
  106. return sqrt(dx*dx + dy*dy) < radius and abs(dz) < half_len;
  107. };
  108. bool is_good_sim(const SimTrack& sim_track) {
  109. const auto& vertex = sim_vertices[sim_track.parentVtxIdx()];
  110. return abs(sim_track.pdgId()) == 11 and // electrons
  111. in_lum_region(vertex) and // from luminous region
  112. abs(sim_track.eta())<3.0; // inside ECAL acceptance
  113. };
  114. bool is_good_sim(const std::vector<SimTrack> prompt_sims, const SimTrack& sim_track) {
  115. return find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end();
  116. };
  117. bool is_good_seed(const Seed& seed, float hoe_cut) {
  118. return seed.isECALDriven() and seed.hoe() < hoe_cut;
  119. }
  120. float reco_energy_rel_err(const SimTrack& sim_track, const Seed& seed) {
  121. return (sim_track.pt() - seed.Et()) / sim_track.pt() ;
  122. }
  123. float reco_energy_rel_err(const SimTrack& sim_track, const Track& track) {
  124. return (sim_track.pt() - track.pt()) / sim_track.pt() ;
  125. }
  126. template<typename TrackOrSeed>
  127. bool reco_energy_consistent(const SimTrack& sim_track, const TrackOrSeed& track, float consistency_cut=0.1) {
  128. return fabs(reco_energy_rel_err(sim_track, track)) < consistency_cut;
  129. }
  130. Vec4 scl_p4(const SuperCluster& scl) {
  131. return Vec4(scl.px(), scl.py(), scl.pz(), scl.e());
  132. }
  133. double dphi(double phi1, double phi2) {
  134. auto spec_mod = [](double a) {
  135. return a - floor(a/pi2)*pi2;
  136. };
  137. return spec_mod(phi2 - phi1 + pi) - pi;
  138. }
  139. double deltaR(double eta1, double phi1, double eta2, double phi2) {
  140. return sqrt(pow(eta1 - eta2, 2.0) +
  141. pow(dphi(phi1, phi2), 2.0));
  142. }
  143. struct Track_ {
  144. unsigned long idx;
  145. float pt;
  146. float eta;
  147. float phi;
  148. };
  149. void run(){
  150. using namespace std;
  151. using namespace fv;
  152. using namespace fv_root;
  153. using namespace fv_util;
  154. auto file_list = the_config->get_source_files();
  155. string output_filename = the_config->get_output_filename();
  156. TrackingDataSet tds(output_filename, file_list, "trackingNtuple/tree");
  157. register_objects(tds);
  158. auto hoe_cut = the_config->get("hoe_cut").as<float>(999);
  159. inTimePileup = tds.track_branch<int>("inTimePileup");
  160. truePileup = tds.track_branch<float>("truePileup");
  161. event = tds.track_branch<unsigned long>("event");
  162. while (tds.next()) {
  163. cout << "event: " << event << endl;
  164. // Find Z's Children
  165. /* for (const auto& gen : gens) { */
  166. /* if (not gen.isPrompt()) continue; */
  167. /* if (abs(gen.pdgId()) == 11 or */
  168. /* abs(gen.pdgId()) == 13 or */
  169. /* abs(gen.pdgId()) == 15) { */
  170. /* cout << " " << gen.pdgId() << endl; */
  171. /* } */
  172. /* } */
  173. vector<Track_> tracks_;
  174. for (const auto& gsf_track : gsf_tracks) {
  175. const auto& seed = seeds[gsf_track.seedIdx()];
  176. tracks_.push_back({gsf_track.idx, gsf_track.pt(), gsf_track.eta(), gsf_track.phi()});
  177. cout << gsf_track.idx << ") " <<
  178. gsf_track.nPixel() << " : " << gsf_track.nPixelLay() << " | " <<
  179. gsf_track.nStrip() << " : " << gsf_track.nStripLay() << " | " <<
  180. algoName[gsf_track.originalAlgo()] << " | " <<
  181. algoName[gsf_track.algo()] << " | " <<
  182. algoName[seed.algo()] << " | " <<
  183. gsf_track.algoMask() << " | " <<
  184. endl;
  185. }
  186. std::sort(tracks_.begin(), tracks_.end(), [](Track_ a, Track_ b) {
  187. return a.phi < b.phi;
  188. });
  189. std::sort(tracks_.begin(), tracks_.end(), [](Track_ a, Track_ b) {
  190. return a.eta < b.eta;
  191. });
  192. std::sort(tracks_.begin(), tracks_.end(), [](Track_ a, Track_ b) {
  193. return a.pt < b.pt;
  194. });
  195. /* for (const auto& track_ : tracks_) { */
  196. /* cout << track_.idx << ") " << */
  197. /* track_.pt << " : " << */
  198. /* track_.eta << " : " << */
  199. /* track_.phi << endl; */
  200. /* } */
  201. }
  202. /* tds.save_all(); */
  203. }
  204. int main(int argc, char * argv[]){
  205. using namespace fv_util;
  206. ArgParser args(argc, argv);
  207. if(args.cmd_option_exists("-c")) {
  208. init_config(args.get_cmd_option("-c"));
  209. args.update_config();
  210. if (args.cmd_option_exists("-s")) {
  211. the_config->update_key("silent", true);
  212. }
  213. if (args.cmd_option_exists("-b")) {
  214. the_config->update_key("batch", true);
  215. }
  216. init_log(LogPriority::kLogInfo);
  217. // gSystem->Load("libfilval.so");
  218. run();
  219. } else {
  220. cout << "Usage: ./" << argv[0] << " (-s) (-b) -c config_file.yaml" << endl;
  221. }
  222. return 0;
  223. }