tracking_eff.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. int x = 12;
  56. int y = 13;
  57. for (auto z : vector<pair<int,int>>{{x, x}, {y, y}}) {
  58. cout << z.first << endl;
  59. }
  60. bs = {
  61. tds.track_branch<float>("bsp_x"),
  62. tds.track_branch<float>("bsp_y"),
  63. tds.track_branch<float>("bsp_z"),
  64. tds.track_branch<float>("bsp_sigmax"),
  65. tds.track_branch<float>("bsp_sigmay"),
  66. tds.track_branch<float>("bsp_sigmaz")
  67. };
  68. std::map<std::tuple<int, int, bool>, ContainerTH2<float>*> BPIX_residuals_dRz;
  69. std::map<std::tuple<int, int, bool>, ContainerTH2<float>*> BPIX_residuals_dPhi;
  70. std::map<std::tuple<int, int, bool>, ContainerTH2<float>*> FPIX_residuals_dRz;
  71. std::map<std::tuple<int, int, bool>, ContainerTH2<float>*> FPIX_residuals_dPhi;
  72. int layer, hit, isFake;
  73. stringstream name;
  74. auto set_name = [&name, &layer, &hit, &isFake](const std::string& var, const std::string& region) {
  75. name.str("");
  76. name << var << "_" << region << "_L" << layer << "_H" << hit << "_v_Et";
  77. if (isFake)
  78. name << "_fake";
  79. };
  80. THParams hist_params;
  81. for (layer=1; layer<=4; layer++) {
  82. for (hit=1; hit<=3; hit++) {
  83. for (isFake=0; isFake<=1; isFake++) {
  84. hist_params = (hit == 1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  85. set_name("dRz", "BPIX");
  86. BPIX_residuals_dRz[{layer, hit, isFake}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  87. set_name("dRz", "FPIX");
  88. FPIX_residuals_dRz[{layer, hit, isFake}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  89. hist_params = (hit == 1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  90. set_name("dPhi", "BPIX");
  91. BPIX_residuals_dPhi[{layer, hit, isFake}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  92. set_name("dPhi", "FPIX");
  93. FPIX_residuals_dPhi[{layer, hit, isFake}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  94. }
  95. }
  96. }
  97. std::map<int,vector<float>> eta_regions = {{1, {0.0, 1.1, 1.8, 3.0}},
  98. {2, {0.0, 1.4, 2.3, 3.0}},
  99. {3, {0.0, 1.0, 2.0, 3.0}}};
  100. auto get_region = [&eta_regions](int hit, float eta) {
  101. auto regions = eta_regions[hit];
  102. if (regions.size() <= 1) return 1;
  103. float abseta = abs(eta);
  104. for (int r_idx=1; r_idx < regions.size(); r_idx++) {
  105. if (abseta > regions[r_idx-1] and abseta < regions[r_idx]) {
  106. return r_idx;
  107. }
  108. }
  109. return static_cast<int>(regions.size());
  110. };
  111. std::map<int, ContainerTH2<float>*> dPhi_residuals_v_eta;
  112. std::map<int, ContainerTH2<float>*> dRz_residuals_v_eta;
  113. std::map<std::pair<int, int>, ContainerTH2<float>*> dPhi_residuals_v_region;
  114. std::map<std::pair<int, int>, ContainerTH2<float>*> dRz_residuals_v_region;
  115. for (int hit=1; hit<=3; hit++) {
  116. name.str("");
  117. name << "dPhi_residuals_v_eta_H" << hit;
  118. hist_params = (hit==1) ? THParams::lookup("dPhi_v_eta") : THParams::lookup("dPhi_v_eta_outer_hits");
  119. dPhi_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  120. name.str("");
  121. name << "dRz_residuals_v_eta_H" << hit;
  122. hist_params = (hit==1) ? THParams::lookup("dRz_v_eta") : THParams::lookup("dRz_v_eta_outer_hits");
  123. dRz_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  124. for (int region=1; region<=eta_regions[hit].size()-1; region++){
  125. hist_params = (hit==1) ? THParams::lookup("dRz_v_Et") : THParams::lookup("dRz_v_Et_outer_hits");
  126. name.str("");
  127. name << "dRz_residuals_H" << hit << "_R" << region;
  128. dRz_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  129. hist_params = (hit==1) ? THParams::lookup("dPhi_v_Et") : THParams::lookup("dPhi_v_Et_outer_hits");
  130. name.str("");
  131. name << "dPhi_residuals_H" << hit << "_R" << region;
  132. dPhi_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  133. }
  134. }
  135. auto& seed_eff_v_pt = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_pt", THParams::lookup("eff_v_pt"));
  136. auto& seed_eff_v_eta = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_eta", THParams::lookup("eff_v_eta"));
  137. auto& seed_eff_v_phi = *tds.register_container<EfficiencyContainer<float>>("seed_eff_v_phi", THParams::lookup("eff_v_phi"));
  138. auto& seed_eff_v_eta_pt = *tds.register_container<EfficiencyContainer2D<float>>("seed_eff_v_eta_pt", THParams::lookup("eff_v_eta_pt"));
  139. auto& tracking_eff_v_pt = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt", THParams::lookup("eff_v_pt"));
  140. auto& tracking_eff_v_eta = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta", THParams::lookup("eff_v_eta"));
  141. auto& tracking_eff_v_phi = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi", THParams::lookup("eff_v_phi"));
  142. auto& tracking_eff_v_eta_pt = *tds.register_container<EfficiencyContainer2D<float>>("tracking_eff_v_eta_pt", THParams::lookup("eff_v_eta_pt"));
  143. auto& tracking_eff_v_pt2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_pt2", THParams::lookup("eff_v_pt"));
  144. auto& tracking_eff_v_eta2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_eta2", THParams::lookup("eff_v_eta"));
  145. auto& tracking_eff_v_phi2 = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_phi2", THParams::lookup("eff_v_phi"));
  146. auto& seed_pur_v_pt = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_pt", THParams::lookup("pur_v_pt"));
  147. auto& seed_pur_v_eta = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_eta", THParams::lookup("pur_v_eta"));
  148. auto& seed_pur_v_phi = *tds.register_container<EfficiencyContainer<float>>("seed_pur_v_phi", THParams::lookup("pur_v_phi"));
  149. auto& tracking_pur_v_pt = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt", THParams::lookup("pur_v_pt"));
  150. auto& tracking_pur_v_eta = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta", THParams::lookup("pur_v_eta"));
  151. auto& tracking_pur_v_phi = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi", THParams::lookup("pur_v_phi"));
  152. auto& tracking_pur_v_pt2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_pt2", THParams::lookup("pur_v_pt"));
  153. auto& tracking_pur_v_eta2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_eta2", THParams::lookup("pur_v_eta"));
  154. auto& tracking_pur_v_phi2 = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_phi2", THParams::lookup("pur_v_phi"));
  155. auto& hit_vs_layer_barrel = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_barrel", THParams::lookup("hit_vs_layer"));
  156. auto& hit_vs_layer_forward = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_forward", THParams::lookup("hit_vs_layer"));
  157. auto& gsf_tracks_nmatch_sim_tracks = *tds.register_container<ContainerTH1<size_t>>("gsf_tracks_nmatch_sim_tracks ", THParams::lookup("gsf_tracks_nmatch_sim_tracks"));
  158. auto& ecal_energy_resolution = *tds.register_container<ContainerTH1<float>>("ecal_energy_resolution ", THParams::lookup("ecal_energy_resolution"));
  159. while (tds.next(!silent)) {
  160. for (const auto& sim_track : sim_tracks) {
  161. if (!is_good_sim(sim_track)) continue;
  162. if (seeds.size() == 0) continue;
  163. for (const auto &seed_idx : sim_track.seedIdx()) {
  164. const Seed& seed = seeds[seed_idx];
  165. if (not seed.isECALDriven()) continue;
  166. ecal_energy_resolution.fill(reco_energy_rel_err(sim_track, seed));
  167. }
  168. }
  169. // Seeding Efficiency
  170. for (const auto& sim_track : sim_tracks) {
  171. if (!is_good_sim(sim_track)) continue;
  172. if (seeds.size() == 0) continue;
  173. bool matched = false;
  174. for (const auto& seed_idx : sim_track.seedIdx()) {
  175. const Seed& seed = seeds[seed_idx];
  176. if (seed.isECALDriven()) {
  177. matched=true;
  178. break;
  179. }
  180. }
  181. seed_eff_v_eta_pt.fill(sim_track.eta(), sim_track.pt(), matched);
  182. if (abs(sim_track.eta()) < 2.4)
  183. seed_eff_v_pt.fill(sim_track.pt(), matched);
  184. if (sim_track.pt() > 20.0)
  185. seed_eff_v_eta.fill(sim_track.eta(), matched);
  186. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  187. seed_eff_v_phi.fill(sim_track.phi(), matched);
  188. }
  189. // Tracking Efficiency
  190. for (const auto& sim_track : sim_tracks) {
  191. if (!is_good_sim(sim_track)) continue;
  192. if (gsf_tracks.size() == 0) continue;
  193. bool matched = false;
  194. for (const auto& track_idx : sim_track.trkIdx()) {
  195. const Seed& seed = seeds[gsf_tracks[track_idx].seedIdx()];
  196. if (seed.isECALDriven()) {
  197. matched=true;
  198. break;
  199. }
  200. }
  201. tracking_eff_v_eta_pt.fill(sim_track.eta(), sim_track.pt(), matched);
  202. if (abs(sim_track.eta()) < 2.4)
  203. tracking_eff_v_pt.fill(sim_track.pt(), matched);
  204. if (sim_track.pt() > 20.0)
  205. tracking_eff_v_eta.fill(sim_track.eta(), matched);
  206. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  207. tracking_eff_v_phi.fill(sim_track.phi(), matched);
  208. matched = false;
  209. for (const auto& seed_idx : sim_track.seedIdx()) {
  210. const Seed& seed = seeds[seed_idx];
  211. if (seed.isECALDriven() and seed.trkIdx()>=0 and reco_energy_consistent(sim_track, seed)) {
  212. matched=true;
  213. break;
  214. }
  215. }
  216. if (abs(sim_track.eta()) < 2.4)
  217. tracking_eff_v_pt2.fill(sim_track.pt(), matched);
  218. if (sim_track.pt() > 20.0)
  219. tracking_eff_v_eta2.fill(sim_track.eta(), matched);
  220. if (abs(sim_track.eta()) < 2.4 and sim_track.pt() > 20)
  221. tracking_eff_v_phi2.fill(sim_track.phi(), matched);
  222. }
  223. // Seeding Purity
  224. for (const auto& seed : seeds) {
  225. if (!seed.isECALDriven()) continue;
  226. bool match = false;
  227. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  228. if(is_good_sim(sim_tracks[sim_track_idx])) {
  229. match = true;
  230. break;
  231. }
  232. }
  233. if (abs(seed.eta()) < 2.4)
  234. seed_pur_v_pt.fill(seed.pt(), match);
  235. if (seed.pt() > 20)
  236. seed_pur_v_eta.fill(seed.eta(), match);
  237. if (abs(seed.eta()) < 2.4 and seed.pt() > 20)
  238. seed_pur_v_phi.fill(seed.phi(), match);
  239. }
  240. // Tracking Purity
  241. for (const auto& gsf_track : gsf_tracks) {
  242. gsf_tracks_nmatch_sim_tracks.fill(gsf_track.simTrkIdx().size());
  243. bool match = false;
  244. for (const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  245. if (is_good_sim(sim_tracks[sim_track_idx])) {
  246. match = true;
  247. break;
  248. }
  249. }
  250. if (abs(gsf_track.eta()) < 2.4)
  251. tracking_pur_v_pt.fill(gsf_track.pt(), match);
  252. if (gsf_track.pt() > 20)
  253. tracking_pur_v_eta.fill(gsf_track.eta(), match);
  254. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  255. tracking_pur_v_phi.fill(gsf_track.phi(), match);
  256. match = false;
  257. for (const auto& sim_track_idx : seeds[gsf_track.seedIdx()].simTrkIdx()) {
  258. if (is_good_sim(sim_tracks[sim_track_idx])) {
  259. match = true;
  260. break;
  261. }
  262. }
  263. if (abs(gsf_track.eta()) < 2.4)
  264. tracking_pur_v_pt2.fill(gsf_track.pt(), match);
  265. if (gsf_track.pt() > 20)
  266. tracking_pur_v_eta2.fill(gsf_track.eta(), match);
  267. if (abs(gsf_track.eta()) < 2.4 and gsf_track.pt() > 20)
  268. tracking_pur_v_phi2.fill(gsf_track.phi(), match);
  269. }
  270. // Hit Residuals
  271. for (const auto& seed : seeds) {
  272. if (seed.trkIdx() < 0) continue; // require that seed produced gsf-track
  273. if (!seed.isECALDriven()) continue;
  274. bool is_sim_matched = false;
  275. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  276. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  277. if (is_good_sim(sim_track) and reco_energy_consistent(sim_track, seed)) {
  278. is_sim_matched = true;
  279. break;
  280. // std::cout << "Matched to non-electron: " << sim_tracks[sim_track_idx].pdgId()
  281. // << " (" << seed.simTrkIdx().size() << ")" << std::endl;
  282. }
  283. }
  284. const auto the_track = gsf_tracks[seed.trkIdx()];
  285. vector<int> hits_valid;
  286. vector<float> hits_dRz;
  287. vector<float> hits_dPhi;
  288. if (the_track.q() == 1) {
  289. hits_valid = seed.isValidPos();
  290. hits_dRz = seed.dRZPos();
  291. hits_dPhi = seed.dPhiPos();
  292. } else {
  293. hits_valid = seed.isValidNeg();
  294. hits_dRz = seed.dRZNeg();
  295. hits_dPhi = seed.dPhiNeg();
  296. }
  297. const vector<int>& hits_isBarrel = seed.isBarrel();
  298. const vector<int>& hits_layerOrDiskNr = seed.layerOrDiskNr();
  299. size_t nHits = hits_valid.size();
  300. for (size_t hit_idx=0; hit_idx<nHits; hit_idx++) {
  301. if (!hits_valid[hit_idx]) continue;
  302. bool isBarrel = hits_isBarrel[hit_idx] == 1;
  303. int layerOrDiskNr = hits_layerOrDiskNr[hit_idx];
  304. float dRz = abs(hits_dRz[hit_idx]);
  305. float dPhi = abs(hits_dPhi[hit_idx]);
  306. int eta_region = get_region(static_cast<int>(hit_idx + 1), seed.eta());
  307. if (is_sim_matched) {
  308. dRz_residuals_v_eta[hit_idx + 1]->fill(dRz, abs(seed.eta()));
  309. dPhi_residuals_v_eta[hit_idx + 1]->fill(dPhi, abs(seed.eta()));
  310. dRz_residuals_v_region[{hit_idx + 1, eta_region}]->fill(dRz, seed.Et());
  311. dPhi_residuals_v_region[{hit_idx + 1, eta_region}]->fill(dPhi, seed.Et());
  312. }
  313. if (layerOrDiskNr == -1) continue; // subDet is not set w/ old seeding
  314. if (isBarrel)
  315. hit_vs_layer_barrel.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  316. else
  317. hit_vs_layer_forward.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  318. if (isBarrel) {
  319. BPIX_residuals_dRz[{layerOrDiskNr, hit_idx+1, !is_sim_matched}]->fill(dRz, seed.Et());
  320. BPIX_residuals_dPhi[{layerOrDiskNr, hit_idx+1, !is_sim_matched}]->fill(dPhi, seed.Et());
  321. } else {
  322. FPIX_residuals_dRz[{layerOrDiskNr, hit_idx+1, !is_sim_matched}]->fill(dRz, seed.Et());
  323. FPIX_residuals_dPhi[{layerOrDiskNr, hit_idx+1, !is_sim_matched}]->fill(dPhi, seed.Et());
  324. }
  325. }
  326. }
  327. }
  328. tds.save_all();
  329. }
  330. int main(int argc, char * argv[]){
  331. using namespace fv_util;
  332. ArgParser args(argc, argv);
  333. bool silent = args.cmd_option_exists("-s");
  334. if(args.cmd_option_exists("-c")) {
  335. init_config(args.get_cmd_option("-c"));
  336. args.update_config();
  337. init_log(LogPriority::kLogInfo);
  338. // gSystem->Load("libfilval.so");
  339. run(silent);
  340. } else {
  341. cout << "Usage: ./" << argv[0] << " (-s) -c config_file.yaml" << endl;
  342. }
  343. return 0;
  344. }