tracking_eff.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <numeric>
  5. #include <utility>
  6. #include <TSystem.h>
  7. #include <Math/VectorUtil.h>
  8. #include "filval.hpp"
  9. #include "root_filval.hpp"
  10. #include "common.hpp"
  11. #include "analysis/TrackingNtupleObjs.hpp"
  12. const double pi = 3.1415926535897932384626;
  13. const double pi2 = 2*pi;
  14. typedef ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float>> Vec4;
  15. SeedCollection seeds;
  16. GenCollection gens;
  17. SimTrackCollection sim_tracks;
  18. SimVertexCollection sim_vertices;
  19. TrackCollection gsf_tracks;
  20. SuperClusterCollection scls;
  21. std::vector<SimTrack> sim_els;
  22. Value<float>* truePileup;
  23. Value<int>* inTimePileup;
  24. /* Value<vector<int>>* outOfTimePileup; */
  25. /* Value<vector<unsigned int>>* totalSeedsAlgo; */
  26. Value<unsigned int>* totalSeeds;
  27. void register_objects(TrackingDataSet& tds){
  28. seeds.init(&tds);
  29. gens.init(&tds);
  30. sim_tracks.init(&tds);
  31. sim_vertices.init(&tds);
  32. gsf_tracks.init(&tds);
  33. scls.init(&tds);
  34. }
  35. struct {
  36. Value<float>* x;
  37. Value<float>* y;
  38. Value<float>* z;
  39. Value<float>* sigma_x;
  40. Value<float>* sigma_y;
  41. Value<float>* sigma_z;
  42. } bs;
  43. struct EffColl {
  44. EfficiencyContainer<float>* pt;
  45. EfficiencyContainer<float>* eta;
  46. EfficiencyContainer<float>* phi;
  47. EffColl(TrackingDataSet& tds, const std::string& pfx,
  48. THParams pt_params, THParams eta_params, THParams phi_params,
  49. const std::string& sfx="") {
  50. std::string _sfx = "";
  51. if (sfx != "") _sfx = "_"+sfx;
  52. pt = tds.register_container<EfficiencyContainer<float>>(pfx+"_v_pt"+_sfx, pt_params);
  53. eta = tds.register_container<EfficiencyContainer<float>>(pfx+"_v_eta"+_sfx, eta_params);
  54. phi = tds.register_container<EfficiencyContainer<float>>(pfx+"_v_phi"+_sfx, phi_params);
  55. }
  56. };
  57. struct KinColl {
  58. ContainerTH1<float>* pt;
  59. ContainerTH1<float>* eta;
  60. ContainerTH1<float>* phi;
  61. ContainerTH2<float>* eta_phi;
  62. KinColl(TrackingDataSet& tds, const std::string& pfx,
  63. THParams pt_params, THParams eta_params, THParams phi_params,
  64. const std::string& sfx="") {
  65. std::string _sfx = "";
  66. if (sfx != "") _sfx = "_"+sfx;
  67. pt = tds.register_container<ContainerTH1<float>>(pfx+"_v_pt"+_sfx, pt_params);
  68. eta = tds.register_container<ContainerTH1<float>>(pfx+"_v_eta"+_sfx, eta_params);
  69. phi = tds.register_container<ContainerTH1<float>>(pfx+"_v_phi"+_sfx, phi_params);
  70. THParams eta_phi_params = eta_params;
  71. eta_phi_params.low_y = phi_params.low_x;
  72. eta_phi_params.high_y = phi_params.high_x;
  73. eta_phi_params.label_y = phi_params.label_x;
  74. eta_phi = tds.register_container<ContainerTH2<float>>(pfx+"_v_eta_phi"+_sfx, eta_phi_params);
  75. }
  76. };
  77. template <typename T1, typename T2>
  78. struct AugKinColl {
  79. T1* pt;
  80. T1* eta;
  81. T1* phi;
  82. T2* eta_phi;
  83. };
  84. int get_n_pv() {
  85. /* std::cout << "------------------------------------------------" << std::endl; */
  86. int n_pv = 0;
  87. for (const auto& sim_vertex : sim_vertices) {
  88. if (sim_vertex.sourceSimIdx().size() == 0) {
  89. /* std::cout << "(" */
  90. /* << sim_vertex.x() << ", " */
  91. /* << sim_vertex.y() << ", " */
  92. /* << sim_vertex.z() << ") " */
  93. /* << sim_vertex.processType() << std::endl; */
  94. n_pv++;
  95. }
  96. }
  97. /* std::cout << "::::: " << pileup << std::endl; */
  98. return n_pv / 2; // for some reason, all vertices are repeated twice...
  99. }
  100. bool in_lum_region(const SimVertex& vertex) {
  101. /* Here approximate the luminous region as a cylinder with length 5*\sigma_z and radius
  102. * sqrt(\simga_x^2 + \sigma_y^2) centered as beamspot position.
  103. */
  104. float dx = vertex.x() - bs.x->get();
  105. float dy = vertex.y() - bs.y->get();
  106. float dz = vertex.z() - bs.z->get();
  107. auto radius = static_cast<float>(5 * sqrt(pow(bs.sigma_x->get(), 2) + pow(bs.sigma_y->get(), 2)));
  108. float half_len = 5*bs.sigma_z->get();
  109. return sqrt(dx*dx + dy*dy) < radius and abs(dz) < half_len;
  110. };
  111. bool is_good_sim(const SimTrack& sim_track) {
  112. const auto& vertex = sim_vertices[sim_track.parentVtxIdx()];
  113. return abs(sim_track.pdgId()) == 11 and // electrons
  114. in_lum_region(vertex) and // from luminous region
  115. abs(sim_track.eta())<3.0; // inside ECAL acceptance
  116. };
  117. bool is_good_sim(const std::vector<SimTrack> prompt_sims, const SimTrack& sim_track) {
  118. return find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end();
  119. };
  120. bool is_good_seed(const Seed& seed, float hoe_cut) {
  121. return seed.isECALDriven() and seed.hoe() < hoe_cut;
  122. }
  123. float reco_energy_rel_err(const SimTrack& sim_track, const Seed& seed) {
  124. return (sim_track.pt() - seed.Et()) / sim_track.pt() ;
  125. }
  126. float reco_energy_rel_err(const SimTrack& sim_track, const Track& track) {
  127. return (sim_track.pt() - track.pt()) / sim_track.pt() ;
  128. }
  129. template<typename TrackOrSeed>
  130. bool reco_energy_consistent(const SimTrack& sim_track, const TrackOrSeed& track, float consistency_cut=0.1) {
  131. return fabs(reco_energy_rel_err(sim_track, track)) < consistency_cut;
  132. }
  133. Vec4 scl_p4(const SuperCluster& scl) {
  134. return Vec4(scl.px(), scl.py(), scl.pz(), scl.e());
  135. }
  136. double dphi(double phi1, double phi2) {
  137. auto spec_mod = [](double a) {
  138. return a - floor(a/pi2)*pi2;
  139. };
  140. return spec_mod(phi2 - phi1 + pi) - pi;
  141. }
  142. double deltaR(double eta1, double phi1, double eta2, double phi2) {
  143. return sqrt(pow(eta1 - eta2, 2.0) +
  144. pow(dphi(phi1, phi2), 2.0));
  145. }
  146. void update_sim_els() {
  147. sim_els.clear();
  148. for (const SimTrack sim_track: sim_tracks) {
  149. if (is_good_sim(sim_track)) {
  150. sim_els.push_back(sim_track);
  151. }
  152. }
  153. }
  154. std::tuple<std::vector<SimTrack>, std::vector<SimTrack>> get_prompt_sims() {
  155. /*
  156. * Returns sim tracks that pass is_good_sim as well as match well with a
  157. * prompt gen electron
  158. */
  159. std::vector<Gen> prompt_gen;
  160. for (const auto& gen : gens) {
  161. if (abs(gen.pdgId()) == 11 and gen.isPrompt()) {
  162. prompt_gen.push_back(gen);
  163. }
  164. }
  165. std::vector<SimTrack> prompt_sim;
  166. for (const auto& gen : prompt_gen) {
  167. float gen_phi = atan2(gen.py(), gen.px());
  168. float gen_eta = pseudorapidityP(gen.px(), gen.py(), gen.pz());
  169. int gen_id = gen.pdgId();
  170. for (const auto& sim_el : sim_els) {
  171. float sim_phi = sim_el.phi();
  172. float sim_eta = sim_el.eta();
  173. int sim_id = sim_el.pdgId();
  174. if (gen_id == sim_id and deltaR(gen_eta, gen_phi, sim_eta, sim_phi) < 0.05) {
  175. prompt_sim.push_back(sim_el);
  176. break;
  177. }
  178. }
  179. }
  180. std::vector<SimTrack> nonprompt_sim;
  181. for (const auto& sim_el : sim_els) {
  182. if (find(prompt_sim.begin(), prompt_sim.end(), sim_el) == prompt_sim.end()) {
  183. nonprompt_sim.push_back(sim_el);
  184. }
  185. }
  186. return make_tuple(prompt_sim, nonprompt_sim);
  187. }
  188. auto get_match_stats(bool dRMatch) {
  189. int nSimTrack = 0;
  190. int nGSFTrack = 0;
  191. int nMatched = 0; // 1-to-1
  192. int nMerged = 0; // n-to-1
  193. int nLost = 0; // 1-to-0
  194. int nSplit = 0; // 1-to-n
  195. int nFaked = 0; // 0-to-1
  196. int nFlipped = 0;
  197. vector<float> matched_dR;
  198. vector<float> matched_dpT;
  199. std::map<int, std::vector<int>> sim_matches;
  200. std::map<int, std::vector<int>> gsf_matches;
  201. for(const auto& sim_track : sim_els)
  202. sim_matches[sim_track.idx] = {};
  203. for(const auto& gsf_track : gsf_tracks)
  204. gsf_matches[gsf_track.idx] = {};
  205. nSimTrack = static_cast<int>(sim_matches.size());
  206. nGSFTrack = static_cast<int>(gsf_matches.size());
  207. for (const auto& sim_track : sim_els) {
  208. if (dRMatch) {
  209. for (const auto& gsf_track : gsf_tracks) {
  210. double dR = deltaR(sim_track.eta(), sim_track.phi(), gsf_track.eta(), gsf_track.phi());
  211. if (dR < 0.2) {
  212. sim_matches[sim_track.idx].push_back(gsf_track.idx);
  213. gsf_matches[gsf_track.idx].push_back(sim_track.idx);
  214. matched_dR.push_back(dR);
  215. matched_dpT.push_back((sim_track.pt() - gsf_track.pt())/sim_track.pt());
  216. if (gsf_track.q() != sim_track.q()) {
  217. nFlipped++;
  218. }
  219. }
  220. }
  221. } else {
  222. for (const auto& gsfIdx : sim_track.trkIdx()) {
  223. const Track& gsf_track = gsf_tracks[gsfIdx];
  224. double dR = deltaR(sim_track.eta(), sim_track.phi(), gsf_track.eta(), gsf_track.phi());
  225. sim_matches[sim_track.idx].push_back(gsf_track.idx);
  226. gsf_matches[gsf_track.idx].push_back(sim_track.idx);
  227. matched_dR.push_back(static_cast<float &&>(dR));
  228. matched_dpT.push_back((sim_track.pt() - gsf_track.pt())/sim_track.pt());
  229. if (gsf_track.q() != sim_track.q()) {
  230. nFlipped++;
  231. }
  232. }
  233. }
  234. }
  235. for (const auto& p : sim_matches) {
  236. const auto& matches = p.second;
  237. if (matches.size() == 0) nLost++;
  238. if (matches.size() == 1 and gsf_matches[matches[0]].size() == 1) nMatched++;
  239. if (matches.size() > 1) nSplit++;
  240. }
  241. for (const auto& p : gsf_matches) {
  242. const auto& matches = p.second;
  243. if (matches.size() == 0) nFaked++;
  244. if (matches.size() > 1) nMerged++;
  245. }
  246. return std::make_tuple(nSimTrack, nGSFTrack, nMatched, nMerged, nLost, nSplit, nFaked, nFlipped, matched_dR, matched_dpT);
  247. }
  248. void run(){
  249. using namespace std;
  250. using namespace fv;
  251. using namespace fv_root;
  252. using namespace fv_util;
  253. auto lookup = [](const std::string&& key) {
  254. return THParams::lookup(std::forward<const std::string>(key));
  255. };
  256. auto file_list = the_config->get_source_files();
  257. string output_filename = the_config->get_output_filename();
  258. TrackingDataSet tds(output_filename, file_list, "trackingNtuple/tree");
  259. register_objects(tds);
  260. auto hoe_cut = the_config->get("hoe_cut").as<float>(999);
  261. bs = {
  262. tds.track_branch<float>("bsp_x"),
  263. tds.track_branch<float>("bsp_y"),
  264. tds.track_branch<float>("bsp_z"),
  265. tds.track_branch<float>("bsp_sigmax"),
  266. tds.track_branch<float>("bsp_sigmay"),
  267. tds.track_branch<float>("bsp_sigmaz")
  268. };
  269. inTimePileup = tds.track_branch<int>("inTimePileup");
  270. truePileup = tds.track_branch<float>("truePileup");
  271. totalSeeds = tds.track_branch<unsigned int>("initsee_tot");
  272. enum TMType {
  273. NoMatch = 0,
  274. SeedMatched = 1,
  275. TrackMatched = 2,
  276. SeedAndTrackMatched = 3,
  277. };
  278. std::map<std::tuple<int, int>, ContainerTH2<float>*> residuals_dRz;
  279. std::map<std::tuple<int, int>, ContainerTH2<float>*> residuals_dPhi;
  280. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> BPIX_residuals_dRz;
  281. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> BPIX_residuals_dPhi;
  282. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> FPIX_residuals_dRz;
  283. std::map<std::tuple<int, int, int>, ContainerTH2<float>*> FPIX_residuals_dPhi;
  284. stringstream name;
  285. auto set_name = [&name](const std::string& var, const std::string& region,
  286. const int& layer, const int& hit, const int& tm_type) {
  287. name.str("");
  288. name << var << "_" << region;
  289. if (layer>0)
  290. name << "_L" << layer;
  291. name << "_H" << hit << "_v_Et";
  292. switch (tm_type) {
  293. case NoMatch:
  294. name << "_NoMatch";
  295. break;
  296. case SeedMatched:
  297. name << "_SeedMatched";
  298. break;
  299. case TrackMatched:
  300. name << "_TrackMatched";
  301. break;
  302. case SeedAndTrackMatched:
  303. name << "_SeedAndTrackMatched";
  304. break;
  305. default:
  306. break;
  307. }
  308. };
  309. THParams hist_params;
  310. for (int hit=1; hit<=3; hit++) {
  311. for (int tm_type = NoMatch; tm_type <= SeedAndTrackMatched; tm_type++) {
  312. for (int layer=1; layer<=4; layer++) {
  313. hist_params = (hit == 1) ? lookup("dRz_v_Et") : lookup("dRz_v_Et_outer_hits");
  314. set_name("dRz", "BPIX", layer, hit, tm_type);
  315. BPIX_residuals_dRz[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  316. set_name("dRz", "FPIX", layer, hit, tm_type);
  317. FPIX_residuals_dRz[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  318. hist_params = (hit == 1) ? lookup("dPhi_v_Et") : lookup("dPhi_v_Et_outer_hits");
  319. set_name("dPhi", "BPIX", layer, hit, tm_type);
  320. BPIX_residuals_dPhi[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  321. set_name("dPhi", "FPIX", layer, hit, tm_type);
  322. FPIX_residuals_dPhi[make_tuple(layer, hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  323. }
  324. hist_params = (hit == 1) ? lookup("dRz_v_Et") : lookup("dRz_v_Et_outer_hits");
  325. set_name("dRz", "ALL", 0, hit, tm_type);
  326. residuals_dRz[std::make_tuple(hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  327. hist_params = (hit == 1) ? lookup("dPhi_v_Et") : lookup("dPhi_v_Et_outer_hits");
  328. set_name("dPhi", "ALL", 0, hit, tm_type);
  329. residuals_dPhi[make_tuple(hit, tm_type)] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  330. }
  331. }
  332. std::map<int,vector<float>> eta_regions = {{1, {0.0, 1.1, 1.8, 3.0}},
  333. {2, {0.0, 1.4, 2.3, 3.0}},
  334. {3, {0.0, 1.0, 2.0, 3.0}}};
  335. auto get_region = [&eta_regions](int hit, float eta) {
  336. auto regions = eta_regions[hit];
  337. if (regions.size() <= 1) return 1;
  338. float abseta = abs(eta);
  339. for (int r_idx=1; r_idx < regions.size(); r_idx++) {
  340. if (abseta > regions[r_idx-1] and abseta < regions[r_idx]) {
  341. return r_idx;
  342. }
  343. }
  344. return static_cast<int>(regions.size());
  345. };
  346. std::map<int, ContainerTH2<float>*> dPhi_residuals_v_eta;
  347. std::map<int, ContainerTH2<float>*> dRz_residuals_v_eta;
  348. std::map<std::pair<int, int>, ContainerTH2<float>*> dPhi_residuals_v_region;
  349. std::map<std::pair<int, int>, ContainerTH2<float>*> dRz_residuals_v_region;
  350. for (int hit=1; hit<=3; hit++) {
  351. name.str("");
  352. name << "dPhi_residuals_v_eta_H" << hit;
  353. hist_params = (hit==1) ? lookup("dPhi_v_eta") : lookup("dPhi_v_eta_outer_hits");
  354. dPhi_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  355. name.str("");
  356. name << "dRz_residuals_v_eta_H" << hit;
  357. hist_params = (hit==1) ? lookup("dRz_v_eta") : lookup("dRz_v_eta_outer_hits");
  358. dRz_residuals_v_eta[hit] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  359. for (int region=1; region<=eta_regions[hit].size()-1; region++){
  360. hist_params = (hit==1) ? lookup("dRz_v_Et") : lookup("dRz_v_Et_outer_hits");
  361. name.str("");
  362. name << "dRz_residuals_H" << hit << "_R" << region;
  363. dRz_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  364. hist_params = (hit==1) ? lookup("dPhi_v_Et") : lookup("dPhi_v_Et_outer_hits");
  365. name.str("");
  366. name << "dPhi_residuals_H" << hit << "_R" << region;
  367. dPhi_residuals_v_region[{hit, region}] = tds.register_container<ContainerTH2<float>>(name.str(), hist_params);
  368. }
  369. }
  370. KinColl good_sim_kinems (tds, "good_sim", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  371. KinColl gsf_kinems (tds, "gsf", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  372. KinColl gsf_high_pt1_kinems(tds, "gsf_high_pt1", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  373. KinColl gsf_high_pt2_kinems(tds, "gsf_high_pt2", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  374. KinColl seed_kinems (tds, "seed", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  375. KinColl scl_kinems (tds, "scl", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  376. KinColl prompt_kinems (tds, "prompt", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  377. KinColl nonprompt_kinems (tds, "nonprompt", lookup("pt_fine"), lookup("eta_fine"), lookup("phi_fine"));
  378. EffColl seed_eff(tds, "seed_eff", lookup("pt"), lookup("eta"), lookup("phi"));
  379. EffColl seed_pur(tds, "seed_pur", lookup("pt"), lookup("eta"), lookup("phi"));
  380. EffColl tracking_eff (tds, "tracking_eff", lookup("pt"), lookup("eta"), lookup("phi"));
  381. EffColl tracking_pur (tds, "tracking_pur", lookup("pt"), lookup("eta"), lookup("phi"));
  382. EffColl prompt_eff (tds, "prompt_eff", lookup("pt"), lookup("eta"), lookup("phi"));
  383. EffColl prompt_pur (tds, "prompt_pur", lookup("pt"), lookup("eta"), lookup("phi"));
  384. EffColl nonprompt_eff(tds, "nonprompt_eff", lookup("pt"), lookup("eta"), lookup("phi"));
  385. EffColl nonprompt_pur(tds, "nonprompt_pur", lookup("pt"), lookup("eta"), lookup("phi"));
  386. EffColl tracking_eff_dR (tds, "tracking_eff", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  387. EffColl tracking_pur_dR (tds, "tracking_pur", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  388. EffColl prompt_eff_dR (tds, "prompt_eff", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  389. EffColl prompt_pur_dR (tds, "prompt_pur", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  390. EffColl nonprompt_eff_dR(tds, "nonprompt_eff", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  391. EffColl nonprompt_pur_dR(tds, "nonprompt_pur", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  392. auto& tracking_eff_v_PU_dR = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_PU_dR", lookup("PU"));
  393. auto& tracking_eff_v_PU_dR_inc = *tds.register_container<EfficiencyContainer<float>>("tracking_eff_v_PU_dR_inc", lookup("PU"));
  394. auto& tracking_pur_v_PU_dR = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_PU_dR", lookup("PU"));
  395. auto& tracking_pur_v_PU_dR_inc = *tds.register_container<EfficiencyContainer<float>>("tracking_pur_v_PU_dR_inc", lookup("PU"));
  396. // Full Efficiency/Purity includes tracker-driven seeds
  397. EffColl tracking_eff_full (tds, "tracking_eff_full", lookup("pt"), lookup("eta"), lookup("phi"));
  398. EffColl tracking_eff_full_dR(tds, "tracking_eff_full", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  399. EffColl tracking_pur_full (tds, "tracking_pur_full", lookup("pt"), lookup("eta"), lookup("phi"));
  400. EffColl tracking_pur_full_dR(tds, "tracking_pur_full", lookup("pt"), lookup("eta"), lookup("phi"), "dR");
  401. EffColl fake_rate (tds, "fake_rate", lookup("pt"), lookup("eta"), lookup("phi"));
  402. EffColl fake_rate_no_e_match(tds, "fake_rate_no_e_match", lookup("pt"), lookup("eta"), lookup("phi"));
  403. auto& fake_rate_no_e_match_v_PU = *tds.register_container<EfficiencyContainer<float>>("fake_rate_no_e_match_v_PU", lookup("PU"));
  404. auto& fake_rate_no_e_match_v_PU_inc = *tds.register_container<EfficiencyContainer<float>>("fake_rate_no_e_match_v_PU_inc", lookup("PU"));
  405. EffColl partial_fake_rate (tds, "partial_fake_rate", lookup("pt"), lookup("eta"), lookup("phi"));
  406. EffColl full_fake_rate (tds, "full_fake_rate", lookup("pt"), lookup("eta"), lookup("phi"));
  407. EffColl clean_fake_rate (tds, "clean_fake_rate", lookup("pt"), lookup("eta"), lookup("phi"));
  408. EffColl fake_rate_incl (tds, "fake_rate_incl", lookup("pt"), lookup("eta"), lookup("phi"));
  409. EffColl fake_rate_no_e_match_incl(tds, "fake_rate_no_e_match_incl", lookup("pt"), lookup("eta"), lookup("phi"));
  410. EffColl partial_fake_rate_incl (tds, "partial_fake_rate_incl", lookup("pt"), lookup("eta"), lookup("phi"));
  411. EffColl full_fake_rate_incl (tds, "full_fake_rate_incl", lookup("pt"), lookup("eta"), lookup("phi"));
  412. EffColl clean_fake_rate_incl (tds, "clean_fake_rate_incl", lookup("pt"), lookup("eta"), lookup("phi"));
  413. auto& hit_vs_layer_barrel = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_barrel", lookup("hit_vs_layer"));
  414. auto& hit_vs_layer_forward = *tds.register_container<ContainerTH2<int>>("hit_vs_layer_forward", lookup("hit_vs_layer"));
  415. auto& gsf_tracks_nmatch_sim_tracks = *tds.register_container<ContainerTH1<size_t>>("gsf_tracks_nmatch_sim_tracks ", lookup("gsf_tracks_nmatch_sim_tracks"));
  416. auto& ecal_energy_resolution = *tds.register_container<ContainerTH1<float>>("ecal_energy_resolution ", lookup("ecal_energy_resolution"));
  417. auto& n_seeds = *tds.register_container<ContainerTH1<size_t>>("n_seeds", lookup("n_seeds"));
  418. auto& n_good_seeds = *tds.register_container<ContainerTH1<size_t>>("n_good_seeds", lookup("n_seeds"));
  419. auto& n_good_sim = *tds.register_container<ContainerTH1<size_t>>("n_good_sim", lookup("n_seeds"));
  420. auto& n_prompt = *tds.register_container<ContainerTH1<size_t>>("n_prompt", lookup("n_seeds"));
  421. auto& n_nonprompt = *tds.register_container<ContainerTH1<size_t>>("n_nonprompt", lookup("n_seeds"));
  422. auto& n_gsf_tracks = *tds.register_container<ContainerTH1<size_t>>("n_gsf_track", lookup("n_seeds"));
  423. auto& n_scl = *tds.register_container<ContainerTH1<size_t>>("n_scl", lookup("n_seeds"));
  424. auto& n_good_scl = *tds.register_container<ContainerTH1<size_t>>("n_good_scl", lookup("n_seeds"));
  425. auto& n_matched = *tds.register_container<ContainerTH1<int>>("n_matched", lookup("n_seeds"));
  426. auto& n_merged = *tds.register_container<ContainerTH1<int>>("n_merged", lookup("n_seeds"));
  427. auto& n_lost = *tds.register_container<ContainerTH1<int>>("n_lost", lookup("n_seeds"));
  428. auto& n_split = *tds.register_container<ContainerTH1<int>>("n_split", lookup("n_seeds"));
  429. auto& n_faked = *tds.register_container<ContainerTH1<int>>("n_faked", lookup("n_seeds"));
  430. auto& n_flipped = *tds.register_container<ContainerTH1<int>>("n_flipped", lookup("n_seeds"));
  431. auto& matched_dR = *tds.register_container<ContainerTH1<float>>("matched_dR", lookup("matched_dR"));
  432. auto& matched_dpT = *tds.register_container<ContainerTH1<float>>("matched_dpT", lookup("matched_dpT"));
  433. auto& n_matched_dR = *tds.register_container<ContainerTH1<int>>("n_matched_dR", lookup("n_seeds"));
  434. auto& n_merged_dR = *tds.register_container<ContainerTH1<int>>("n_merged_dR", lookup("n_seeds"));
  435. auto& n_lost_dR = *tds.register_container<ContainerTH1<int>>("n_lost_dR", lookup("n_seeds"));
  436. auto& n_split_dR = *tds.register_container<ContainerTH1<int>>("n_split_dR", lookup("n_seeds"));
  437. auto& n_faked_dR = *tds.register_container<ContainerTH1<int>>("n_faked_dR", lookup("n_seeds"));
  438. auto& n_flipped_dR = *tds.register_container<ContainerTH1<int>>("n_flipped_dR", lookup("n_seeds"));
  439. auto& matched_dR_dR = *tds.register_container<ContainerTH1<float>>("matched_dR_dR", lookup("matched_dR"));
  440. auto& matched_dpT_dR = *tds.register_container<ContainerTH1<float>>("matched_dpT_dR", lookup("matched_dpT"));
  441. auto& tm_corr = *tds.register_container<ContainerTH2<int>>("tm_corr", THParams(2, -0.5, 1.5, 2, -0.5, 1.5));
  442. auto& n_pileup = *tds.register_container<ContainerTH1<int>>("n_pileup", lookup("n_seeds"));
  443. auto& n_seeds_v_PU = *tds.register_container<ContainerTH2<int>>("n_seeds_v_PU", lookup("n_seeds_v_PU"));
  444. auto& n_good_seeds_v_PU = *tds.register_container<ContainerTH2<int>>("n_good_seeds_v_PU", lookup("n_seeds_v_PU"));
  445. auto& n_tracks_v_PU = *tds.register_container<ContainerTH2<int>>("n_tracks_v_PU", lookup("n_seeds_v_PU"));
  446. auto& n_pv_v_PU = *tds.register_container<ContainerTH2<int>>("n_pv_v_PU", lookup("n_seeds_v_PU"));
  447. auto& n_seeds_v_tPU = *tds.register_container<ContainerTH2<float>>("n_seeds_v_tPU", lookup("n_seeds_v_PU"));
  448. auto& n_good_seeds_v_tPU = *tds.register_container<ContainerTH2<float>>("n_good_seeds_v_tPU", lookup("n_seeds_v_PU"));
  449. auto& n_tracks_v_tPU = *tds.register_container<ContainerTH2<float>>("n_tracks_v_tPU", lookup("n_seeds_v_PU"));
  450. auto& n_pv_v_tPU = *tds.register_container<ContainerTH2<float>>("n_pv_v_tPU", lookup("n_seeds_v_PU"));
  451. auto& n_seeds_v_n_scl = *tds.register_container<ContainerTH2<int>>("n_seeds_v_n_scl", lookup("n_seeds_v_PU"));
  452. auto& n_good_seeds_v_n_scl = *tds.register_container<ContainerTH2<int>>("n_good_seeds_v_n_scl", lookup("n_seeds_v_PU"));
  453. auto& n_good_seeds_v_n_good_scl = *tds.register_container<ContainerTH2<int>>("n_good_seeds_v_n_good_scl", lookup("n_seeds_v_PU"));
  454. auto& n_initseeds_v_PU = *tds.register_container<ContainerTH2<int>>("n_initseeds_v_PU", lookup("n_initseeds_v_PU"));
  455. auto& n_initseeds_v_tPU = *tds.register_container<ContainerTH2<float>>("n_initseeds_v_tPU", lookup("n_initseeds_v_PU"));
  456. while (tds.next()) {
  457. update_sim_els();
  458. vector<SimTrack> prompt_sims, nonprompt_sims;
  459. std::tie(prompt_sims, nonprompt_sims) = get_prompt_sims();
  460. int n_pv = get_n_pv();
  461. n_pileup.fill(n_pv);
  462. n_pv_v_PU.fill(n_pv, inTimePileup->get());
  463. n_pv_v_tPU.fill(n_pv, truePileup->get());
  464. size_t _n_good_seeds = 0;
  465. /* cout << "Event: " << tds.get_current_event() << endl; */
  466. int idx = 0;
  467. for (const auto& seed : seeds) {
  468. /* cout << idx << ") [" << seed.pt() << ", " << seed.eta() << ", " << seed.phi() << "] " << seed.sclIdx() << " "; */
  469. if (is_good_seed(seed, hoe_cut)) {
  470. _n_good_seeds++;
  471. seed_kinems.pt->fill(seed.pt());
  472. seed_kinems.eta->fill(seed.eta());
  473. seed_kinems.phi->fill(seed.phi());
  474. seed_kinems.eta_phi->fill(seed.eta(), seed.phi());
  475. }
  476. idx++;
  477. }
  478. size_t _n_good_scl = 0;
  479. for (const auto& scl : scls) {
  480. if (scl.hoe() < hoe_cut) {
  481. _n_good_scl++;
  482. scl_kinems.pt->fill(hypot(scl.px(), scl.py()));
  483. float eta = pseudorapidity(scl.x(), scl.y(), scl.z());
  484. float phi = atan2(scl.y(), scl.x());
  485. scl_kinems.eta->fill(eta);
  486. scl_kinems.phi->fill(phi);
  487. scl_kinems.eta_phi->fill(eta, phi);
  488. }
  489. }
  490. /* idx = 0; */
  491. /* for (const auto& track : gsf_tracks) { */
  492. /* cout << idx << ") [" << track.pt() << ", " << track.eta() << ", " << track.phi() << "] " << track.seedIdx() << endl; */
  493. /* idx++; */
  494. /* } */
  495. /* string tmp; */
  496. /* cin >> tmp; */
  497. /* cout << "total seeds: " << totalSeeds->get() << endl; */
  498. n_initseeds_v_PU.fill(totalSeeds->get(), inTimePileup->get());
  499. n_initseeds_v_tPU.fill(totalSeeds->get(), truePileup->get());
  500. n_seeds_v_n_scl.fill(seeds.size(), scls.size());
  501. n_good_seeds_v_n_scl.fill(_n_good_seeds, scls.size());
  502. n_good_seeds_v_n_good_scl.fill(_n_good_seeds, _n_good_scl);
  503. n_seeds.fill(seeds.size());
  504. n_good_seeds.fill(_n_good_seeds);
  505. n_seeds_v_PU.fill(seeds.size(), inTimePileup->get());
  506. n_good_seeds_v_PU.fill(_n_good_seeds, inTimePileup->get());
  507. n_seeds_v_tPU.fill(seeds.size(), truePileup->get());
  508. n_good_seeds_v_tPU.fill(_n_good_seeds, truePileup->get());
  509. for (const auto& sim_track : sim_els) {
  510. good_sim_kinems.pt->fill(sim_track.pt());
  511. good_sim_kinems.eta->fill(sim_track.eta());
  512. good_sim_kinems.phi->fill(sim_track.phi());
  513. good_sim_kinems.eta_phi->fill(sim_track.eta(), sim_track.phi());
  514. }
  515. n_prompt.fill(prompt_sims.size());
  516. for (const auto& prompt : prompt_sims) {
  517. prompt_kinems.pt->fill(prompt.pt());
  518. prompt_kinems.eta->fill(prompt.eta());
  519. prompt_kinems.phi->fill(prompt.phi());
  520. prompt_kinems.eta_phi->fill(prompt.eta(), prompt.phi());
  521. }
  522. n_nonprompt.fill(nonprompt_sims.size());
  523. for (const auto& nonprompt : nonprompt_sims) {
  524. nonprompt_kinems.pt->fill(nonprompt.pt());
  525. nonprompt_kinems.eta->fill(nonprompt.eta());
  526. nonprompt_kinems.phi->fill(nonprompt.phi());
  527. nonprompt_kinems.eta_phi->fill(nonprompt.eta(), nonprompt.phi());
  528. }
  529. for (const auto& gsf_track : gsf_tracks) {
  530. if (!is_good_seed(seeds[gsf_track.seedIdx()], hoe_cut)) continue;
  531. gsf_kinems.pt->fill(gsf_track.pt());
  532. gsf_kinems.eta->fill(gsf_track.eta());
  533. gsf_kinems.phi->fill(gsf_track.phi());
  534. gsf_kinems.eta_phi->fill(gsf_track.eta(), gsf_track.phi());
  535. if (gsf_track.pt() > 10) {
  536. gsf_high_pt1_kinems.pt->fill(gsf_track.pt());
  537. gsf_high_pt1_kinems.eta->fill(gsf_track.eta());
  538. gsf_high_pt1_kinems.phi->fill(gsf_track.phi());
  539. gsf_high_pt1_kinems.eta_phi->fill(gsf_track.eta(), gsf_track.phi());
  540. }
  541. if (gsf_track.pt() > 20) {
  542. gsf_high_pt2_kinems.pt->fill(gsf_track.pt());
  543. gsf_high_pt2_kinems.eta->fill(gsf_track.eta());
  544. gsf_high_pt2_kinems.phi->fill(gsf_track.phi());
  545. gsf_high_pt2_kinems.eta_phi->fill(gsf_track.eta(), gsf_track.phi());
  546. }
  547. }
  548. n_tracks_v_PU.fill(gsf_tracks.size(), inTimePileup->get());
  549. n_tracks_v_tPU.fill(gsf_tracks.size(), truePileup->get());
  550. n_scl.fill(scls.size());
  551. n_good_scl.fill(_n_good_scl);
  552. auto match_stats = get_match_stats(false);
  553. n_good_sim.fill(std::get<0>(match_stats));
  554. n_gsf_tracks.fill(std::get<1>(match_stats));
  555. n_matched.fill(std::get<2>(match_stats));
  556. n_merged.fill(std::get<3>(match_stats));
  557. n_lost.fill(std::get<4>(match_stats));
  558. n_split.fill(std::get<5>(match_stats));
  559. n_faked.fill(std::get<6>(match_stats));
  560. n_flipped.fill(std::get<7>(match_stats));
  561. for(float dR : std::get<8>(match_stats)) matched_dR.fill(dR);
  562. for(float dpT : std::get<9>(match_stats)) matched_dpT.fill(dpT);
  563. match_stats = get_match_stats(true);
  564. n_matched_dR.fill(std::get<2>(match_stats));
  565. n_merged_dR.fill(std::get<3>(match_stats));
  566. n_lost_dR.fill(std::get<4>(match_stats));
  567. n_split_dR.fill(std::get<5>(match_stats));
  568. n_faked_dR.fill(std::get<6>(match_stats));
  569. n_flipped_dR.fill(std::get<7>(match_stats));
  570. for(float dR : std::get<8>(match_stats)) matched_dR_dR.fill(dR);
  571. for(float dpT : std::get<9>(match_stats)) matched_dpT_dR.fill(dpT);
  572. for (const auto& sim_track : sim_els) {
  573. if (seeds.size() == 0) continue;
  574. for (const auto &seed_idx : sim_track.seedIdx()) {
  575. const Seed& seed = seeds[seed_idx];
  576. if (!is_good_seed(seed, hoe_cut)) continue;
  577. ecal_energy_resolution.fill(reco_energy_rel_err(sim_track, seed));
  578. }
  579. }
  580. // Seeding Efficiency
  581. for (const auto& sim_track : sim_els) {
  582. if (seeds.size() == 0) continue;
  583. bool matched = false;
  584. for (const auto& seed_idx : sim_track.seedIdx()) {
  585. const Seed& seed = seeds[seed_idx];
  586. if (is_good_seed(seed, hoe_cut)) {
  587. matched=true;
  588. break;
  589. }
  590. }
  591. if (abs(sim_track.eta()) < 2.5)
  592. seed_eff.pt->fill(sim_track.pt(), matched);
  593. if (sim_track.pt() > 20.0)
  594. seed_eff.eta->fill(sim_track.eta(), matched);
  595. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20)
  596. seed_eff.phi->fill(sim_track.phi(), matched);
  597. }
  598. // Tracking Efficiency
  599. for (const auto& sim_track : sim_els) {
  600. if (gsf_tracks.size() == 0) continue;
  601. bool matched = false;
  602. bool matched_any = false;
  603. for (const auto& track_idx : sim_track.trkIdx()) {
  604. const Seed& seed = seeds[gsf_tracks[track_idx].seedIdx()];
  605. if (is_good_seed(seed, hoe_cut)) {
  606. matched = true;
  607. matched_any = true;
  608. } else if (!seed.isECALDriven()) {
  609. matched_any = true;
  610. }
  611. }
  612. if (abs(sim_track.eta()) < 2.5) {
  613. tracking_eff.pt->fill(sim_track.pt(), matched);
  614. tracking_eff_full.pt->fill(sim_track.pt(), matched_any);
  615. }
  616. if (sim_track.pt() > 20.0) {
  617. tracking_eff.eta->fill(sim_track.eta(), matched);
  618. tracking_eff_full.eta->fill(sim_track.eta(), matched_any);
  619. }
  620. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20) {
  621. tracking_eff.phi->fill(sim_track.phi(), matched);
  622. tracking_eff_full.phi->fill(sim_track.phi(), matched_any);
  623. }
  624. if (std::find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end()) {
  625. if (abs(sim_track.eta()) < 2.5)
  626. prompt_eff.pt->fill(sim_track.pt(), matched);
  627. if (sim_track.pt() > 20.0)
  628. prompt_eff.eta->fill(sim_track.eta(), matched);
  629. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20)
  630. prompt_eff.phi->fill(sim_track.phi(), matched);
  631. }
  632. if (std::find(nonprompt_sims.begin(), nonprompt_sims.end(), sim_track) != nonprompt_sims.end()) {
  633. if (abs(sim_track.eta()) < 2.5)
  634. nonprompt_eff.pt->fill(sim_track.pt(), matched);
  635. if (sim_track.pt() > 20.0)
  636. nonprompt_eff.eta->fill(sim_track.eta(), matched);
  637. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20)
  638. nonprompt_eff.phi->fill(sim_track.phi(), matched);
  639. }
  640. // dR-matching
  641. matched = false;
  642. matched_any = false;
  643. for (const auto& gsf_track : gsf_tracks) {
  644. const Seed& seed = seeds[gsf_track.seedIdx()];
  645. double dR = deltaR(sim_track.eta(), sim_track.phi(), gsf_track.eta(), gsf_track.phi());
  646. if (is_good_seed(seed, hoe_cut) and dR < 0.2) {
  647. matched = true;
  648. matched_any = true;
  649. } else if (!seed.isECALDriven()) {
  650. matched_any = true;
  651. }
  652. }
  653. if (abs(sim_track.eta()) < 2.5) {
  654. tracking_eff_dR.pt->fill(sim_track.pt(), matched);
  655. tracking_eff_full_dR.pt->fill(sim_track.pt(), matched_any);
  656. }
  657. if (sim_track.pt() > 20.0) {
  658. tracking_eff_dR.eta->fill(sim_track.eta(), matched);
  659. tracking_eff_full_dR.eta->fill(sim_track.eta(), matched_any);
  660. }
  661. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20) {
  662. tracking_eff_dR.phi->fill(sim_track.phi(), matched);
  663. tracking_eff_full_dR.phi->fill(sim_track.phi(), matched_any);
  664. tracking_eff_v_PU_dR.fill(inTimePileup->get(), matched);
  665. }
  666. tracking_eff_v_PU_dR_inc.fill(inTimePileup->get(), matched);
  667. if (find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end()) {
  668. if (abs(sim_track.eta()) < 2.5)
  669. prompt_eff_dR.pt->fill(sim_track.pt(), matched);
  670. if (sim_track.pt() > 20.0)
  671. prompt_eff_dR.eta->fill(sim_track.eta(), matched);
  672. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20)
  673. prompt_eff_dR.phi->fill(sim_track.phi(), matched);
  674. }
  675. if (find(nonprompt_sims.begin(), nonprompt_sims.end(), sim_track) != nonprompt_sims.end()) {
  676. if (abs(sim_track.eta()) < 2.5)
  677. nonprompt_eff_dR.pt->fill(sim_track.pt(), matched);
  678. if (sim_track.pt() > 20.0)
  679. nonprompt_eff_dR.eta->fill(sim_track.eta(), matched);
  680. if (abs(sim_track.eta()) < 2.5 and sim_track.pt() > 20)
  681. nonprompt_eff_dR.phi->fill(sim_track.phi(), matched);
  682. }
  683. }
  684. // Seeding Purity
  685. for (const auto& seed : seeds) {
  686. if (!is_good_seed(seed, hoe_cut)) continue;
  687. bool match = false;
  688. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  689. if (is_good_sim(sim_els, sim_tracks[sim_track_idx])) {
  690. match = true;
  691. break;
  692. }
  693. }
  694. if (abs(seed.eta()) < 2.5)
  695. seed_pur.pt->fill(seed.pt(), match);
  696. if (seed.pt() > 20)
  697. seed_pur.eta->fill(seed.eta(), match);
  698. if (abs(seed.eta()) < 2.5 and seed.pt() > 20)
  699. seed_pur.phi->fill(seed.phi(), match);
  700. }
  701. // Tracking Purity
  702. for (const auto& gsf_track : gsf_tracks) {
  703. gsf_tracks_nmatch_sim_tracks.fill(gsf_track.simTrkIdx().size());
  704. const Seed& seed = seeds[gsf_track.seedIdx()];
  705. /* if (!is_good_seed(seed, hoe_cut)) continue; */
  706. bool good_ecal_driven_seed = is_good_seed(seed, hoe_cut);
  707. bool good_seed = good_ecal_driven_seed or !seed.isECALDriven();
  708. bool matched = false;
  709. bool prompt_matched = false;
  710. bool nonprompt_matched = false;
  711. for (const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  712. const auto& sim_track = sim_tracks[sim_track_idx];
  713. if (!is_good_sim(sim_els, sim_track)) continue;
  714. matched = true;
  715. if (find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end()) {
  716. prompt_matched = true;
  717. }
  718. if (find(nonprompt_sims.begin(), nonprompt_sims.end(), sim_track) != nonprompt_sims.end()) {
  719. nonprompt_matched = true;
  720. }
  721. }
  722. if (abs(gsf_track.eta()) < 2.5) {
  723. if (good_ecal_driven_seed) tracking_pur.pt->fill(gsf_track.pt(), matched);
  724. if (good_seed) tracking_pur_full.pt->fill(gsf_track.pt(), matched);
  725. }
  726. if (gsf_track.pt() > 20) {
  727. if (good_ecal_driven_seed) tracking_pur.eta->fill(gsf_track.eta(), matched);
  728. if (good_seed) tracking_pur_full.eta->fill(gsf_track.eta(), matched);
  729. }
  730. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20) {
  731. if (good_ecal_driven_seed) tracking_pur.phi->fill(gsf_track.phi(), matched);
  732. if (good_seed) tracking_pur_full.phi->fill(gsf_track.phi(), matched);
  733. }
  734. if (abs(gsf_track.eta()) < 2.5)
  735. if (good_ecal_driven_seed) prompt_pur.pt->fill(gsf_track.pt(), prompt_matched);
  736. if (gsf_track.pt() > 20)
  737. if (good_ecal_driven_seed) prompt_pur.eta->fill(gsf_track.eta(), prompt_matched);
  738. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20)
  739. if (good_ecal_driven_seed) prompt_pur.phi->fill(gsf_track.phi(), prompt_matched);
  740. if (abs(gsf_track.eta()) < 2.5)
  741. if (good_ecal_driven_seed) nonprompt_pur.pt->fill(gsf_track.pt(), nonprompt_matched);
  742. if (gsf_track.pt() > 20)
  743. if (good_ecal_driven_seed) nonprompt_pur.eta->fill(gsf_track.eta(), nonprompt_matched);
  744. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20)
  745. if (good_ecal_driven_seed) nonprompt_pur.phi->fill(gsf_track.phi(), nonprompt_matched);
  746. // dR-matching
  747. matched = false;
  748. prompt_matched = false;
  749. nonprompt_matched = false;
  750. for (const auto& sim_track : sim_els) {
  751. double dR = deltaR(sim_track.eta(), sim_track.phi(), gsf_track.eta(), gsf_track.phi());
  752. if (dR < 0.2) {
  753. matched = true;
  754. if (find(prompt_sims.begin(), prompt_sims.end(), sim_track) != prompt_sims.end()) {
  755. prompt_matched = true;
  756. }
  757. if (find(nonprompt_sims.begin(), nonprompt_sims.end(), sim_track) != nonprompt_sims.end()) {
  758. nonprompt_matched = true;
  759. }
  760. }
  761. }
  762. if (abs(gsf_track.eta()) < 2.5) {
  763. if (good_ecal_driven_seed) tracking_pur_dR.pt->fill(gsf_track.pt(), matched);
  764. if (good_seed) tracking_pur_full_dR.pt->fill(gsf_track.pt(), matched);
  765. }
  766. if (gsf_track.pt() > 20.0) {
  767. if (good_ecal_driven_seed) tracking_pur_dR.eta->fill(gsf_track.eta(), matched);
  768. if (good_seed) tracking_pur_full_dR.eta->fill(gsf_track.eta(), matched);
  769. }
  770. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20) {
  771. if (good_ecal_driven_seed) tracking_pur_dR.phi->fill(gsf_track.phi(), matched);
  772. if (good_seed) tracking_pur_full_dR.phi->fill(gsf_track.phi(), matched);
  773. if (good_ecal_driven_seed) tracking_pur_v_PU_dR.fill(inTimePileup->get(), matched);
  774. }
  775. tracking_pur_v_PU_dR_inc.fill(inTimePileup->get(), matched);
  776. if (abs(gsf_track.eta()) < 2.5)
  777. if (good_ecal_driven_seed) prompt_pur_dR.pt->fill(gsf_track.pt(), prompt_matched);
  778. if (gsf_track.pt() > 20)
  779. if (good_ecal_driven_seed) prompt_pur_dR.eta->fill(gsf_track.eta(), prompt_matched);
  780. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20)
  781. if (good_ecal_driven_seed) prompt_pur_dR.phi->fill(gsf_track.phi(), prompt_matched);
  782. if (abs(gsf_track.eta()) < 2.5)
  783. if (good_ecal_driven_seed) nonprompt_pur_dR.pt->fill(gsf_track.pt(), nonprompt_matched);
  784. if (gsf_track.pt() > 20)
  785. if (good_ecal_driven_seed) nonprompt_pur_dR.eta->fill(gsf_track.eta(), nonprompt_matched);
  786. if (abs(gsf_track.eta()) < 2.5 and gsf_track.pt() > 20)
  787. if (good_ecal_driven_seed) nonprompt_pur_dR.phi->fill(gsf_track.phi(), nonprompt_matched);
  788. }
  789. // Fake-Rate
  790. std::map<size_t, std::vector<int>> scl2trk; // Maps superclusters to tm-status of matched gsf-tracks
  791. for (const auto &gsf_track : gsf_tracks) {
  792. bool gsf_truth_matched = false;
  793. for(const auto& sim_track_idx : gsf_track.simTrkIdx()) {
  794. if (is_good_sim(sim_els, sim_tracks[sim_track_idx])) {
  795. gsf_truth_matched = true;
  796. break;
  797. }
  798. }
  799. int scl_idx = seeds[gsf_track.seedIdx()].sclIdx();
  800. scl2trk[scl_idx].push_back(gsf_truth_matched);
  801. }
  802. std::set<size_t> tm_scls; // Set of super-clusters with well matched sim-electrons
  803. std::set<size_t> tm_scls_no_e_match; // Set of super-clusters with well matched sim-electrons w/o energy matching
  804. for (const auto &scl : scls) {
  805. Vec4 p4 = scl_p4(scl);
  806. for(const auto& sim_track : sim_els) {
  807. if (deltaR(p4.eta(), p4.phi(), sim_track.eta(), sim_track.phi()) > 0.2) continue;
  808. tm_scls_no_e_match.insert(scl.idx);
  809. if (((p4.Et() - sim_track.pt()) / p4.Et()) > 0.1) continue;
  810. tm_scls.insert(scl.idx);
  811. break;
  812. }
  813. }
  814. /* cout << "scls: "; */
  815. /* for (const auto& scl: scls) cout << scl.idx << ", "; */
  816. /* cout << endl << "tm_scls: "; */
  817. /* for (const auto& idx: tm_scls) cout << idx << ", "; */
  818. /* cout << endl; */
  819. for (const auto &scl : scls) {
  820. if (scl.hoe() > hoe_cut) continue;
  821. float scl_pt = hypot(scl.px(), scl.py());
  822. float scl_eta = pseudorapidity(scl.x(), scl.y(), scl.z());
  823. float scl_phi = atan2(scl.y(), scl.x());
  824. float rad = hypot(scl.x(), scl.y());
  825. /* cout << "weird phi: " << scl_phi << " (" << scl.x() << ", " << scl.y() << ") - " << rad << "\n"; */
  826. /* if (scl_phi > 3.141592653589793238 or scl_phi < -3.141592653589793238) { */
  827. /* cout << "weird phi: " << scl_phi << " (" << scl.x() << ", " << scl.y() << ")\n"; */
  828. /* } */
  829. int ntracks = scl2trk[scl.idx].size();
  830. int ntmtracks = std::accumulate(scl2trk[scl.idx].begin(), scl2trk[scl.idx].end(), 0);
  831. bool partial_fake = ntmtracks > 0 and ntracks > ntmtracks;
  832. bool full_fake = ntmtracks == 0 and ntracks > 0;
  833. /* cout << "ntracks: " << ntracks << " "; */
  834. /* cout << "count: " << tm_scls.count(scl.idx); */
  835. if (ntracks > 0) {
  836. partial_fake_rate_incl.pt->fill(scl_pt, partial_fake);
  837. partial_fake_rate_incl.eta->fill(scl_eta, partial_fake);
  838. partial_fake_rate_incl.phi->fill(scl_phi, partial_fake);
  839. if (abs(scl_eta) < 2.5) partial_fake_rate.pt->fill(scl_pt, partial_fake);
  840. if (scl_pt > 20.0) partial_fake_rate.eta->fill(scl_eta, partial_fake);
  841. if (abs(scl_eta) < 2.5 and scl_pt > 20) partial_fake_rate.phi->fill(scl_phi, partial_fake);
  842. full_fake_rate_incl.pt->fill(scl_pt, full_fake);
  843. full_fake_rate_incl.eta->fill(scl_eta, full_fake);
  844. full_fake_rate_incl.phi->fill(scl_phi, full_fake);
  845. if (abs(scl_eta) < 2.5) full_fake_rate.pt->fill(scl_pt, full_fake);
  846. if (scl_pt > 20.0) full_fake_rate.eta->fill(scl_eta, full_fake);
  847. if (abs(scl_eta) < 2.5 and scl_pt > 20) full_fake_rate.phi->fill(scl_phi, full_fake);
  848. if (tm_scls.count(scl.idx) == 0) {
  849. clean_fake_rate_incl.pt->fill(scl_pt, full_fake);
  850. clean_fake_rate_incl.eta->fill(scl_eta, full_fake);
  851. clean_fake_rate_incl.phi->fill(scl_phi, full_fake);
  852. if (abs(scl_eta) < 2.5) clean_fake_rate.pt->fill(scl_pt, full_fake);
  853. if (scl_pt > 20.0) clean_fake_rate.eta->fill(scl_eta, full_fake);
  854. if (abs(scl_eta) < 2.5 and scl_pt > 20) clean_fake_rate.phi->fill(scl_phi, full_fake);
  855. }
  856. }
  857. if (tm_scls.count(scl.idx) == 0) {
  858. fake_rate_incl.pt->fill(scl_pt, ntracks>0);
  859. fake_rate_incl.eta->fill(scl_eta, ntracks>0);
  860. fake_rate_incl.phi->fill(scl_phi, ntracks>0);
  861. if (abs(scl_eta) < 2.5) fake_rate.pt->fill(scl_pt, ntracks>0);
  862. if (scl_pt > 20.0) fake_rate.eta->fill(scl_eta, ntracks>0);
  863. if (abs(scl_eta) < 2.5 and scl_pt > 20) fake_rate.phi->fill(scl_phi, ntracks>0);
  864. }
  865. if (tm_scls_no_e_match.count(scl.idx) == 0) {
  866. fake_rate_no_e_match_incl.pt->fill(scl_pt, ntracks>0);
  867. fake_rate_no_e_match_incl.eta->fill(scl_eta, ntracks>0);
  868. fake_rate_no_e_match_incl.phi->fill(scl_phi, ntracks>0);
  869. if (abs(scl_eta) < 2.5) fake_rate_no_e_match.pt->fill(scl_pt, ntracks>0);
  870. if (scl_pt > 20.0) fake_rate_no_e_match.eta->fill(scl_eta, ntracks>0);
  871. if (abs(scl_eta) < 2.5 and scl_pt > 20) {
  872. fake_rate_no_e_match.phi->fill(scl_phi, ntracks>0);
  873. fake_rate_no_e_match_v_PU.fill(inTimePileup->get(), ntracks>0);
  874. }
  875. fake_rate_no_e_match_v_PU_inc.fill(inTimePileup->get(), ntracks>0);
  876. }
  877. /* cout << endl; */
  878. }
  879. // Hit Residuals
  880. for (const auto& seed : seeds) {
  881. if (seed.trkIdx() < 0) continue; // require that seed produced gsf-track
  882. if (!is_good_seed(seed, hoe_cut)) continue;
  883. bool is_seed_sim_matched = false;
  884. for (const auto& sim_track_idx : seed.simTrkIdx()) {
  885. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  886. if (is_good_sim(sim_els, sim_track) and reco_energy_consistent(sim_track, seed)) {
  887. is_seed_sim_matched = true;
  888. break;
  889. }
  890. }
  891. bool is_track_sim_matched = false;
  892. const auto the_track = gsf_tracks[seed.trkIdx()];
  893. for (const auto& sim_track_idx : the_track.simTrkIdx()) {
  894. const SimTrack& sim_track = sim_tracks[sim_track_idx];
  895. if (is_good_sim(sim_els, sim_track) and reco_energy_consistent(sim_track, the_track)) {
  896. is_track_sim_matched = true;
  897. break;
  898. }
  899. }
  900. std::vector<TMType> tm_types;
  901. if (is_seed_sim_matched and is_track_sim_matched) {
  902. tm_types = {SeedAndTrackMatched, SeedMatched, TrackMatched};
  903. } else if (is_seed_sim_matched) {
  904. tm_types = {SeedMatched};
  905. } else if (is_track_sim_matched) {
  906. tm_types = {TrackMatched};
  907. } else {
  908. tm_types = {NoMatch};
  909. }
  910. tm_corr.fill(is_seed_sim_matched, is_track_sim_matched);
  911. vector<int> hits_valid;
  912. vector<float> hits_dRz;
  913. vector<float> hits_dPhi;
  914. if (the_track.q() == 1) {
  915. hits_valid = seed.isValidPos();
  916. hits_dRz = seed.dRZPos();
  917. hits_dPhi = seed.dPhiPos();
  918. } else {
  919. hits_valid = seed.isValidNeg();
  920. hits_dRz = seed.dRZNeg();
  921. hits_dPhi = seed.dPhiNeg();
  922. }
  923. const vector<int>& hits_isBarrel = seed.isBarrel();
  924. const vector<int>& hits_layerOrDiskNr = seed.layerOrDiskNr();
  925. for (const auto& tm_type : tm_types) {
  926. size_t nHits = hits_valid.size();
  927. for (size_t hit_idx = 0; hit_idx < nHits; hit_idx++) {
  928. if (!hits_valid[hit_idx]) continue;
  929. bool isBarrel = hits_isBarrel[hit_idx] == 1;
  930. int layerOrDiskNr = hits_layerOrDiskNr[hit_idx];
  931. float dRz = abs(hits_dRz[hit_idx]);
  932. float dPhi = abs(hits_dPhi[hit_idx]);
  933. int eta_region = get_region(static_cast<int>(hit_idx + 1), seed.eta());
  934. if (is_seed_sim_matched) {
  935. dRz_residuals_v_eta[hit_idx + 1]->fill(dRz, abs(seed.eta()));
  936. dPhi_residuals_v_eta[hit_idx + 1]->fill(dPhi, abs(seed.eta()));
  937. dRz_residuals_v_region[make_pair(hit_idx + 1, eta_region)]->fill(dRz, seed.Et());
  938. dPhi_residuals_v_region[make_pair(hit_idx + 1, eta_region)]->fill(dPhi, seed.Et());
  939. }
  940. residuals_dRz[make_tuple(hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  941. residuals_dPhi[make_tuple(hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  942. if (layerOrDiskNr == -1) continue; // subDet is not set w/ old seeding
  943. if (isBarrel)
  944. hit_vs_layer_barrel.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  945. else
  946. hit_vs_layer_forward.fill(layerOrDiskNr, static_cast<const int &>(hit_idx + 1));
  947. if (isBarrel) {
  948. BPIX_residuals_dRz[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  949. BPIX_residuals_dPhi[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  950. } else {
  951. FPIX_residuals_dRz[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dRz, seed.Et());
  952. FPIX_residuals_dPhi[make_tuple(layerOrDiskNr, hit_idx + 1, tm_type)]->fill(dPhi, seed.Et());
  953. }
  954. }
  955. }
  956. }
  957. }
  958. tds.save_all();
  959. }
  960. int main(int argc, char * argv[]){
  961. using namespace fv_util;
  962. ArgParser args(argc, argv);
  963. if(args.cmd_option_exists("-c")) {
  964. init_config(args.get_cmd_option("-c"));
  965. args.update_config();
  966. if (args.cmd_option_exists("-s")) {
  967. the_config->update_key("silent", true);
  968. }
  969. if (args.cmd_option_exists("-b")) {
  970. the_config->update_key("batch", true);
  971. }
  972. init_log(LogPriority::kLogInfo);
  973. // gSystem->Load("libfilval.so");
  974. run();
  975. } else {
  976. cout << "Usage: ./" << argv[0] << " (-s) (-b) -c config_file.yaml" << endl;
  977. }
  978. return 0;
  979. }