tracking_eff.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. typedef ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float>> Vec4;
  12. SeedCollection seeds;
  13. SimTrackCollection sim_tracks;
  14. SimVertexCollection sim_vertices;
  15. TrackCollection gsf_tracks;
  16. SuperClusterCollection scls;
  17. void register_objects(TrackingDataSet& tds){
  18. seeds.init(tds);
  19. sim_tracks.init(tds);
  20. sim_vertices.init(tds);
  21. gsf_tracks.init(tds);
  22. scls.init(tds);
  23. }
  24. struct {
  25. Value<float>* x;
  26. Value<float>* y;
  27. Value<float>* z;
  28. Value<float>* sigma_x;
  29. Value<float>* sigma_y;
  30. Value<float>* sigma_z;
  31. } bs;
  32. template <typename T>
  33. struct KinColl {
  34. T* pt;
  35. T* eta;
  36. T* phi;
  37. };
  38. bool in_lum_region(const SimVertex& vertex) {
  39. /* Here approximate the luminous region as a cylinder with length 5*\sigma_z and radius
  40. * sqrt(\simga_x^2 + \sigma_y^2) centered as beamspot position.
  41. */
  42. float dx = vertex.x() - bs.x->get();
  43. float dy = vertex.y() - bs.y->get();
  44. float dz = vertex.z() - bs.z->get();
  45. auto radius = static_cast<float>(5 * sqrt(pow(bs.sigma_x->get(), 2) + pow(bs.sigma_y->get(), 2)));
  46. float half_len = 5*bs.sigma_z->get();
  47. return sqrt(dx*dx + dy*dy) < radius and abs(dz) < half_len;
  48. };
  49. bool is_good_sim(const SimTrack& sim_track) {
  50. const auto& vertex = sim_vertices[sim_track.parentVtxIdx()];
  51. return abs(sim_track.pdgId()) == 11 and // electrons
  52. in_lum_region(vertex) and // from luminous region
  53. abs(sim_track.eta())<3.0; // inside ECAL acceptance
  54. };
  55. //bool is_good_fake(const SimTrack& sim_track) {
  56. // const auto& vertex = sim_vertices[sim_track.parentVtxIdx()];
  57. // return abs(sim_track.pdgId()) != 11 and sim_track.q() != 0 and in_lum_region(vertex);
  58. //};
  59. bool is_good_seed(const Seed& seed, float hoe_cut) {
  60. return seed.isECALDriven() and seed.hoe() < hoe_cut;
  61. }
  62. float reco_energy_rel_err(const SimTrack& sim_track, const Seed& seed) {
  63. return (sim_track.pt() - seed.Et()) / sim_track.pt() ;
  64. }
  65. float reco_energy_rel_err(const SimTrack& sim_track, const Track& track) {
  66. return (sim_track.pt() - track.pt()) / sim_track.pt() ;
  67. }
  68. template<typename TrackOrSeed>
  69. bool reco_energy_consistent(const SimTrack& sim_track, const TrackOrSeed& track, float consistency_cut=0.1) {
  70. return fabs(reco_energy_rel_err(sim_track, track)) < consistency_cut;
  71. }
  72. Vec4 scl_p4(const SuperCluster& scl) {
  73. return Vec4(scl.px(), scl.py(), scl.pz(), scl.e());
  74. }
  75. double deltaR(float scl_eta, float scl_phi, const SimTrack& sim_track) {
  76. return sqrt(pow(sim_track.eta() - scl_eta, 2.0) +
  77. pow(sim_track.phi() - scl_phi, 2.0));
  78. }
  79. double deltaR(const SimTrack& sim_track, const Track& gsf_track) {
  80. return sqrt(pow(sim_track.eta() - gsf_track.eta(), 2.0) +
  81. pow(sim_track.phi() - gsf_track.phi(), 2.0));
  82. }
  83. auto get_match_stats(bool dRMatch) {
  84. int nSimTrack = 0;
  85. int nGSFTrack = 0;
  86. int nMatched = 0; // 1-to-1
  87. int nMerged = 0; // n-to-1
  88. int nLost = 0; // 1-to-0
  89. int nSplit = 0; // 1-to-n
  90. int nFaked = 0; // 0-to-1
  91. int nFlipped = 0;
  92. vector<float> matched_dR;
  93. vector<float> matched_dpT;
  94. std::map<int, std::vector<int>> sim_matches;
  95. std::map<int, std::vector<int>> gsf_matches;
  96. for(const auto& sim_track : sim_tracks)
  97. if (is_good_sim(sim_track)) sim_matches[sim_track.idx] = {};
  98. for(const auto& gsf_track : gsf_tracks)
  99. gsf_matches[gsf_track.idx] = {};
  100. nSimTrack = sim_matches.size();
  101. nGSFTrack = gsf_matches.size();
  102. for (const auto& sim_track : sim_tracks) {
  103. if (!is_good_sim(sim_track)) continue;
  104. if (dRMatch) {
  105. for (const auto& gsf_track : gsf_tracks) {
  106. float dR = deltaR(sim_track, gsf_track);
  107. if (dR < 0.2) {
  108. sim_matches[sim_track.idx].push_back(gsf_track.idx);
  109. gsf_matches[gsf_track.idx].push_back(sim_track.idx);
  110. matched_dR.push_back(dR);
  111. matched_dpT.push_back((sim_track.pt() - gsf_track.pt())/sim_track.pt());
  112. if (gsf_track.q() != sim_track.q()) nFlipped++;
  113. }
  114. }
  115. } else {
  116. for (const auto& gsfIdx : sim_track.trkIdx()) {
  117. const Track& gsf_track = gsf_tracks[gsfIdx];
  118. float dR = deltaR(sim_track, gsf_track);
  119. sim_matches[sim_track.idx].push_back(gsf_track.idx);
  120. gsf_matches[gsf_track.idx].push_back(sim_track.idx);
  121. matched_dR.push_back(dR);
  122. matched_dpT.push_back((sim_track.pt() - gsf_track.pt())/sim_track.pt());
  123. if (gsf_track.q() != sim_track.q()) nFlipped++;
  124. }
  125. }
  126. }
  127. for (const auto& p : sim_matches) {
  128. const auto& matches = p.second;
  129. if (matches.size() == 0) nLost++;
  130. if (matches.size() == 1 and gsf_matches[matches[0]].size() == 1) nMatched++;
  131. if (matches.size() > 1) nSplit++;
  132. }
  133. for (const auto& p : gsf_matches) {
  134. const auto& matches = p.second;
  135. if (matches.size() == 0) nFaked++;
  136. if (matches.size() > 1) nMerged++;
  137. }
  138. return std::make_tuple(nSimTrack, nGSFTrack, nMatched, nMerged, nLost, nSplit, nFaked, nFlipped, matched_dR, matched_dpT);
  139. }
  140. void run(){
  141. using namespace std;
  142. using namespace fv;
  143. using namespace fv_root;
  144. using namespace fv_util;
  145. auto file_list = the_config->get_source_files();
  146. string output_filename = the_config->get_output_filename();
  147. TrackingDataSet tds(output_filename, file_list, "trackingNtuple/tree");
  148. register_objects(tds);
  149. auto hoe_cut = the_config->get("hoe_cut").as<float>(999);
  150. bs = {
  151. tds.track_branch<float>("bsp_x"),
  152. tds.track_branch<float>("bsp_y"),
  153. tds.track_branch<float>("bsp_z"),
  154. tds.track_branch<float>("bsp_sigmax"),
  155. tds.track_branch<float>("bsp_sigmay"),
  156. tds.track_branch<float>("bsp_sigmaz")
  157. };
  158. enum TMType {
  159. NoMatch = 0,
  160. SeedMatched = 1,
  161. TrackMatched = 2,
  162. SeedAndTrackMatched = 3,
  163. };
  164. std::map<std::tuple<int, int>, ContainerTH2<float>*> residuals_dRz;
  165. std::map<std::tuple<int, int>, ContainerTH2<float>*> residuals_dPhi;
  166. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> BPIX_residuals_dRz;
  167. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> BPIX_residuals_dPhi;
  168. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> FPIX_residuals_dRz;
  169. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> FPIX_residuals_dPhi;
  170. stringstream name;
  171. auto set_name = [&name](const std::string& var, const std::string& region,
  172. const int& layer, const int& hit, const int& tm_type) {
  173. name.str("");
  174. name << var << "_" << region;
  175. if (layer)
  176. name << "_L" << layer;
  177. name << "_H" << hit << "_v_Et";
  178. switch (tm_type) {
  179. case NoMatch:
  180. name << "_NoMatch";
  181. break;
  182. case SeedMatched:
  183. name << "_SeedMatched";
  184. break;
  185. case TrackMatched:
  186. name << "_TrackMatched";
  187. break;
  188. case SeedAndTrackMatched:
  189. name << "_SeedAndTrackMatched";
  190. break;
  191. default:
  192. break;
  193. }
  194. };
  195. THParams hist_params;
  196. for (int hit=1; hit<=3; hit++) {
  197. for (int tm_type = NoMatch; tm_type <= SeedAndTrackMatched; tm_type++) {
  198. for (int layer=1; layer<=4; layer++) {
  199. hist_params = (hit == 1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  200. set_name("dRz", "BPIX", layer, hit, tm_type);
  201. BPIX_residuals_dRz[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  202. set_name("dRz", "FPIX", layer, hit, tm_type);
  203. FPIX_residuals_dRz[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  204. hist_params = (hit == 1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  205. set_name("dPhi", "BPIX", layer, hit, tm_type);
  206. BPIX_residuals_dPhi[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  207. set_name("dPhi", "FPIX", layer, hit, tm_type);
  208. FPIX_residuals_dPhi[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  209. }
  210. hist_params = (hit == 1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  211. set_name("dRz", "ALL", 0, hit, tm_type);
  212. residuals_dRz[std::make_tuple(hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  213. hist_params = (hit == 1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  214. set_name("dPhi", "ALL", 0, hit, tm_type);
  215. residuals_dPhi[make_tuple(hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  216. }
  217. }
  218. std::map<int,vector<float>> eta_regions = {{1, {0.0, 1.1, 1.8, 3.0}},
  219. {2, {0.0, 1.4, 2.3, 3.0}},
  220. {3, {0.0, 1.0, 2.0, 3.0}}};
  221. auto get_region = [&eta_regions](int hit, float eta) {
  222. auto regions = eta_regions[hit];
  223. if (regions.size() <= 1) return 1;
  224. float abseta = abs(eta);
  225. for (int r_idx=1; r_idx < regions.size(); r_idx++) {
  226. if (abseta > regions[r_idx-1] and abseta < regions[r_idx]) {
  227. return r_idx;
  228. }
  229. }
  230. return static_cast<int>(regions.size());
  231. };
  232. std::map<int, ContainerTH2<float>*> dPhi_residuals_v_eta;
  233. std::map<int, ContainerTH2<float>*> dRz_residuals_v_eta;
  234. std::map<std::pair<int, int>, ContainerTH2<float>*> dPhi_residuals_v_region;
  235. std::map<std::pair<int, int>, ContainerTH2<float>*> dRz_residuals_v_region;
  236. for (int hit=1; hit<=3; hit++) {
  237. name.str("");
  238. name << "dPhi_residuals_v_eta_H" << hit;
  239. hist_params = (hit==1) ? THParams::lookup("dPhi_v_eta") : THParams::lookup("dPhi_v_eta_outer_hits");
  240. dPhi_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  241. name.str("");
  242. name << "dRz_residuals_v_eta_H" << hit;
  243. hist_params = (hit==1) ? THParams::lookup("dRz_v_eta") : THParams::lookup("dRz_v_eta_outer_hits");
  244. dRz_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  245. for (int region=1; region<=eta_regions[hit].size()-1; region++){
  246. hist_params = (hit==1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  247. name.str("");
  248. name << "dRz_residuals_H" << hit << "_R" << region;
  249. dRz_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  250. hist_params = (hit==1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  251. name.str("");
  252. name << "dPhi_residuals_H" << hit << "_R" << region;
  253. dPhi_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  254. }
  255. }
  256. KinColl<EfficiencyContainer<float>> seed_eff = {
  257. tds.register_container<EfficiencyContainer<float>>("seed_eff_v_pt", THParams::lookup("pt")),
  258. tds.register_container<EfficiencyContainer<float>>("seed_eff_v_eta", THParams::lookup("eta")),
  259. tds.register_container<EfficiencyContainer<float>>("seed_eff_v_phi", THParams::lookup("phi"))};
  260. KinColl<EfficiencyContainer<float>> seed_pur = {
  261. tds.register_container<EfficiencyContainer<float>>("seed_pur_v_pt", THParams::lookup("pt")),
  262. tds.register_container<EfficiencyContainer<float>>("seed_pur_v_eta", THParams::lookup("eta")),
  263. tds.register_container<EfficiencyContainer<float>>("seed_pur_v_phi", THParams::lookup("phi"))};
  264. KinColl<EfficiencyContainer<float>> tracking_eff = {
  265. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt", THParams::lookup("pt")),
  266. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta", THParams::lookup("eta")),
  267. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi", THParams::lookup("phi"))};
  268. KinColl<EfficiencyContainer<float>> tracking_pur = {
  269. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt", THParams::lookup("pt")),
  270. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta", THParams::lookup("eta")),
  271. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi", THParams::lookup("phi"))};
  272. KinColl<EfficiencyContainer<float>> tracking_eff_dR = {
  273. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt_dR", THParams::lookup("pt")),
  274. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta_dR", THParams::lookup("eta")),
  275. tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi_dR", THParams::lookup("phi"))};
  276. KinColl<EfficiencyContainer<float>> tracking_pur_dR = {
  277. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt_dR", THParams::lookup("pt")),
  278. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta_dR", THParams::lookup("eta")),
  279. tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi_dR", THParams::lookup("phi"))};
  280. KinColl<EfficiencyContainer<float>> partial_fake_rate = {
  281. tds.register_container<EfficiencyContainer<float>>("partial_fake_rate_v_pt", THParams::lookup("pt")),
  282. tds.register_container<EfficiencyContainer<float>>("partial_fake_rate_v_eta", THParams::lookup("eta")),
  283. tds.register_container<EfficiencyContainer<float>>("partial_fake_rate_v_phi", THParams::lookup("phi"))};
  284. KinColl<EfficiencyContainer<float>> full_fake_rate = {
  285. tds.register_container<EfficiencyContainer<float>>("full_fake_rate_v_pt", THParams::lookup("pt")),
  286. tds.register_container<EfficiencyContainer<float>>("full_fake_rate_v_eta", THParams::lookup("eta")),
  287. tds.register_container<EfficiencyContainer<float>>("full_fake_rate_v_phi", THParams::lookup("phi"))};
  288. KinColl<EfficiencyContainer<float>> clean_fake_rate = {
  289. tds.register_container<EfficiencyContainer<float>>("clean_fake_rate_v_pt", THParams::lookup("pt")),
  290. tds.register_container<EfficiencyContainer<float>>("clean_fake_rate_v_eta", THParams::lookup("eta")),
  291. tds.register_container<EfficiencyContainer<float>>("clean_fake_rate_v_phi", THParams::lookup("phi"))};
  292. auto& hit_vs_layer_barrel = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_barrel", THParams::lookup("hit_vs_layer"));
  293. auto& hit_vs_layer_forward = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_forward", THParams::lookup("hit_vs_layer"));
  294. auto& gsf_tracks_nmatch_sim_tracks = *tds.register_container<ContainerTH1<size_t>>("gsf_tracks_nmatch_sim_tracks ", THParams::lookup("gsf_tracks_nmatch_sim_tracks"));
  295. auto& ecal_energy_resolution = *tds.register_container<ContainerTH1<float>>("ecal_energy_resolution ", THParams::lookup("ecal_energy_resolution"));
  296. auto& n_seeds = *tds.register_container<ContainerTH1<size_t>>("n_seeds", THParams::lookup("n_seeds"));
  297. auto& n_good_sim = *tds.register_container<ContainerTH1<int>>("n_good_sim", THParams::lookup("n_seeds"));
  298. auto& n_gsf_tracks = *tds.register_container<ContainerTH1<int>>("n_gsf_track", THParams::lookup("n_seeds"));
  299. auto& n_matched = *tds.register_container<ContainerTH1<int>>("n_matched", THParams::lookup("n_seeds"));
  300. auto& n_merged = *tds.register_container<ContainerTH1<int>>("n_merged", THParams::lookup("n_seeds"));
  301. auto& n_lost = *tds.register_container<ContainerTH1<int>>("n_lost", THParams::lookup("n_seeds"));
  302. auto& n_split = *tds.register_container<ContainerTH1<int>>("n_split", THParams::lookup("n_seeds"));
  303. auto& n_faked = *tds.register_container<ContainerTH1<int>>("n_faked", THParams::lookup("n_seeds"));
  304. auto& n_flipped = *tds.register_container<ContainerTH1<int>>("n_flipped", THParams::lookup("n_seeds"));
  305. auto& matched_dR = *tds.register_container<ContainerTH1<float>>("matched_dR", THParams::lookup("matched_dR"));
  306. auto& matched_dpT = *tds.register_container<ContainerTH1<float>>("matched_dpT", THParams::lookup("matched_dpT"));
  307. auto& n_matched_dR = *tds.register_container<ContainerTH1<int>>("n_matched_dR", THParams::lookup("n_seeds"));
  308. auto& n_merged_dR = *tds.register_container<ContainerTH1<int>>("n_merged_dR", THParams::lookup("n_seeds"));
  309. auto& n_lost_dR = *tds.register_container<ContainerTH1<int>>("n_lost_dR", THParams::lookup("n_seeds"));
  310. auto& n_split_dR = *tds.register_container<ContainerTH1<int>>("n_split_dR", THParams::lookup("n_seeds"));
  311. auto& n_faked_dR = *tds.register_container<ContainerTH1<int>>("n_faked_dR", THParams::lookup("n_seeds"));
  312. auto& n_flipped_dR = *tds.register_container<ContainerTH1<int>>("n_flipped_dR", THParams::lookup("n_seeds"));
  313. auto& matched_dR_dR = *tds.register_container<ContainerTH1<float>>("matched_dR_dR", THParams::lookup("matched_dR"));
  314. auto& matched_dpT_dR = *tds.register_container<ContainerTH1<float>>("matched_dpT_dR", THParams::lookup("matched_dpT"));
  315. auto& sim_pt = *tds.register_container<ContainerTH1<float>>("sim_pt", THParams::lookup("pt_fine"));
  316. auto& sim_eta = *tds.register_container<ContainerTH1<float>>("sim_eta", THParams::lookup("eta_fine"));
  317. auto& sim_phi = *tds.register_container<ContainerTH1<float>>("sim_phi", THParams::lookup("phi_fine"));
  318. auto& reco_pt = *tds.register_container<ContainerTH1<float>>("reco_pt", THParams::lookup("pt_fine"));
  319. auto& reco_eta = *tds.register_container<ContainerTH1<float>>("reco_eta", THParams::lookup("eta_fine"));
  320. auto& reco_phi = *tds.register_container<ContainerTH1<float>>("reco_phi", THParams::lookup("phi_fine"));
  321. while (tds.next()) {
  322. n_seeds.fill(seeds.size());
  323. auto match_stats = get_match_stats(false);
  324. n_good_sim.fill(std::get<0>(match_stats));
  325. n_gsf_tracks.fill(std::get<1>(match_stats));
  326. n_matched.fill(std::get<2>(match_stats));
  327. n_merged.fill(std::get<3>(match_stats));
  328. n_lost.fill(std::get<4>(match_stats));
  329. n_split.fill(std::get<5>(match_stats));
  330. n_faked.fill(std::get<6>(match_stats));
  331. n_flipped.fill(std::get<7>(match_stats));
  332. for(float dR : std::get<8>(match_stats)) matched_dR.fill(dR);
  333. for(float dpT : std::get<9>(match_stats)) matched_dpT.fill(dpT);
  334. match_stats = get_match_stats(true);
  335. n_matched_dR.fill(std::get<2>(match_stats));
  336. n_merged_dR.fill(std::get<3>(match_stats));
  337. n_lost_dR.fill(std::get<4>(match_stats));
  338. n_split_dR.fill(std::get<5>(match_stats));
  339. n_faked_dR.fill(std::get<6>(match_stats));
  340. n_flipped_dR.fill(std::get<7>(match_stats));
  341. for(float dR : std::get<8>(match_stats)) matched_dR_dR.fill(dR);
  342. for(float dpT : std::get<9>(match_stats)) matched_dpT_dR.fill(dpT);
  343. for (const auto& sim_track : sim_tracks) {
  344. if (!is_good_sim(sim_track)) continue;
  345. sim_pt.fill(sim_track.pt());
  346. sim_eta.fill(sim_track.eta());
  347. sim_phi.fill(sim_track.phi());
  348. }
  349. for (const auto& reco_track : gsf_tracks) {
  350. reco_pt.fill(reco_track.pt());
  351. reco_eta.fill(reco_track.eta());
  352. reco_phi.fill(reco_track.phi());
  353. }
  354. for (const auto& sim_track : sim_tracks) {
  355. if (!is_good_sim(sim_track)) continue;
  356. if (seeds.size() == 0) continue;
  357. for (const auto &seed_idx : sim_track.seedIdx()) {
  358. const Seed& seed = seeds[seed_idx];
  359. if (!is_good_seed(seed, hoe_cut)) continue;
  360. ecal_energy_resolution.fill(reco_energy_rel_err(sim_track, seed));
  361. }
  362. }
  363. // Seeding Efficiency
  364. for (const auto& sim_track : sim_tracks) {
  365. if (!is_good_sim(sim_track)) continue;
  366. if (seeds.size() == 0) continue;
  367. bool matched = false;
  368. for (const auto& seed_idx : sim_track.seedIdx()) {
  369. const Seed& seed = seeds[seed_idx];
  370. if (is_good_seed(seed, hoe_cut)) {
  371. matched=true;
  372. break;
  373. }
  374. }
  375. if (abs(sim_track.eta()) < 2.4)
  376. seed_eff.pt->fill(sim_track.pt(), matched);
  377. if (sim_track.pt() > 20.0)
  378. seed_eff.eta->fill(sim_track.eta(), matched);
  379. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  380. seed_eff.phi->fill(sim_track.phi(), matched);
  381. }
  382. // Tracking Efficiency
  383. for (const auto& sim_track : sim_tracks) {
  384. if (!is_good_sim(sim_track)) continue;
  385. if (gsf_tracks.size() == 0) continue;
  386. bool matched = false;
  387. for (const auto& track_idx : sim_track.trkIdx()) {
  388. const Seed& seed = seeds[gsf_tracks[track_idx].seedIdx()];
  389. if (is_good_seed(seed, hoe_cut)) {
  390. matched=true;
  391. break;
  392. }
  393. }
  394. if (abs(sim_track.eta()) < 2.4)
  395. tracking_eff.pt->fill(sim_track.pt(), matched);
  396. if (sim_track.pt() > 20.0)
  397. tracking_eff.eta->fill(sim_track.eta(), matched);
  398. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  399. tracking_eff.phi->fill(sim_track.phi(), matched);
  400. // dR-matching
  401. matched = false;
  402. for (const auto& gsf_track : gsf_tracks) {
  403. const Seed& seed = seeds[gsf_track.seedIdx()];
  404. double dR = deltaR(sim_track, gsf_track);
  405. if (is_good_seed(seed, hoe_cut) and dR < 0.2) {
  406. matched = true;
  407. break;
  408. }
  409. }
  410. if (abs(sim_track.eta()) < 2.4)
  411. tracking_eff_dR.pt->fill(sim_track.pt(), matched);
  412. if (sim_track.pt() > 20.0)
  413. tracking_eff_dR.eta->fill(sim_track.eta(), matched);
  414. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  415. tracking_eff_dR.phi->fill(sim_track.phi(), matched);
  416. }
  417. // Seeding Purity
  418. for (const auto& seed : seeds) {
  419. if (!is_good_seed(seed, hoe_cut)) continue;
  420. bool match = false;
  421. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  422. if(is_good_sim(sim_tracks[sim_track_idx])) {
  423. match = true;
  424. break;
  425. }
  426. }
  427. if (abs(seed.eta()) < 2.4)
  428. seed_pur.pt->fill(seed.pt(), match);
  429. if (seed.pt() > 20)
  430. seed_pur.eta->fill(seed.eta(), match);
  431. if (abs(seed.eta()) < 2.4 and seed.pt() > 20)
  432. seed_pur.phi->fill(seed.phi(), match);
  433. }
  434. // Tracking Purity
  435. for (const auto& gsf_track : gsf_tracks) {
  436. gsf_tracks_nmatch_sim_tracks.fill(gsf_track.simTrkIdx().size());
  437. const Seed& seed = seeds[gsf_track.seedIdx()];
  438. if (!is_good_seed(seed, hoe_cut)) continue;
  439. bool matched = false;
  440. for (const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  441. if (is_good_sim(sim_tracks[sim_track_idx])) {
  442. matched = true;
  443. break;
  444. }
  445. }
  446. if (abs(gsf_track.eta()) < 2.4)
  447. tracking_pur.pt->fill(gsf_track.pt(), matched);
  448. if (gsf_track.pt() > 20)
  449. tracking_pur.eta->fill(gsf_track.eta(), matched);
  450. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  451. tracking_pur.phi->fill(gsf_track.phi(), matched);
  452. // dR-matching
  453. matched = false;
  454. for (const auto& sim_track : sim_tracks) {
  455. double dR = deltaR(sim_track, gsf_track);
  456. if (is_good_sim(sim_track) and dR < 0.2) {
  457. matched = true;
  458. break;
  459. }
  460. }
  461. if (abs(gsf_track.eta()) < 2.4)
  462. tracking_pur_dR.pt->fill(gsf_track.pt(), matched);
  463. if (gsf_track.pt() > 20.0)
  464. tracking_pur_dR.eta->fill(gsf_track.eta(), matched);
  465. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  466. tracking_pur_dR.phi->fill(gsf_track.phi(), matched);
  467. }
  468. // Fake-Rate
  469. std::map<size_t, std::vector<int>> scl2trk; // Maps superclusters to tm-status of matched gsf-tracks
  470. for (const auto &gsf_track : gsf_tracks) {
  471. bool gsf_truth_matched = false;
  472. for(const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  473. if (is_good_sim(sim_tracks[sim_track_idx])) {
  474. gsf_truth_matched = true;
  475. break;
  476. }
  477. }
  478. int scl_idx = seeds[gsf_track.seedIdx()].sclIdx();
  479. scl2trk[scl_idx].push_back(gsf_truth_matched);
  480. }
  481. std::set<size_t> tm_scls; // Set of super-clusters with well matched sim-electrons
  482. for (const auto &scl : scls) {
  483. Vec4 p4 = scl_p4(scl);
  484. for(const auto& sim_track : sim_tracks) {
  485. if (!is_good_sim(sim_track)) continue;
  486. if (deltaR(p4.eta(), p4.phi(), sim_track) > 0.2) continue;
  487. if (((p4.Et() - sim_track.pt()) / p4.Et()) > 0.1) continue;
  488. tm_scls.insert(scl.idx);
  489. break;
  490. }
  491. }
  492. /* cout << "scls: "; */
  493. /* for (const auto& scl: scls) cout << scl.idx << ", "; */
  494. /* cout << endl << "tm_scls: "; */
  495. /* for (const auto& idx: tm_scls) cout << idx << ", "; */
  496. /* cout << endl; */
  497. for (const auto &scl : scls) {
  498. if (scl.hoe() > hoe_cut) continue;
  499. float scl_pt = hypot(scl.px(), scl.py());
  500. float scl_eta = pseudorapidity(scl.x(), scl.y(), scl.z());
  501. float scl_phi = atan2(scl.py(), scl.px());
  502. int ntracks = scl2trk[scl.idx].size();
  503. int ntmtracks = std::accumulate(scl2trk[scl.idx].begin(), scl2trk[scl.idx].end(), 0);
  504. bool partial_fake = ntmtracks > 0 and ntracks > ntmtracks;
  505. bool full_fake = ntmtracks == 0 and ntracks > 0;
  506. /* cout << "ntracks: " << ntracks << " "; */
  507. /* cout << "count: " << tm_scls.count(scl.idx); */
  508. if (ntracks > 0) {
  509. partial_fake_rate.pt->fill(scl_pt, partial_fake);
  510. partial_fake_rate.eta->fill(scl_eta, partial_fake);
  511. partial_fake_rate.phi->fill(scl_phi, partial_fake);
  512. full_fake_rate.pt->fill(scl_pt, full_fake);
  513. full_fake_rate.eta->fill(scl_eta, full_fake);
  514. full_fake_rate.phi->fill(scl_phi, full_fake);
  515. if (tm_scls.count(scl.idx) == 0) {
  516. /* cout << "Filling clean_fake_rate" << endl; */
  517. clean_fake_rate.pt->fill(scl_pt, full_fake);
  518. clean_fake_rate.eta->fill(scl_eta, full_fake);
  519. clean_fake_rate.phi->fill(scl_phi, full_fake);
  520. }
  521. }
  522. /* cout << endl; */
  523. }
  524. // Hit Residuals
  525. for (const auto& seed : seeds) {
  526. if (seed.trkIdx() < 0) continue; // require that seed produced gsf-track
  527. if (!is_good_seed(seed, hoe_cut)) continue;
  528. bool is_seed_sim_matched = false;
  529. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  530. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  531. if (is_good_sim(sim_track) and reco_energy_consistent(sim_track, seed)) {
  532. is_seed_sim_matched = true;
  533. break;
  534. }
  535. }
  536. bool is_track_sim_matched = false;
  537. const auto the_track = gsf_tracks[seed.trkIdx()];
  538. for (const auto& sim_track_idx : the_track.simTrkIdx()) {
  539. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  540. if (is_good_sim(sim_track) and reco_energy_consistent(sim_track, the_track)) {
  541. is_track_sim_matched = true;
  542. break;
  543. }
  544. }
  545. std::vector<TMType> tm_types;
  546. if (is_seed_sim_matched and is_track_sim_matched) {
  547. tm_types = {SeedAndTrackMatched, SeedMatched, TrackMatched};
  548. } else if (is_seed_sim_matched) {
  549. tm_types = {SeedMatched};
  550. } else if (is_track_sim_matched) {
  551. tm_types = {TrackMatched};
  552. } else {
  553. tm_types = {NoMatch};
  554. }
  555. vector<int> hits_valid;
  556. vector<float> hits_dRz;
  557. vector<float> hits_dPhi;
  558. if (the_track.q() == 1) {
  559. hits_valid = seed.isValidPos();
  560. hits_dRz = seed.dRZPos();
  561. hits_dPhi = seed.dPhiPos();
  562. } else {
  563. hits_valid = seed.isValidNeg();
  564. hits_dRz = seed.dRZNeg();
  565. hits_dPhi = seed.dPhiNeg();
  566. }
  567. const vector<int>& hits_isBarrel = seed.isBarrel();
  568. const vector<int>& hits_layerOrDiskNr = seed.layerOrDiskNr();
  569. for (const auto& tm_type : tm_types) {
  570. size_t nHits = hits_valid.size();
  571. for (size_t hit_idx = 0; hit_idx < nHits; hit_idx++) {
  572. if (!hits_valid[hit_idx]) continue;
  573. bool isBarrel = hits_isBarrel[hit_idx] == 1;
  574. int layerOrDiskNr = hits_layerOrDiskNr[hit_idx];
  575. float dRz = abs(hits_dRz[hit_idx]);
  576. float dPhi = abs(hits_dPhi[hit_idx]);
  577. int eta_region = get_region(static_cast<int>(hit_idx + 1), seed.eta());
  578. if (is_seed_sim_matched) {
  579. dRz_residuals_v_eta[hit_idx + 1]->fill(dRz, abs(seed.eta()));
  580. dPhi_residuals_v_eta[hit_idx + 1]->fill(dPhi, abs(seed.eta()));
  581. dRz_residuals_v_region[make_pair(hit_idx + 1, eta_region)]->fill(dRz, seed.Et());
  582. dPhi_residuals_v_region[make_pair(hit_idx + 1, eta_region)]->fill(dPhi, seed.Et());
  583. }
  584. residuals_dRz[make_tuple(hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  585. residuals_dPhi[make_tuple(hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  586. if (layerOrDiskNr == -1) continue; // subDet is not set w/ old seeding
  587. if (isBarrel)
  588. hit_vs_layer_barrel.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  589. else
  590. hit_vs_layer_forward.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  591. if (isBarrel) {
  592. BPIX_residuals_dRz[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  593. BPIX_residuals_dPhi[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  594. } else {
  595. FPIX_residuals_dRz[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  596. FPIX_residuals_dPhi[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  597. }
  598. }
  599. }
  600. }
  601. }
  602. tds.save_all();
  603. }
  604. int main(int argc, char * argv[]){
  605. using namespace fv_util;
  606. ArgParser args(argc, argv);
  607. if(args.cmd_option_exists("-c")) {
  608. init_config(args.get_cmd_option("-c"));
  609. args.update_config();
  610. if (args.cmd_option_exists("-s")) {
  611. the_config->update_key("silent", true);
  612. }
  613. if (args.cmd_option_exists("-b")) {
  614. the_config->update_key("batch", true);
  615. }
  616. init_log(LogPriority::kLogInfo);
  617. // gSystem->Load("libfilval.so");
  618. run();
  619. } else {
  620. cout << "Usage: ./" << argv[0] << " (-s) (-b) -c config_file.yaml" << endl;
  621. }
  622. return 0;
  623. }