tracking_eff.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <TSystem.h>
  5. #include "filval.hpp"
  6. #include "root_filval.hpp"
  7. #include "analysis/TrackingNtupleObjs.hpp"
  8. SeedCollection seeds;
  9. SimTrackCollection sim_tracks;
  10. SimVertexCollection sim_vertices;
  11. TrackCollection gsf_tracks;
  12. void register_objects(TrackingDataSet& tds){
  13. seeds.init(tds);
  14. sim_tracks.init(tds);
  15. sim_vertices.init(tds);
  16. gsf_tracks.init(tds);
  17. }
  18. struct {
  19. Value<float>* x;
  20. Value<float>* y;
  21. Value<float>* z;
  22. Value<float>* sigma_x;
  23. Value<float>* sigma_y;
  24. Value<float>* sigma_z;
  25. } bs;
  26. bool in_lum_region(const SimVertex& vertex) {
  27. /* Here approximate the luminous region as a cylinder with length 5*\sigma_z and radius
  28. * sqrt(\simga_x^2 + \sigma_y^2) centered as beamspot position.
  29. */
  30. float dx = vertex.x() - bs.x->get();
  31. float dy = vertex.y() - bs.y->get();
  32. float dz = vertex.z() - bs.z->get();
  33. auto radius = static_cast<float>(5 * sqrt(pow(bs.sigma_x->get(), 2) + pow(bs.sigma_y->get(), 2)));
  34. float half_len = 5*bs.sigma_z->get();
  35. return sqrt(dx*dx + dy*dy) < radius and abs(dz) < half_len;
  36. };
  37. bool is_good_sim(const SimTrack& sim_track) {
  38. const auto& vertex = sim_vertices[sim_track.parentVtxIdx()];
  39. return abs(sim_track.pdgId()) == 11 and in_lum_region(vertex);
  40. };
  41. float reco_energy_rel_err(const SimTrack& sim_track, const Seed& seed) {
  42. return (sim_track.pt() - seed.Et()) / sim_track.pt() ;
  43. }
  44. bool reco_energy_consistent(const SimTrack& sim_track, const Seed& seed, float consistency_cut=0.1) {
  45. return fabs(reco_energy_rel_err(sim_track, seed)) < consistency_cut;
  46. }
  47. void run(bool silent){
  48. using namespace std;
  49. using namespace fv;
  50. using namespace fv_root;
  51. auto file_list = the_config->get_source_files();
  52. string output_filename = the_config->get_output_filename();
  53. TrackingDataSet tds(output_filename, file_list, "trackingNtuple/tree");
  54. register_objects(tds);
  55. bs = {
  56. tds.track_branch<float>("bsp_x"),
  57. tds.track_branch<float>("bsp_y"),
  58. tds.track_branch<float>("bsp_z"),
  59. tds.track_branch<float>("bsp_sigmax"),
  60. tds.track_branch<float>("bsp_sigmay"),
  61. tds.track_branch<float>("bsp_sigmaz")
  62. };
  63. std::map<std::pair<int, int>, ContainerTH2<float>*> BPIX_residuals_dRz;
  64. std::map<std::pair<int, int>, ContainerTH2<float>*> BPIX_residuals_dPhi;
  65. std::map<std::pair<int, int>, ContainerTH2<float>*> FPIX_residuals_dRz;
  66. std::map<std::pair<int, int>, ContainerTH2<float>*> FPIX_residuals_dPhi;
  67. stringstream name;
  68. THParams hist_params;
  69. for (int layer=1; layer<=4; layer++) {
  70. for (int hit=1; hit<=3; hit++) {
  71. hist_params = (hit==1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  72. name.str("");
  73. name << "dRz_BPIX_L" << layer << "_H" << hit << "_v_Et";
  74. BPIX_residuals_dRz[{layer,hit}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  75. name.str("");
  76. name << "dRz_FPIX_L" << layer << "_H" << hit << "_v_Et";
  77. FPIX_residuals_dRz[{layer,hit}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  78. hist_params = (hit==1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  79. name.str("");
  80. name << "dPhi_BPIX_L" << layer << "_H" << hit << "_v_Et";
  81. BPIX_residuals_dPhi[{layer,hit}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  82. name.str("");
  83. name << "dPhi_FPIX_L" << layer << "_H" << hit << "_v_Et";
  84. FPIX_residuals_dPhi[{layer,hit}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  85. }
  86. }
  87. auto& seed_eff_v_pt = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_pt", THParams::lookup("eff_v_pt"));
  88. auto& seed_eff_v_eta = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_eta", THParams::lookup("eff_v_eta"));
  89. auto& seed_eff_v_phi = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_phi", THParams::lookup("eff_v_phi"));
  90. auto& seed_eff_v_eta_pt = *tds.register_container<EfficiencyContainer2D<float>>("seed_eff_v_eta_pt", THParams::lookup("eff_v_eta_pt"));
  91. auto& tracking_eff_v_pt = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt", THParams::lookup("eff_v_pt"));
  92. auto& tracking_eff_v_eta = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta", THParams::lookup("eff_v_eta"));
  93. auto& tracking_eff_v_phi = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi", THParams::lookup("eff_v_phi"));
  94. auto& tracking_eff_v_eta_pt = *tds.register_container<EfficiencyContainer2D<float>>("tracking_eff_v_eta_pt", THParams::lookup("eff_v_eta_pt"));
  95. auto& tracking_eff_v_pt2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt2", THParams::lookup("eff_v_pt"));
  96. auto& tracking_eff_v_eta2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta2", THParams::lookup("eff_v_eta"));
  97. auto& tracking_eff_v_phi2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi2", THParams::lookup("eff_v_phi"));
  98. auto& seed_pur_v_pt = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_pt", THParams::lookup("pur_v_pt"));
  99. auto& seed_pur_v_eta = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_eta", THParams::lookup("pur_v_eta"));
  100. auto& seed_pur_v_phi = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_phi", THParams::lookup("pur_v_phi"));
  101. auto& tracking_pur_v_pt = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt", THParams::lookup("pur_v_pt"));
  102. auto& tracking_pur_v_eta = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta", THParams::lookup("pur_v_eta"));
  103. auto& tracking_pur_v_phi = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi", THParams::lookup("pur_v_phi"));
  104. auto& tracking_pur_v_pt2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt2", THParams::lookup("pur_v_pt"));
  105. auto& tracking_pur_v_eta2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta2", THParams::lookup("pur_v_eta"));
  106. auto& tracking_pur_v_phi2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi2", THParams::lookup("pur_v_phi"));
  107. auto& hit_vs_layer_barrel = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_barrel", THParams::lookup("hit_vs_layer"));
  108. auto& hit_vs_layer_forward = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_forward", THParams::lookup("hit_vs_layer"));
  109. auto& gsf_tracks_nmatch_sim_tracks = *tds.register_container<ContainerTH1<size_t>>("gsf_tracks_nmatch_sim_tracks ", THParams::lookup("gsf_tracks_nmatch_sim_tracks"));
  110. auto& ecal_energy_resolution = *tds.register_container<ContainerTH1<float>>("ecal_energy_resolution ", THParams::lookup("ecal_energy_resolution"));
  111. while (tds.next(!silent)) {
  112. // for (const auto& gsf_track : gsf_tracks) {
  113. // cout << "GSF Track " << gsf_track.idx << endl;
  114. // long n_matched_sim = gsf_track.simTrkIdx().size();
  115. // for (int idx=0; idx<n_matched_sim; idx++) {
  116. // int sim_trk_idx = gsf_track.simTrkIdx()[idx];
  117. // int sim_trk_share = gsf_track.shareFrac()[idx];
  118. // cout << " " <<
  119. // cout << " " << sim_tracks[sim_trk_idx].
  120. // }
  121. // }
  122. // ecal_energy_resolution.fill(0.0);
  123. for (const auto& sim_track : sim_tracks) {
  124. if (!is_good_sim(sim_track)) continue;
  125. if (seeds.size() == 0) continue;
  126. for (const auto &seed_idx : sim_track.seedIdx()) {
  127. const Seed& seed = seeds[seed_idx];
  128. if (not seed.isECALDriven()) continue;
  129. // cout << "trk_idx: " << sim_track.idx << " seed_idx: " << seed_idx << " err: " << reco_energy_rel_err(sim_track, seed) << endl;
  130. ecal_energy_resolution.fill(reco_energy_rel_err(sim_track, seed));
  131. }
  132. }
  133. // Seeding Efficiency
  134. for (const auto& sim_track : sim_tracks) {
  135. if (!is_good_sim(sim_track)) continue;
  136. if (seeds.size() == 0) continue;
  137. bool matched = false;
  138. for (const auto& seed_idx : sim_track.seedIdx()) {
  139. const Seed& seed = seeds[seed_idx];
  140. if (seed.isECALDriven()) {
  141. matched=true;
  142. break;
  143. }
  144. }
  145. seed_eff_v_eta_pt.fill(sim_track.eta(), sim_track.pt(), matched);
  146. if (abs(sim_track.eta()) < 2.4)
  147. seed_eff_v_pt.fill(sim_track.pt(), matched);
  148. if (sim_track.pt() > 20.0)
  149. seed_eff_v_eta.fill(sim_track.eta(), matched);
  150. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  151. seed_eff_v_phi.fill(sim_track.phi(), matched);
  152. }
  153. // Tracking Efficiency
  154. for (const auto& sim_track : sim_tracks) {
  155. if (!is_good_sim(sim_track)) continue;
  156. if (gsf_tracks.size() == 0) continue;
  157. bool matched = false;
  158. for (const auto& track_idx : sim_track.trkIdx()) {
  159. const Seed& seed = seeds[gsf_tracks[track_idx].seedIdx()];
  160. if (seed.isECALDriven()) {
  161. matched=true;
  162. break;
  163. }
  164. }
  165. tracking_eff_v_eta_pt.fill(sim_track.eta(), sim_track.pt(), matched);
  166. if (abs(sim_track.eta()) < 2.4)
  167. tracking_eff_v_pt.fill(sim_track.pt(), matched);
  168. if (sim_track.pt() > 20.0)
  169. tracking_eff_v_eta.fill(sim_track.eta(), matched);
  170. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  171. tracking_eff_v_phi.fill(sim_track.phi(), matched);
  172. matched = false;
  173. for (const auto& seed_idx : sim_track.seedIdx()) {
  174. const Seed& seed = seeds[seed_idx];
  175. if (seed.isECALDriven() and seed.trkIdx()>=0 and reco_energy_consistent(sim_track, seed)) {
  176. matched=true;
  177. break;
  178. }
  179. }
  180. if (abs(sim_track.eta()) < 2.4)
  181. tracking_eff_v_pt2.fill(sim_track.pt(), matched);
  182. if (sim_track.pt() > 20.0)
  183. tracking_eff_v_eta2.fill(sim_track.eta(), matched);
  184. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  185. tracking_eff_v_phi2.fill(sim_track.phi(), matched);
  186. }
  187. // Seeding Purity
  188. for (const auto& seed : seeds) {
  189. if (!seed.isECALDriven()) continue;
  190. bool match = false;
  191. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  192. if(is_good_sim(sim_tracks[sim_track_idx])) {
  193. match = true;
  194. break;
  195. }
  196. }
  197. if (abs(seed.eta()) < 2.4)
  198. seed_pur_v_pt.fill(seed.pt(), match);
  199. if (seed.pt() > 20)
  200. seed_pur_v_eta.fill(seed.eta(), match);
  201. if (abs(seed.eta()) < 2.4 and seed.pt() > 20)
  202. seed_pur_v_phi.fill(seed.phi(), match);
  203. }
  204. // Tracking Purity
  205. for (const auto& gsf_track : gsf_tracks) {
  206. gsf_tracks_nmatch_sim_tracks.fill(gsf_track.simTrkIdx().size());
  207. bool match = false;
  208. for (const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  209. if (is_good_sim(sim_tracks[sim_track_idx])) {
  210. match = true;
  211. break;
  212. }
  213. }
  214. if (abs(gsf_track.eta()) < 2.4)
  215. tracking_pur_v_pt.fill(gsf_track.pt(), match);
  216. if (gsf_track.pt() > 20)
  217. tracking_pur_v_eta.fill(gsf_track.eta(), match);
  218. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  219. tracking_pur_v_phi.fill(gsf_track.phi(), match);
  220. match = false;
  221. for (const auto& sim_track_idx : seeds[gsf_track.seedIdx()].simTrkIdx()) {
  222. if (is_good_sim(sim_tracks[sim_track_idx])) {
  223. match = true;
  224. break;
  225. }
  226. }
  227. if (abs(gsf_track.eta()) < 2.4)
  228. tracking_pur_v_pt2.fill(gsf_track.pt(), match);
  229. if (gsf_track.pt() > 20)
  230. tracking_pur_v_eta2.fill(gsf_track.eta(), match);
  231. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  232. tracking_pur_v_phi2.fill(gsf_track.phi(), match);
  233. }
  234. // Hit Residuals
  235. for (const auto& seed : seeds) {
  236. if (seed.trkIdx() < 0) continue; // require that seed produced gsf-track
  237. if (!seed.isECALDriven()) continue;
  238. bool is_sim_matched = false;
  239. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  240. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  241. if (is_good_sim(sim_track) and reco_energy_consistent(sim_track, seed)) {
  242. is_sim_matched = true;
  243. break;
  244. // std::cout << "Matched to non-electron: " << sim_tracks[sim_track_idx].pdgId()
  245. // << " (" << seed.simTrkIdx().size() << ")" << std::endl;
  246. }
  247. }
  248. if (!is_sim_matched) continue;
  249. const auto the_track = gsf_tracks[seed.trkIdx()];
  250. vector<int> hits_valid;
  251. vector<float> hits_dRz;
  252. vector<float> hits_dPhi;
  253. if (the_track.q() == 1) {
  254. hits_valid = seed.isValidPos();
  255. hits_dRz = seed.dRZPos();
  256. hits_dPhi = seed.dPhiPos();
  257. } else {
  258. hits_valid = seed.isValidNeg();
  259. hits_dRz = seed.dRZNeg();
  260. hits_dPhi = seed.dPhiNeg();
  261. }
  262. const vector<int>& hits_isBarrel = seed.isBarrel();
  263. const vector<int>& hits_layerOrDiskNr = seed.layerOrDiskNr();
  264. size_t nHits = hits_valid.size();
  265. for (size_t hit_idx=0; hit_idx<nHits; hit_idx++) {
  266. if (!hits_valid[hit_idx]) continue;
  267. bool isBarrel = hits_isBarrel[hit_idx] == 1;
  268. int layerOrDiskNr = hits_layerOrDiskNr[hit_idx];
  269. float dRz = abs(hits_dRz[hit_idx]);
  270. float dPhi = abs(hits_dPhi[hit_idx]);
  271. if (isBarrel) {
  272. BPIX_residuals_dRz[{layerOrDiskNr, hit_idx+1}]->fill(dRz, seed.Et());
  273. BPIX_residuals_dPhi[{layerOrDiskNr, hit_idx+1}]->fill(dPhi, seed.Et());
  274. } else {
  275. FPIX_residuals_dRz[{layerOrDiskNr, hit_idx+1}]->fill(dRz, seed.Et());
  276. FPIX_residuals_dPhi[{layerOrDiskNr, hit_idx+1}]->fill(dPhi, seed.Et());
  277. }
  278. if (isBarrel)
  279. hit_vs_layer_barrel.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  280. else
  281. hit_vs_layer_forward.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  282. }
  283. }
  284. }
  285. tds.save_all();
  286. }
  287. int main(int argc, char * argv[]){
  288. using namespace fv_util;
  289. ArgParser args(argc, argv);
  290. bool silent = args.cmd_option_exists("-s");
  291. if(args.cmd_option_exists("-c")) {
  292. init_config(args.get_cmd_option("-c"));
  293. args.update_config();
  294. init_log(LogPriority::kLogInfo);
  295. // gSystem->Load("libfilval.so");
  296. run(silent);
  297. } else {
  298. cout << "Usage: ./" << argv[0] << " (-s) -c config_file.yaml" << endl;
  299. }
  300. return 0;
  301. }