tracking_validation.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <utility>
  5. #include <numeric>
  6. #include <limits>
  7. #include <TSystem.h>
  8. #include "filval/filval.hpp"
  9. #include "filval/root/filval.hpp"
  10. #include <boost/range/combine.hpp>
  11. #include <boost/format.hpp>
  12. #include "analysis/TrackingNtuple.h"
  13. #include "analysis/obj_types.cpp"
  14. #include "analysis/common.hpp"
  15. using namespace std;
  16. using namespace fv;
  17. using namespace fv::root;
  18. typedef std::pair<std::vector<float>,std::vector<float>> pair_vec;
  19. #define HIT_TYPE_PIXEL 0
  20. #define HIT_TYPE_GLUED 1
  21. #define HIT_TYPE_STRIP 2
  22. #define PIXEL_BARREL 1
  23. #define PIXEL_ENDCAP 2
  24. std::map<int,string> subdet_names = {{1, "BPIX"}, {2, "FPIX"}};
  25. typedef std::tuple<PixRecHit, SimHit> HitPair;
  26. void setup_hit_pair_functions(){
  27. func<pair_vec(vector<HitPair>)>("calc_dphi_v_eta",
  28. FUNC(([](const vector<HitPair>& hit_pairs){
  29. vector<float> dphis;
  30. vector<float> etas;
  31. for(auto hit_pair : hit_pairs){
  32. auto& pixrec_hit = std::get<PixRecHit>(hit_pair);
  33. auto& sim_hit = std::get<SimHit>(hit_pair);
  34. dphis.push_back(atan2(sim_hit.x, sim_hit.y) - atan2(pixrec_hit.x, pixrec_hit.y));
  35. etas.push_back(pseudorapidity(pixrec_hit));
  36. }
  37. return std::make_pair(etas, dphis);
  38. })));
  39. func<pair_vec(vector<HitPair>)>("calc_dr_v_eta",
  40. FUNC(([](const vector<HitPair>& hit_pairs){
  41. vector<float> drs;
  42. vector<float> etas;
  43. for(auto hit_pair : hit_pairs){
  44. auto& pixrec_hit = std::get<PixRecHit>(hit_pair);
  45. auto& sim_hit = std::get<SimHit>(hit_pair);
  46. drs.push_back(displacement(pixrec_hit, sim_hit));
  47. etas.push_back(pseudorapidity(pixrec_hit));
  48. }
  49. return std::make_pair(etas, drs);
  50. })));
  51. func<pair_vec(vector<HitPair>)>("calc_dz_v_eta",
  52. FUNC(([](const vector<HitPair>& hit_pairs){
  53. vector<float> dzs;
  54. vector<float> etas;
  55. for(auto hit_pair : hit_pairs){
  56. auto& pixrec_hit = std::get<PixRecHit>(hit_pair);
  57. auto& sim_hit = std::get<SimHit>(hit_pair);
  58. dzs.push_back(sim_hit.z - pixrec_hit.z);
  59. etas.push_back(pseudorapidity(pixrec_hit));
  60. }
  61. return std::make_pair(etas, dzs);
  62. })));
  63. func<pair_vec(vector<HitPair>)>("calc_drho_v_eta",
  64. FUNC(([](const vector<HitPair>& hit_pairs){
  65. vector<float> drhos;
  66. vector<float> etas;
  67. for(auto hit_pair : hit_pairs){
  68. auto& pixrec_hit = std::get<PixRecHit>(hit_pair);
  69. auto& sim_hit = std::get<SimHit>(hit_pair);
  70. drhos.push_back(rho(sim_hit) - rho(pixrec_hit));
  71. etas.push_back(pseudorapidity(pixrec_hit));
  72. }
  73. return std::make_pair(etas, drhos);
  74. })));
  75. }
  76. vector<string> seedTypes =
  77. {"initialStepSeeds",
  78. "highPtTripletStepSeeds",
  79. "mixedTripletStepSeeds",
  80. "pixelLessStepSeeds",
  81. "tripletElectronSeeds",
  82. "pixelPairElectronSeeds",
  83. "stripPairElectronSeeds"};
  84. Value<vector<SuperCluster>>* super_clusters;
  85. Value<vector<Seed>>* seeds;
  86. Value<vector<PixRecHit>>* pixrec_hits;
  87. Value<vector<Track>>* tracks;
  88. Value<vector<SimHit>>* sim_hits;
  89. Value<vector<SimTrack>>* sim_tracks;
  90. void register_objects(TrackingDataSet& tds){
  91. super_clusters = register_super_clusters(tds);
  92. seeds = register_seeds(tds);
  93. pixrec_hits = register_pixrec_hits(tds);
  94. tracks = register_tracks(tds);
  95. sim_hits = register_sim_hits(tds);
  96. sim_tracks = register_sim_tracks(tds);
  97. }
  98. template<typename A>
  99. bool in_det(const A& hit, const unsigned int& det, const unsigned int& lay){
  100. return hit.det == det && hit.lay == lay;
  101. }
  102. void setup_skipped_layer_hit_pairs(TrackingDataSet& tds){
  103. /* Finds SimHit/RecHit pairs
  104. */
  105. auto find_matched_nth_hit_in_layer_with_skip =
  106. func<vector<HitPair>(vector<Seed>,
  107. vector<PixRecHit>,
  108. vector<SimHit>,
  109. vector<Track>,
  110. vector<SimTrack>,
  111. int, int, int, bool)>("find_matched_nth_hit_in_layer_with_skip",
  112. FUNC(([](const vector<Seed>& seeds,
  113. const vector<PixRecHit>& pixrec_hits,
  114. const vector<SimHit>& sim_hits,
  115. const vector<Track>& tracks,
  116. const vector<SimTrack>& sim_tracks,
  117. const unsigned int&& det,
  118. const unsigned int&& pix_layer,
  119. const unsigned int&& skip,
  120. const bool&& first){
  121. vector<HitPair> matched_hits;
  122. for(const Track &trk : tracks){ // loop over all tracks
  123. const Seed &seed = seeds[trk.seedIdx];
  124. // Require seed's source algorithm is one of those in seedTypes
  125. if(seed.algoOriginal < 0 || seed.algoOriginal >= seedTypes.size()) continue;
  126. // Require seed w/ at least 2 hits
  127. if(seed.hitIdx.size() < 2) continue;
  128. // take only pixel hits for now
  129. if(seed.hitType[0] != HIT_TYPE_PIXEL || seed.hitType[1] != HIT_TYPE_PIXEL) continue;
  130. const PixRecHit &rec_hit1 = pixrec_hits[seed.hitIdx[0]];
  131. const PixRecHit &rec_hit2 = pixrec_hits[seed.hitIdx[1]];
  132. if(first){ // Looking at first hit in the pair
  133. if(in_det(rec_hit1, det, pix_layer) && in_det(rec_hit2, det, pix_layer+1+skip)){
  134. // We have the RecHit we want to work with, now find a properly* matched SimHit
  135. if(rec_hit1.simHitIdx.size() == 0) continue; // if rechit is matched to no simhits, give up.
  136. if(trk.simTrkIdx.size() == 0) continue; // if track is matched to no simtracks, give up.
  137. const int &simTrkIdx = trk.simTrkIdx[0]; // get first matched track
  138. for(const int& simHitIdx : sim_tracks[simTrkIdx].simHitIdx){ // loop over SimHits from SimTrack
  139. if(simHitIdx == rec_hit1.simHitIdx[0]) // take first matched simhit (should be the closest one)
  140. matched_hits.push_back({rec_hit1, sim_hits[rec_hit1.simHitIdx[0]]});
  141. }
  142. }
  143. }else{ // Looking at second hit in the pair
  144. if(in_det(rec_hit2, det, pix_layer) && in_det(rec_hit1, det, pix_layer-1-skip)){
  145. // We have the RecHit we want to work with, now find a properly* matched SimHit
  146. if(rec_hit2.simHitIdx.size() == 0) continue; // if rechit is matched to no simhits, give up.
  147. if(trk.simTrkIdx.size() == 0) continue; // if track is matched to no simtracks, give up.
  148. const int &simTrkIdx = trk.simTrkIdx[0]; // get first matched track
  149. for(const int& simHitIdx : sim_tracks[simTrkIdx].simHitIdx){ // loop over SimHits from SimTrack
  150. if(simHitIdx == rec_hit1.simHitIdx[0]) // take first matched simhit (should be the closest one)
  151. matched_hits.push_back({rec_hit2, sim_hits[rec_hit2.simHitIdx[0]]});
  152. }
  153. }
  154. }
  155. }
  156. return matched_hits;
  157. })));
  158. auto pix_barrel = constant("PIXEL_BARREL", PIXEL_BARREL);
  159. auto pix_endcap = constant("PIXEL_ENDCAP", PIXEL_ENDCAP);
  160. auto L1 = constant("L1", 1);
  161. auto L2 = constant("L2", 2);
  162. auto skip_zero = constant("skip_zero", 0);
  163. auto skip_one = constant("skip_one", 1);
  164. auto first = constant("first", true);
  165. auto second = constant("second", false);
  166. // First hit on 1st/2nd bpix layers and second hit in 2nd/3rd
  167. auto first_hits_in_B1_skip_zero = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  168. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_barrel, L1, skip_zero, first), "first_hits_in_B1_skip_zero");
  169. auto first_hits_in_B2_skip_zero = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  170. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_barrel, L2, skip_zero, first), "first_hits_in_B2_skip_zero");
  171. // First hit on 1st/2nd fpix layers and second hit in 2nd/3rd
  172. auto first_hits_in_F1_skip_zero = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  173. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_endcap, L1, skip_zero, first), "first_hits_in_F1_skip_zero");
  174. auto first_hits_in_F2_skip_zero = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  175. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_endcap, L2, skip_zero, first), "first_hits_in_F2_skip_zero");
  176. // First hit on 1st/2nd fpix layers and second hit in 3rd/4th
  177. auto first_hits_in_B1_skip_one = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  178. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_barrel, L1, skip_one, first), "first_hits_in_B1_skip_one");
  179. auto first_hits_in_B2_skip_one = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  180. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_barrel, L2, skip_one, first), "first_hits_in_B2_skip_one");
  181. // First hit on 1st fpix layer and second hit in 3rd layer
  182. auto first_hits_in_F1_skip_one = fv::tup_apply(find_matched_nth_hit_in_layer_with_skip,
  183. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, pix_endcap, L1, skip_one, first), "first_hits_in_F1_skip_one");
  184. TH2Params params_dz = {"$\\eta$", 100, -4, 4,
  185. "$\\Delta z$(rad)", 50, -.01, .01};
  186. auto calc_dz_v_eta = lookup_function<pair_vec(vector<HitPair>)>("calc_dz_v_eta");
  187. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B1_skip_zero",
  188. fv::apply(calc_dz_v_eta, first_hits_in_B1_skip_zero),
  189. "First Hit in BPIX-L1 - Skip 0", params_dz);
  190. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B2_skip_zero",
  191. fv::apply(calc_dz_v_eta, first_hits_in_B2_skip_zero),
  192. "First Hit in BPIX-L2 - Skip 0", params_dz);
  193. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_F1_skip_zero",
  194. fv::apply(calc_dz_v_eta, first_hits_in_F1_skip_zero),
  195. "First Hit in FPIX-L1 - Skip 0", params_dz);
  196. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_F2_skip_zero",
  197. fv::apply(calc_dz_v_eta, first_hits_in_F2_skip_zero),
  198. "First Hit in FPIX-L2 - Skip 0", params_dz);
  199. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B1_skip_one",
  200. fv::apply(calc_dz_v_eta, first_hits_in_B1_skip_one),
  201. "First Hit in BPIX-L1 - Skip 1", params_dz);
  202. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B2_skip_one",
  203. fv::apply(calc_dz_v_eta, first_hits_in_B2_skip_one),
  204. "First Hit in BPIX-L2 - Skip 1", params_dz);
  205. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_F1_skip_one",
  206. fv::apply(calc_dz_v_eta, first_hits_in_F1_skip_one),
  207. "First Hit in FPIX-L1 - Skip 1", params_dz);
  208. }
  209. void setup_first_hit_pairs(TrackingDataSet& tds){
  210. auto find_matched_nth_hit_in_layer =
  211. func<vector<HitPair>(vector<Seed>,
  212. vector<PixRecHit>,
  213. vector<SimHit>,
  214. vector<Track>,
  215. vector<SimTrack>,
  216. int, int, int)>("find_matched_nth_hit_in_layer",
  217. FUNC(([](const vector<Seed>& seeds,
  218. const vector<PixRecHit>& pixrec_hits,
  219. const vector<SimHit>& sim_hits,
  220. const vector<Track>& tracks,
  221. const vector<SimTrack>& sim_tracks,
  222. const unsigned int&& det,
  223. const unsigned int&& pix_layer,
  224. const unsigned int&& hit_number){
  225. vector<HitPair> matched_hits;
  226. for(const Track &trk : tracks){ // loop over all tracks
  227. const Seed &seed = seeds[trk.seedIdx];
  228. if(seed.hitIdx.size() <= hit_number) continue; // looking for hit_number'th hit
  229. if(seed.algoOriginal < 0 || seed.algoOriginal >= seedTypes.size()) continue;
  230. if(seed.hitType[hit_number] != HIT_TYPE_PIXEL) continue; // take only pixel hits for now
  231. const PixRecHit &rec_hit = pixrec_hits[seed.hitIdx[hit_number]];
  232. if(rec_hit.det == det && rec_hit.lay == pix_layer){
  233. // We have the RecHit we want to work with, now find a properly* matched SimHit
  234. if(rec_hit.simHitIdx.size() == 0) continue; // if rechit is matched to no simhits, give up.
  235. if(trk.simTrkIdx.size() == 0) continue; // if rechit is matched to no simhits, give up.
  236. const int &simTrkIdx = trk.simTrkIdx[0]; // get first matched track
  237. for(const int& simHitIdx : sim_tracks[simTrkIdx].simHitIdx){ // loop over SimHits from SimTrack
  238. if(simHitIdx == rec_hit.simHitIdx[0]) // take first matched simhit (should be the closest one)
  239. matched_hits.push_back({rec_hit, sim_hits[rec_hit.simHitIdx[0]]});
  240. }
  241. }
  242. }
  243. return matched_hits;
  244. })));
  245. auto barrel_val = constant("PIXEL_BARREL", PIXEL_BARREL);
  246. auto endcap_val = constant("PIXEL_ENDCAP", PIXEL_ENDCAP);
  247. auto first_hit = constant("1st", 0);
  248. auto second_hit = constant("2nd", 1);
  249. // First hits on inner three bpix layers
  250. auto first_hits_in_B1 = fv::tup_apply(find_matched_nth_hit_in_layer,
  251. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, barrel_val, constant("L1", 1), first_hit), "first_hits_in_B1");
  252. auto first_hits_in_B2 = fv::tup_apply(find_matched_nth_hit_in_layer,
  253. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, barrel_val, constant("L2", 2), first_hit), "first_hits_in_B2");
  254. // Second hits on outer three bpix layers
  255. auto second_hits_in_B2 = fv::tup_apply(find_matched_nth_hit_in_layer,
  256. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, barrel_val, constant("L2", 2), second_hit), "second_hits_in_B2");
  257. auto second_hits_in_B3 = fv::tup_apply(find_matched_nth_hit_in_layer,
  258. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, barrel_val, constant("L3", 3), second_hit), "second_hits_in_B3");
  259. auto second_hits_in_B4 = fv::tup_apply(find_matched_nth_hit_in_layer,
  260. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, barrel_val, constant("L4", 4), second_hit), "second_hits_in_B4");
  261. auto first_hits_in_F1 = fv::tup_apply(find_matched_nth_hit_in_layer,
  262. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, endcap_val, constant("L1", 1), first_hit), "first_hits_in_F1");
  263. auto first_hits_in_F2 = fv::tup_apply(find_matched_nth_hit_in_layer,
  264. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, endcap_val, constant("L2", 2), first_hit), "first_hits_in_F2");
  265. auto second_hits_in_F2 = fv::tup_apply(find_matched_nth_hit_in_layer,
  266. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, endcap_val, constant("L2", 2), second_hit), "second_hits_in_F2");
  267. auto second_hits_in_F3 = fv::tup_apply(find_matched_nth_hit_in_layer,
  268. fv::tuple(seeds, pixrec_hits, sim_hits, tracks, sim_tracks, endcap_val, constant("L3", 3), second_hit), "second_hits_in_F3");
  269. // Even vs Odd Ladders
  270. auto even = constant("even", false);
  271. auto odd = constant("odd", true);
  272. auto select_even_odd_ladder_blade_hit_pairs =
  273. func<vector<HitPair>(vector<HitPair>, bool)>("select_even_odd_ladder_blade_hit_pairs",
  274. FUNC(([](const vector<HitPair>& hit_pairs,
  275. const bool &&odd){
  276. vector<HitPair> even_pairs;
  277. for(const HitPair &hit_pair : hit_pairs){ // loop over all seeds
  278. if(std::get<PixRecHit>(hit_pair).ladder_blade % 2 == odd){
  279. even_pairs.push_back(hit_pair);
  280. }
  281. }
  282. return even_pairs;
  283. })));
  284. auto first_hits_in_B1_even_ladder = fv::tup_apply(select_even_odd_ladder_blade_hit_pairs,
  285. fv::tuple(first_hits_in_B1, even), "first_hits_in_B1_even_ladder");
  286. auto first_hits_in_B1_odd_ladder = fv::tup_apply(select_even_odd_ladder_blade_hit_pairs,
  287. fv::tuple(first_hits_in_B1, odd), "first_hits_in_B1_odd_ladder");
  288. auto first_hits_in_B2_even_ladder = fv::tup_apply(select_even_odd_ladder_blade_hit_pairs,
  289. fv::tuple(first_hits_in_B2, even), "first_hits_in_B2_even_ladder");
  290. auto first_hits_in_B2_odd_ladder = fv::tup_apply(select_even_odd_ladder_blade_hit_pairs,
  291. fv::tuple(first_hits_in_B2, odd), "first_hits_in_B2_odd_ladder");
  292. //Plots for dPhi of collections defined above
  293. auto calc_dphi_v_eta = lookup_function<pair_vec(vector<HitPair>)>("calc_dphi_v_eta");
  294. TH2Params params_dphi = {"$\\eta$", 100, -4, 4,
  295. "$\\Delta \\phi$(rad)", 50, -.0015, .0015};
  296. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B1",
  297. fv::apply(calc_dphi_v_eta, first_hits_in_B1),
  298. "First Hit in BPIX-L1", params_dphi);
  299. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B2",
  300. fv::apply(calc_dphi_v_eta, first_hits_in_B2),
  301. "First Hit in BPIX-L2", params_dphi);
  302. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B1_even_ladder",
  303. fv::apply(calc_dphi_v_eta, first_hits_in_B1_even_ladder),
  304. "First Hit in BPIX-L1 - Even Ladders", params_dphi);
  305. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B1_odd_ladder",
  306. fv::apply(calc_dphi_v_eta, first_hits_in_B1_odd_ladder),
  307. "First Hit in BPIX-L1 - Odd Ladders", params_dphi);
  308. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B2_even_ladder",
  309. fv::apply(calc_dphi_v_eta, first_hits_in_B2_even_ladder),
  310. "First Hit in BPIX-L2 - Even Ladders", params_dphi);
  311. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_B2_odd_ladder",
  312. fv::apply(calc_dphi_v_eta, first_hits_in_B2_odd_ladder),
  313. "First Hit in BPIX-L2 - Odd Ladders", params_dphi);
  314. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_second_hits_in_B2",
  315. fv::apply(calc_dphi_v_eta, second_hits_in_B2),
  316. "Second Hit in BPIX-L2", params_dphi);
  317. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_second_hits_in_B3",
  318. fv::apply(calc_dphi_v_eta, second_hits_in_B3),
  319. "Second Hit in BPIX-L3", params_dphi);
  320. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_second_hits_in_B4",
  321. fv::apply(calc_dphi_v_eta, second_hits_in_B4),
  322. "Second Hit in BPIX-L4", params_dphi);
  323. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_F1",
  324. fv::apply(calc_dphi_v_eta, first_hits_in_F1),
  325. "First Hit in FPIX-L1", params_dphi);
  326. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_first_hits_in_F2",
  327. fv::apply(calc_dphi_v_eta, first_hits_in_F2),
  328. "First Hit in FPIX-L2", params_dphi);
  329. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_second_hits_in_F2",
  330. fv::apply(calc_dphi_v_eta, second_hits_in_F2),
  331. "Second Hit in FPIX-L2", params_dphi);
  332. tds.register_container<ContainerTH2Many<float>>("dphi_v_eta_second_hits_in_F3",
  333. fv::apply(calc_dphi_v_eta, second_hits_in_F3),
  334. "Second Hit in FPIX-L3", params_dphi);
  335. //Plots for dr of collections defined above
  336. auto calc_dr_v_eta = lookup_function<pair_vec(vector<HitPair>)>("calc_dr_v_eta");
  337. TH2Params params_dr = {"$\\eta$", 100, -4, 4,
  338. "$\\Delta \\r$(rad)", 50, 0, .006};
  339. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B1",
  340. fv::apply(calc_dr_v_eta, first_hits_in_B1),
  341. "First Hit in BPIX-L1", params_dr);
  342. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B2",
  343. fv::apply(calc_dr_v_eta, first_hits_in_B2),
  344. "First Hit in BPIX-L2", params_dr);
  345. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B1_even_ladder",
  346. fv::apply(calc_dr_v_eta, first_hits_in_B1_even_ladder),
  347. "First Hit in BPIX-L1 - Even Ladders", params_dr);
  348. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B1_odd_ladder",
  349. fv::apply(calc_dr_v_eta, first_hits_in_B1_odd_ladder),
  350. "First Hit in BPIX-L1 - Odd Ladders", params_dr);
  351. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B2_even_ladder",
  352. fv::apply(calc_dr_v_eta, first_hits_in_B2_even_ladder),
  353. "First Hit in BPIX-L2 - Even Ladders", params_dr);
  354. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_B2_odd_ladder",
  355. fv::apply(calc_dr_v_eta, first_hits_in_B2_odd_ladder),
  356. "First Hit in BPIX-L2 - Odd Ladders", params_dr);
  357. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_second_hits_in_B2",
  358. fv::apply(calc_dr_v_eta, second_hits_in_B2),
  359. "Second Hit in BPIX-L2", params_dr);
  360. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_second_hits_in_B3",
  361. fv::apply(calc_dr_v_eta, second_hits_in_B3),
  362. "Second Hit in BPIX-L3", params_dr);
  363. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_second_hits_in_B4",
  364. fv::apply(calc_dr_v_eta, second_hits_in_B4),
  365. "Second Hit in BPIX-L4", params_dr);
  366. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_F1",
  367. fv::apply(calc_dr_v_eta, first_hits_in_F1),
  368. "First Hit in FPIX-L1", params_dr);
  369. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_first_hits_in_F2",
  370. fv::apply(calc_dr_v_eta, first_hits_in_F2),
  371. "First Hit in FPIX-L2", params_dr);
  372. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_second_hits_in_F2",
  373. fv::apply(calc_dr_v_eta, second_hits_in_F2),
  374. "Second Hit in FPIX-L2", params_dr);
  375. tds.register_container<ContainerTH2Many<float>>("dr_v_eta_second_hits_in_F3",
  376. fv::apply(calc_dr_v_eta, second_hits_in_F3),
  377. "Second Hit in FPIX-L3", params_dr);
  378. //Plots for dz of collections defined above
  379. auto calc_dz_v_eta = lookup_function<pair_vec(vector<HitPair>)>("calc_dz_v_eta");
  380. TH2Params params_dz = {"$\\eta$", 100, -4, 4,
  381. "$\\Delta z$(rad)", 50, -.01, .01};
  382. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B1",
  383. fv::apply(calc_dz_v_eta, first_hits_in_B1),
  384. "First Hit in BPIX-L1", params_dz);
  385. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B2",
  386. fv::apply(calc_dz_v_eta, first_hits_in_B2),
  387. "First Hit in BPIX-L2", params_dz);
  388. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B1_even_ladder",
  389. fv::apply(calc_dz_v_eta, first_hits_in_B1_even_ladder),
  390. "First Hit in BPIX-L1 - Even Ladders", params_dz);
  391. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B1_odd_ladder",
  392. fv::apply(calc_dz_v_eta, first_hits_in_B1_odd_ladder),
  393. "First Hit in BPIX-L1 - Odd Ladders", params_dz);
  394. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B2_even_ladder",
  395. fv::apply(calc_dz_v_eta, first_hits_in_B2_even_ladder),
  396. "First Hit in BPIX-L2 - Even Ladders", params_dz);
  397. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_first_hits_in_B2_odd_ladder",
  398. fv::apply(calc_dz_v_eta, first_hits_in_B2_odd_ladder),
  399. "First Hit in BPIX-L2 - Odd Ladders", params_dz);
  400. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_second_hits_in_B2",
  401. fv::apply(calc_dz_v_eta, second_hits_in_B2),
  402. "Second Hit in BPIX-L2", params_dz);
  403. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_second_hits_in_B3",
  404. fv::apply(calc_dz_v_eta, second_hits_in_B3),
  405. "Second Hit in BPIX-L3", params_dz);
  406. tds.register_container<ContainerTH2Many<float>>("dz_v_eta_second_hits_in_B4",
  407. fv::apply(calc_dz_v_eta, second_hits_in_B4),
  408. "Second Hit in BPIX-L4", params_dz);
  409. //Plots for drho of collections defined above
  410. auto calc_drho_v_eta = lookup_function<pair_vec(vector<HitPair>)>("calc_drho_v_eta");
  411. TH2Params params_drho = {"$\\eta$", 100, -4, 4,
  412. "$\\Delta \\rho$(cm)", 50, -.01, .01};
  413. tds.register_container<ContainerTH2Many<float>>("drho_v_eta_first_hits_in_F1",
  414. fv::apply(calc_drho_v_eta, first_hits_in_F1),
  415. "First Hit in FPIX-L1", params_drho);
  416. tds.register_container<ContainerTH2Many<float>>("drho_v_eta_first_hits_in_F2",
  417. fv::apply(calc_drho_v_eta, first_hits_in_F2),
  418. "First Hit in FPIX-L2", params_drho);
  419. tds.register_container<ContainerTH2Many<float>>("drho_v_eta_second_hits_in_F2",
  420. fv::apply(calc_drho_v_eta, second_hits_in_F2),
  421. "Second Hit in FPIX-L2", params_drho);
  422. tds.register_container<ContainerTH2Many<float>>("drho_v_eta_second_hits_in_F3",
  423. fv::apply(calc_drho_v_eta, second_hits_in_F3),
  424. "Second Hit in FPIX-L3", params_drho);
  425. }
  426. void sc_value_hists_for_lb(TrackingDataSet& tds, std::function<bool(int)>& parity_check,
  427. const std::string& label){
  428. auto relabel = [label](const std::string&& base){
  429. return base + "_" + label;
  430. };
  431. auto pix_barrel = constant("PIXEL_BARREL", PIXEL_BARREL);
  432. auto pix_endcap = constant("PIXEL_ENDCAP", PIXEL_ENDCAP);
  433. auto first_hit = constant("1st", 0);
  434. auto second_hit = constant("2nd", 1);
  435. auto L1 = constant("L1", 1);
  436. auto L2 = constant("L2", 2);
  437. auto L3 = constant("L3", 3);
  438. auto L4 = constant("L4", 4);
  439. auto dRz = constant("dRz", true);
  440. auto dPhi = constant("dPhi", false);
  441. auto pcheck = constant(label, parity_check);
  442. auto sc_hit_residuals = lookup_function<
  443. pair_vec(vector<SuperCluster>, int, int, int, bool, std::function<bool(int)>)>("sc_hit_residuals");
  444. // First hits on inner three bpix layers
  445. auto sc_first_hits_in_B1_dz = fv::tup_apply(sc_hit_residuals,
  446. fv::tuple(super_clusters, pix_barrel, L1, first_hit, dRz, pcheck), relabel("sc_first_hits_in_B1_dz"));
  447. auto sc_first_hits_in_B2_dz = fv::tup_apply(sc_hit_residuals,
  448. fv::tuple(super_clusters, pix_barrel, L2, first_hit, dRz, pcheck), relabel("sc_first_hits_in_B2_dz"));
  449. auto sc_first_hits_in_B3_dz = fv::tup_apply(sc_hit_residuals,
  450. fv::tuple(super_clusters, pix_barrel, L3, first_hit, dRz, pcheck), relabel("sc_first_hits_in_B3_dz"));
  451. auto sc_first_hits_in_B1_dphi = fv::tup_apply(sc_hit_residuals,
  452. fv::tuple(super_clusters, pix_barrel, L1, first_hit, dPhi, pcheck), relabel("sc_first_hits_in_B1_dphi"));
  453. auto sc_first_hits_in_B2_dphi = fv::tup_apply(sc_hit_residuals,
  454. fv::tuple(super_clusters, pix_barrel, L2, first_hit, dPhi, pcheck), relabel("sc_first_hits_in_B2_dphi"));
  455. auto sc_first_hits_in_B3_dphi = fv::tup_apply(sc_hit_residuals,
  456. fv::tuple(super_clusters, pix_barrel, L3, first_hit, dPhi, pcheck), relabel("sc_first_hits_in_B3_dphi"));
  457. // Second hits on outer three bpix layers
  458. auto sc_second_hits_in_B2_dz = fv::tup_apply(sc_hit_residuals,
  459. fv::tuple(super_clusters, pix_barrel, L2, second_hit, dRz, pcheck), relabel("sc_second_hits_in_B2_dz"));
  460. auto sc_second_hits_in_B3_dz = fv::tup_apply(sc_hit_residuals,
  461. fv::tuple(super_clusters, pix_barrel, L3, second_hit, dRz, pcheck), relabel("sc_second_hits_in_B3_dz"));
  462. auto sc_second_hits_in_B4_dz = fv::tup_apply(sc_hit_residuals,
  463. fv::tuple(super_clusters, pix_barrel, L4, second_hit, dRz, pcheck), relabel("sc_second_hits_in_B4_dz"));
  464. auto sc_second_hits_in_B2_dphi = fv::tup_apply(sc_hit_residuals,
  465. fv::tuple(super_clusters, pix_barrel, L2, second_hit, dPhi, pcheck), relabel("sc_second_hits_in_B2_dphi"));
  466. auto sc_second_hits_in_B3_dphi = fv::tup_apply(sc_hit_residuals,
  467. fv::tuple(super_clusters, pix_barrel, L3, second_hit, dPhi, pcheck), relabel("sc_second_hits_in_B3_dphi"));
  468. auto sc_second_hits_in_B4_dphi = fv::tup_apply(sc_hit_residuals,
  469. fv::tuple(super_clusters, pix_barrel, L4, second_hit, dPhi, pcheck), relabel("sc_second_hits_in_B4_dphi"));
  470. TH2Params params_dz = {"$\\eta$", 100, -4, 4,
  471. "$\\delta z$(cm)", 100, -0.7, 0.7};
  472. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B1_dz"),
  473. sc_first_hits_in_B1_dz,
  474. "First Hit in BPIX-L1", params_dz);
  475. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B2_dz"),
  476. sc_first_hits_in_B2_dz,
  477. "First Hit in BPIX-L2", params_dz);
  478. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B3_dz"),
  479. sc_first_hits_in_B3_dz,
  480. "First Hit in BPIX-L3", params_dz);
  481. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B2_dz"),
  482. sc_second_hits_in_B2_dz,
  483. "Second Hit in BPIX-L2", params_dz);
  484. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B3_dz"),
  485. sc_second_hits_in_B3_dz,
  486. "Second Hit in BPIX-L3", params_dz);
  487. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B4_dz"),
  488. sc_second_hits_in_B4_dz,
  489. "Second Hit in BPIX-L4", params_dz);
  490. TH2Params params_dphi = {"$\\eta$", 100, -4, 4,
  491. "$\\delta \\phi$(rad)", 500, -0.7, 0.7};
  492. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B1_dphi"),
  493. sc_first_hits_in_B1_dphi,
  494. "First Hit in BPIX-L1", params_dphi);
  495. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B2_dphi"),
  496. sc_first_hits_in_B2_dphi,
  497. "First Hit in BPIX-L2", params_dphi);
  498. tds.register_container<ContainerTH2Many<float>>(relabel("sc_first_hits_in_B3_dphi"),
  499. sc_first_hits_in_B3_dphi,
  500. "First Hit in BPIX-L3", params_dphi);
  501. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B2_dphi"),
  502. sc_second_hits_in_B2_dphi,
  503. "Second Hit in BPIX-L2", params_dphi);
  504. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B3_dphi"),
  505. sc_second_hits_in_B3_dphi,
  506. "Second Hit in BPIX-L3", params_dphi);
  507. tds.register_container<ContainerTH2Many<float>>(relabel("sc_second_hits_in_B4_dphi"),
  508. sc_second_hits_in_B4_dphi,
  509. "Second Hit in BPIX-L4", params_dphi);
  510. }
  511. void setup_sc_residuals(TrackingDataSet& tds){
  512. func<pair_vec(vector<SuperCluster>,
  513. int, int, int, bool,
  514. std::function<bool(int)>)>("sc_hit_residuals",
  515. FUNC(([](const vector<SuperCluster>& super_clusters,
  516. const int&& det,
  517. const int&& pix_layer,
  518. const int&& hit_number,
  519. const bool&& sel_dRz,
  520. const std::function<bool(int)>& pcheck){
  521. vector<float> residuals;
  522. vector<float> etas;
  523. for(const SuperCluster& super_cluster : super_clusters){ // loop over all supser-clusters
  524. float dRz, dPhi;
  525. int ladder_blade;
  526. unsigned int nSeeds = super_cluster.charge.size();
  527. for(unsigned int seedIdx=0; seedIdx<nSeeds; seedIdx++){ // loop over all seeds associated w/ sc
  528. if(hit_number == 0){
  529. if(super_cluster.lay1[seedIdx] != pix_layer || super_cluster.subDet1[seedIdx] != det) continue;
  530. dRz = super_cluster.dRz1[seedIdx];
  531. dPhi = super_cluster.dPhi1[seedIdx];
  532. ladder_blade = super_cluster.ladder_blade1[seedIdx];
  533. }else{
  534. if(super_cluster.lay2[seedIdx] != pix_layer || super_cluster.subDet2[seedIdx] != det) continue;
  535. dRz = super_cluster.dRz2[seedIdx];
  536. dPhi = super_cluster.dPhi2[seedIdx];
  537. ladder_blade = super_cluster.ladder_blade2[seedIdx];
  538. }
  539. DEBUG("New Seed, Idx: " << seedIdx << endl
  540. << "\tdRz: " << dRz << endl
  541. << "\tdPhi: " << dPhi << endl);
  542. if(!pcheck(ladder_blade)) continue;
  543. if(sel_dRz)
  544. residuals.push_back(dRz);
  545. else
  546. residuals.push_back(dPhi);
  547. etas.push_back(pseudorapidityP(super_cluster));
  548. }
  549. }
  550. return std::make_pair(etas,residuals);
  551. })));
  552. std::function<bool(int)> even = [](const int& t){ return !(t%2); };
  553. std::function<bool(int)> odd = [](const int& t){ return (t%2); };
  554. std::function<bool(int)> both = [](const int&){ return true; };
  555. sc_value_hists_for_lb(tds, even, "even");
  556. sc_value_hists_for_lb(tds, odd, "odd");
  557. sc_value_hists_for_lb(tds, both, "either");
  558. }
  559. void run_analysis(const vector<fv::util::DataFileDescriptor>& dfds, const string output_filename, bool silent){
  560. gSystem->Load("libfilval.so");
  561. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  562. return input.substr(0, input.find_last_of(".")) + new_suffix;
  563. };
  564. string log_filename = replace_suffix(output_filename, ".log");
  565. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogInfo);
  566. TrackingDataSet tds(output_filename, dfds, "trackingNtuple/tree");
  567. /* tds.set_max_events(10); */
  568. register_objects(tds);
  569. setup_hit_pair_functions();
  570. setup_first_hit_pairs(tds);
  571. setup_skipped_layer_hit_pairs(tds);
  572. setup_sc_residuals(tds);
  573. tds.process(silent);
  574. tds.save_all();
  575. }
  576. int main(int argc, char * argv[]){
  577. fv::util::ArgParser args(argc, argv);
  578. bool silent = args.cmdOptionExists("-s");
  579. string output_filename = args.cmdOptionExists("-o") ? args.getCmdOption("-o") : "output.root";
  580. if(args.cmdOptionExists("-F")){
  581. auto file_list = fv::util::read_input_list(args.getCmdOption("-F"));
  582. run_analysis(file_list, output_filename, silent);
  583. }else if(args.cmdOptionExists("-f")){
  584. string input_filename = args.getCmdOption("-f");
  585. string data_label = args.cmdOptionExists("-l") ? args.getCmdOption("-l") : "";
  586. string data_category = args.cmdOptionExists("-c") ? args.getCmdOption("-c") : "";
  587. fv::util::DataFileDescriptor dfd(input_filename, data_label, data_category);
  588. run_analysis(std::vector<fv::util::DataFileDescriptor>({dfd}), output_filename, silent);
  589. }else {
  590. cout << "Usage: ./tracking_validation (-s) {-o output_filename} -F datafiles.txt" << endl;
  591. cout << " ./tracking_validation (-s) {-l DATA_LABEL} {-c DATA_CATEGORY} {-o output_filename} -f treefile.root" << endl;
  592. }
  593. return 0;
  594. }