tracking_validation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <numeric>
  5. #include <limits>
  6. #include <cmath>
  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 "TrackingNtuple.h"
  13. #include "analysis/obj_types.cpp"
  14. using namespace std;
  15. using namespace fv;
  16. using namespace fv::root;
  17. using namespace std;
  18. #define HIT_TYPE_PIXEL 0
  19. #define HIT_TYPE_GLUED 1
  20. #define HIT_TYPE_STRIP 2
  21. vector<string> seedTypes =
  22. {"initialStepSeeds",
  23. "highPtTripletStepSeeds",
  24. "mixedTripletStepSeeds",
  25. "pixelLessStepSeeds",
  26. "tripletElectronSeeds",
  27. "pixelPairElectronSeeds",
  28. "stripPairElectronSeeds"};
  29. int nSeedTypes = seedTypes.size();
  30. Value<vector<Seed>>* seeds;
  31. Value<vector<PixRecHit>>* pixrec_hits;
  32. Value<vector<Track>>* tracks;
  33. Value<vector<SimHit>>* sim_hits;
  34. Value<vector<SimTrack>>* sim_tracks;
  35. void register_objects(TrackingDataSet& tds){
  36. seeds = register_seeds(tds);
  37. pixrec_hits = register_pixrec_hits(tds);
  38. tracks = register_tracks(tds);
  39. sim_hits = register_sim_hits(tds);
  40. sim_tracks = register_sim_tracks(tds);
  41. }
  42. void setup_matched_tracks(TrackingDataSet& tds){
  43. // Finds pairs of (track,sim_track) where the cycle
  44. // track->seed->rec_hit->sim_hit->sim_track->track
  45. // is closed
  46. typedef std::tuple<Seed, PixRecHit, Track, SimHit, SimTrack> MatchedTrack;
  47. auto& find_matched_tracks =
  48. func<vector<MatchedTrack>(vector<Seed>,
  49. vector<PixRecHit>,
  50. vector<Track>,
  51. vector<SimHit>,
  52. vector<SimTrack>)>("find_matched_tracks",
  53. FUNC(([](const vector<Seed>& seeds,
  54. const vector<PixRecHit> pixrec_hits,
  55. const vector<Track>& tracks,
  56. const vector<SimHit> sim_hits,
  57. const vector<SimTrack> sim_tracks){
  58. INFO("New event");
  59. INFO(boost::format("Number of tracks is %d, number of seeds is %d, number of hits %d, number of simhits %d")
  60. % tracks.size() % seeds.size() % pixrec_hits.size() % sim_hits.size());
  61. vector<MatchedTrack> matched_tracks;
  62. for(const Track &track : tracks){
  63. const Seed& seed = seeds[track.seedIdx];
  64. if( seed.algoOriginal < 0 || seed.algoOriginal >= nSeedTypes) continue;
  65. INFO(boost::format(" seed indx= %d algorithm= %d type %s\n")
  66. % track.seedIdx % seed.algoOriginal % seedTypes[seed.algoOriginal]);
  67. for(auto tup : boost::combine(seed.hitIdx, seed.hitType)){ //for hits in track's seed
  68. int hitIdx, hitType;
  69. boost::tie(hitIdx, hitType) = tup;
  70. if(hitType != HIT_TYPE_PIXEL) continue; // take only pixel hits for now
  71. const PixRecHit &rec_hit = pixrec_hits[hitIdx];
  72. TVector3 recoHitPoint;
  73. recoHitPoint.SetXYZ( rec_hit.x, rec_hit.y, rec_hit.z);
  74. float Rreco = recoHitPoint.Perp();
  75. INFO(boost::format(" hit= %d is at radius= %f\n") % hitIdx % Rreco);
  76. for(const int& simHitIdx : rec_hit.simHitIdx){ // for sim-hits matching rec-hit
  77. const SimHit &sim_hit = sim_hits[simHitIdx];
  78. const SimTrack &sim_track = sim_tracks[sim_hit.simTrkIdx];
  79. for(const int &trkIdx : sim_track.trkIdx){ // for tracks matched to this sim_track
  80. if(trkIdx == track.idx){ // sim_track matches with our original track
  81. matched_tracks.push_back({seed, rec_hit, track, sim_hit, sim_track});
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return matched_tracks;
  89. })));
  90. auto matched_tracks =
  91. fv::apply(find_matched_tracks, fv::tuple(seeds, pixrec_hits, tracks, sim_hits, sim_tracks), "matched_tracks");
  92. auto delta_pt = fv::map(func<float(MatchedTrack)>("matched_track_delta_pt",
  93. FUNC(([](const MatchedTrack& matched_track){
  94. const Track& track = std::get<Track>(matched_track);
  95. const SimTrack& sim_track = std::get<SimTrack>(matched_track);
  96. return track.pt - sim_track.pt;
  97. }))), matched_tracks, "matched_track_delta_pt");
  98. tds.register_container<ContainerTH1Many<float>>("matched_track_delta_pt", "Matched Track delta P_t",
  99. delta_pt, 20, -10, 10, "P_t");
  100. /* auto dphi_vs_eta_B1 = */
  101. /* fv::apply(calc_dphi_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("1", 1)), "dphi_vs_eta_B1"); */
  102. /* auto dphi_vs_eta_B2 = */
  103. /* fv::apply(calc_dphi_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("2", 2)), "dphi_vs_eta_B2"); */
  104. /* auto dphi_vs_eta_B3 = */
  105. /* fv::apply(calc_dphi_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("3", 3)), "dphi_vs_eta_B3"); */
  106. /* auto dphi_vs_eta_B4 = */
  107. /* fv::apply(calc_dphi_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("4", 4)), "dphi_vs_eta_B4"); */
  108. /* tds.register_container<ContainerTH2Many<float>>("dphi_vs_eta_B1", "dphi vs eta (BPIX L1)", */
  109. /* dphi_vs_eta_B1, 20, -3, 3, 100, -0.002, 0.002, */
  110. /* "eta", "dphi"); */
  111. /* tds.register_container<ContainerTH2Many<float>>("dphi_vs_eta_B2", "dphi vs eta (BPIX L2)", */
  112. /* dphi_vs_eta_B2, 20, -3, 3, 100, -0.002, 0.002, */
  113. /* "eta", "dphi"); */
  114. /* tds.register_container<ContainerTH2Many<float>>("dphi_vs_eta_B3", "dphi vs eta (BPIX L3)", */
  115. /* dphi_vs_eta_B3, 20, -3, 3, 100, -0.002, 0.002, */
  116. /* "eta", "dphi"); */
  117. /* tds.register_container<ContainerTH2Many<float>>("dphi_vs_eta_B4", "dphi vs eta (BPIX L4)", */
  118. /* dphi_vs_eta_B4, 20, -3, 3, 100, -0.002, 0.002, */
  119. /* "eta", "dphi"); */
  120. }
  121. /* void setup_dz_vs_eta(TrackingDataSet& tds){ */
  122. /* auto& calc_dz_vs_eta = */
  123. /* func<std::pair<vector<float>,vector<float>>(vector<Seed>, */
  124. /* vector<PixRecHit>, */
  125. /* vector<SimHit>, int)>("calc_dz_vs_eta", */
  126. /* FUNC(([](const vector<Seed>& seeds, */
  127. /* const vector<PixRecHit> pixrec_hits, */
  128. /* const vector<SimHit> sim_hits, */
  129. /* int layer){ */
  130. /* vector<float> dz; */
  131. /* vector<float> eta; */
  132. /* for(const Seed &seed : seeds){ */
  133. /* int hitIdx, hitType; */
  134. /* for(auto tup : boost::combine(seed.hitIdx, seed.hitType)){ */
  135. /* boost::tie(hitIdx, hitType) = tup; */
  136. /* if(hitType == HIT_TYPE_PIXEL){ */
  137. /* const PixRecHit &hit = pixrec_hits[hitIdx]; */
  138. /* if(layer && hit.lay != layer) continue; */
  139. /* for(const int& simHitIdx : hit.simHitIdx){ */
  140. /* dz.push_back(hit.z-sim_hits[simHitIdx].z); */
  141. /* eta.push_back(seed.eta); */
  142. /* } */
  143. /* } */
  144. /* } */
  145. /* } */
  146. /* return std::make_pair(eta,dz); */
  147. /* }))); */
  148. /* auto dz_vs_eta_B1 = */
  149. /* fv::apply(calc_dz_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("1", 1)), "dz_vs_eta_B1"); */
  150. /* auto dz_vs_eta_B2 = */
  151. /* fv::apply(calc_dz_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("2", 2)), "dz_vs_eta_B2"); */
  152. /* auto dz_vs_eta_B3 = */
  153. /* fv::apply(calc_dz_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("3", 3)), "dz_vs_eta_B3"); */
  154. /* auto dz_vs_eta_B4 = */
  155. /* fv::apply(calc_dz_vs_eta, fv::tuple(seeds, pixrec_hits, sim_hits, constant<int>("4", 4)), "dz_vs_eta_B4"); */
  156. /* tds.register_container<ContainerTH2Many<float>>("dz_vs_eta_B1", "dz vs eta (BPIX L1)", */
  157. /* dz_vs_eta_B1, 20, -3, 3, 20, -.01, .01, */
  158. /* "eta", "dz"); */
  159. /* tds.register_container<ContainerTH2Many<float>>("dz_vs_eta_B2", "dz vs eta (BPIX L2)", */
  160. /* dz_vs_eta_B2, 20, -3, 3, 20, -.01, .01, */
  161. /* "eta", "dz"); */
  162. /* tds.register_container<ContainerTH2Many<float>>("dz_vs_eta_B3", "dz vs eta (BPIX L3)", */
  163. /* dz_vs_eta_B3, 20, -3, 3, 20, -.01, .01, */
  164. /* "eta", "dz"); */
  165. /* tds.register_container<ContainerTH2Many<float>>("dz_vs_eta_B4", "dz vs eta (BPIX L4)", */
  166. /* dz_vs_eta_B4, 20, -3, 3, 20, -.01, .01, */
  167. /* "eta", "dz"); */
  168. /* } */
  169. void run_analysis(const vector<fv::util::DataFileDescriptor>& dfds, const string output_filename, bool silent){
  170. gSystem->Load("libfilval.so");
  171. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  172. return input.substr(0, input.find_last_of(".")) + new_suffix;
  173. };
  174. string log_filename = replace_suffix(output_filename, ".log");
  175. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
  176. TrackingDataSet tds(output_filename, dfds, "trackingNtuple/tree");
  177. /* tds.set_max_events(10); */
  178. register_objects(tds);
  179. /* setup_dz_vs_eta(tds); */
  180. /* setup_dphi_vs_eta(tds); */
  181. setup_matched_tracks(tds);
  182. tds.process(silent);
  183. tds.save_all();
  184. }
  185. int main(int argc, char * argv[])
  186. {
  187. fv::util::ArgParser args(argc, argv);
  188. bool silent = args.cmdOptionExists("-s");
  189. string output_filename = args.cmdOptionExists("-o") ? args.getCmdOption("-o") : "output.root";
  190. if(args.cmdOptionExists("-F")){
  191. auto file_list = fv::util::read_input_list(args.getCmdOption("-F"));
  192. run_analysis(file_list, output_filename, silent);
  193. }else if(args.cmdOptionExists("-f")) {
  194. string input_filename = args.getCmdOption("-f");
  195. string data_label = args.cmdOptionExists("-l") ? args.getCmdOption("-l") : "";
  196. fv::util::DataFileDescriptor dfd(input_filename, data_label);
  197. run_analysis(std::vector<fv::util::DataFileDescriptor>({dfd}), output_filename, silent);
  198. }else {
  199. cout << "Usage: ./tracking_validation (-s) {-o output_filename} -F datafiles.txt" << endl;
  200. cout << " ./tracking_validation (-s) {-l DATA_LABEL} {-o output_filename} -f treefile.root" << endl;
  201. }
  202. }