CMS 3D CMS Logo

WriteMemoryFile.cc
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
3 
4 #include <iostream>
5 #include <list>
6 #include <unordered_map>
11 
12 using namespace mkfit;
13 
15 
16 constexpr bool useMatched = false;
17 
18 constexpr int cleanSimTrack_minSimHits = 3;
19 constexpr int cleanSimTrack_minRecHits = 2;
20 
21 //check if this is the same as in the release
22 enum class HitType { Pixel = 0, Strip = 1, Glued = 2, Invalid = 3, Phase2OT = 4, Unknown = 99 };
23 
24 typedef std::list<std::string> lStr_t;
25 typedef lStr_t::iterator lStr_i;
27  lStr_i j = i;
28  if (++j == args.end() || ((*j)[0] == '-')) {
29  std::cerr << "Error: option " << *i << " requires an argument.\n";
30  exit(1);
31  }
32  i = j;
33 }
34 
36  lStr_i j = i;
37  if (++j == args.end() || ((*j)[0] == '-')) {
38  return false;
39  }
40  i = j;
41  return true;
42 }
43 
44 void printHelp(const char* av0) {
45  printf(
46  "Usage: %s [options]\n"
47  "Options:\n"
48  " --input <str> input file\n"
49  " --output <str> output file\n"
50  " --geo <file> binary TrackerInfo geometry (def: CMS-2017.bin)\n"
51  " --verbosity <num> print details (0 quiet, 1 print counts, 2 print all; def: 0)\n"
52  " --maxevt <num> maxevt events to write (-1 for everything in the file def: -1)\n"
53  " --clean-sim-tracks apply sim track cleaning (def: no cleaning)\n"
54  " --write-all-events write all events (def: skip events with 0 simtracks or seeds)\n"
55  " --write-rec-tracks write rec tracks (def: not written)\n"
56  " --apply-ccc apply cluster charge cut to strip hits (def: false)\n"
57  " --all-seeds merge all seeds from the input file (def: false)\n",
58  av0);
59 }
60 
61 int main(int argc, char* argv[]) {
62  bool haveInput = false;
64  bool haveOutput = false;
66  std::string geoFileName("CMS-2017.bin");
67 
68  bool cleanSimTracks = false;
69  bool writeAllEvents = false;
70  bool writeRecTracks = false;
71  bool writeHitIterMasks = false;
72  bool applyCCC = false;
73  bool allSeeds = false;
74 
75  int verbosity = 0;
76  long long maxevt = -1;
77 
78  int cutValueCCC = 1620; //Nominal value (from first iteration of CMSSW) is 1620
79 
80  lStr_t mArgs;
81  for (int i = 1; i < argc; ++i) {
82  mArgs.push_back(argv[i]);
83  }
84 
85  lStr_i i = mArgs.begin();
86  while (i != mArgs.end()) {
87  lStr_i start = i;
88 
89  if (*i == "-h" || *i == "-help" || *i == "--help") {
90  printHelp(argv[0]);
91  } else if (*i == "--input") {
92  next_arg_or_die(mArgs, i);
93  inputFileName = *i;
94  haveInput = true;
95  } else if (*i == "--output") {
96  next_arg_or_die(mArgs, i);
97  outputFileName = *i;
98  haveOutput = true;
99  } else if (*i == "--geo") {
100  next_arg_or_die(mArgs, i);
101  geoFileName = *i;
102  } else if (*i == "--verbosity") {
103  next_arg_or_die(mArgs, i);
104  verbosity = std::atoi(i->c_str());
105  } else if (*i == "--maxevt") {
106  next_arg_or_die(mArgs, i);
107  maxevt = std::atoi(i->c_str());
108  } else if (*i == "--clean-sim-tracks") {
109  cleanSimTracks = true;
110  } else if (*i == "--write-all-events") {
111  writeAllEvents = true;
112  } else if (*i == "--write-rec-tracks") {
113  writeRecTracks = true;
114  } else if (*i == "--write-hit-iter-masks") {
115  writeHitIterMasks = true;
116  } else if (*i == "--apply-ccc") {
117  applyCCC = true;
118  if (next_arg_option(mArgs, i)) {
119  cutValueCCC = std::atoi(i->c_str());
120  }
121  } else if (*i == "--all-seeds") {
122  allSeeds = true;
123  } else {
124  fprintf(stderr, "Error: Unknown option/argument '%s'.\n", i->c_str());
125  printHelp(argv[0]);
126  exit(1);
127  }
128  mArgs.erase(start, ++i);
129  } //while arguments
130 
131  if (not haveOutput or not haveInput) {
132  fprintf(stderr, "Error: both input and output are required\n");
133  printHelp(argv[0]);
134  exit(1);
135  }
136 
137  using namespace std;
138 
139  TrackerInfo tkinfo;
140  tkinfo.read_bin_file(geoFileName);
142  const unsigned int nTotalLayers = lnc.nLayers();
143  assert(nTotalLayers == (unsigned int)tkinfo.n_layers());
144 
145  int nstot = 0;
146  std::vector<int> nhitstot(nTotalLayers, 0);
147 
148  TFile* f = TFile::Open(inputFileName.c_str());
149  if (f == 0) {
150  fprintf(stderr, "Failed opening input root file '%s'\n", inputFileName.c_str());
151  exit(1);
152  }
153 
154  TTree* t = (TTree*)f->Get("trackingNtuple/tree");
155 
156  unsigned long long event;
157  t->SetBranchAddress("event", &event);
158 
159  //sim tracks
160  std::vector<float>* sim_eta = 0;
161  std::vector<float>* sim_px = 0;
162  std::vector<float>* sim_py = 0;
163  std::vector<float>* sim_pz = 0;
164  std::vector<int>* sim_parentVtxIdx = 0;
165  std::vector<int>* sim_q = 0;
166  std::vector<int>* sim_event = 0;
167  std::vector<int>* sim_bunchCrossing = 0;
168  std::vector<int>* sim_nValid = 0; //simHit count, actually
169  t->SetBranchAddress("sim_eta", &sim_eta);
170  t->SetBranchAddress("sim_px", &sim_px);
171  t->SetBranchAddress("sim_py", &sim_py);
172  t->SetBranchAddress("sim_pz", &sim_pz);
173  t->SetBranchAddress("sim_parentVtxIdx", &sim_parentVtxIdx);
174  t->SetBranchAddress("sim_q", &sim_q);
175  t->SetBranchAddress("sim_event", &sim_event);
176  t->SetBranchAddress("sim_bunchCrossing", &sim_bunchCrossing);
177  t->SetBranchAddress("sim_nValid", &sim_nValid);
178 
179  std::vector<vector<int>>* sim_trkIdx = 0;
180  t->SetBranchAddress("sim_trkIdx", &sim_trkIdx);
181 
182  //simvtx
183  std::vector<float>* simvtx_x = 0;
184  std::vector<float>* simvtx_y = 0;
185  std::vector<float>* simvtx_z = 0;
186  t->SetBranchAddress("simvtx_x", &simvtx_x);
187  t->SetBranchAddress("simvtx_y", &simvtx_y);
188  t->SetBranchAddress("simvtx_z", &simvtx_z);
189 
190  //simhit
191  std::vector<short>* simhit_process = 0;
192  std::vector<int>* simhit_particle = 0;
193  std::vector<int>* simhit_simTrkIdx = 0;
194  std::vector<float>* simhit_x = 0;
195  std::vector<float>* simhit_y = 0;
196  std::vector<float>* simhit_z = 0;
197  std::vector<float>* simhit_px = 0;
198  std::vector<float>* simhit_py = 0;
199  std::vector<float>* simhit_pz = 0;
200  t->SetBranchAddress("simhit_process", &simhit_process);
201  t->SetBranchAddress("simhit_particle", &simhit_particle);
202  t->SetBranchAddress("simhit_simTrkIdx", &simhit_simTrkIdx);
203  t->SetBranchAddress("simhit_x", &simhit_x);
204  t->SetBranchAddress("simhit_y", &simhit_y);
205  t->SetBranchAddress("simhit_z", &simhit_z);
206  t->SetBranchAddress("simhit_px", &simhit_px);
207  t->SetBranchAddress("simhit_py", &simhit_py);
208  t->SetBranchAddress("simhit_pz", &simhit_pz);
209 
210  std::vector<std::vector<int>>* simhit_hitIdx = 0;
211  t->SetBranchAddress("simhit_hitIdx", &simhit_hitIdx);
212  std::vector<std::vector<int>>* simhit_hitType = 0;
213  t->SetBranchAddress("simhit_hitType", &simhit_hitType);
214 
215  //rec tracks
216  std::vector<int>* trk_q = 0;
217  std::vector<unsigned int>* trk_nValid = 0;
218  std::vector<int>* trk_seedIdx = 0;
219  std::vector<unsigned long long>* trk_algoMask = 0;
220  std::vector<unsigned int>* trk_algo = 0;
221  std::vector<unsigned int>* trk_originalAlgo = 0;
222  std::vector<float>* trk_nChi2 = 0;
223  std::vector<float>* trk_px = 0;
224  std::vector<float>* trk_py = 0;
225  std::vector<float>* trk_pz = 0;
226  std::vector<float>* trk_pt = 0;
227  std::vector<float>* trk_phi = 0;
228  std::vector<float>* trk_lambda = 0;
229  std::vector<float>* trk_refpoint_x = 0;
230  std::vector<float>* trk_refpoint_y = 0;
231  std::vector<float>* trk_refpoint_z = 0;
232  std::vector<float>* trk_dxyErr = 0;
233  std::vector<float>* trk_dzErr = 0;
234  std::vector<float>* trk_ptErr = 0;
235  std::vector<float>* trk_phiErr = 0;
236  std::vector<float>* trk_lambdaErr = 0;
237  t->SetBranchAddress("trk_q", &trk_q);
238  t->SetBranchAddress("trk_nValid", &trk_nValid);
239  t->SetBranchAddress("trk_seedIdx", &trk_seedIdx);
240  t->SetBranchAddress("trk_algoMask", &trk_algoMask);
241  t->SetBranchAddress("trk_algo", &trk_algo);
242  t->SetBranchAddress("trk_originalAlgo", &trk_originalAlgo);
243  t->SetBranchAddress("trk_nChi2", &trk_nChi2);
244  t->SetBranchAddress("trk_px", &trk_px);
245  t->SetBranchAddress("trk_py", &trk_py);
246  t->SetBranchAddress("trk_pz", &trk_pz);
247  t->SetBranchAddress("trk_pt", &trk_pt);
248  t->SetBranchAddress("trk_phi", &trk_phi);
249  t->SetBranchAddress("trk_lambda", &trk_lambda);
250  t->SetBranchAddress("trk_refpoint_x", &trk_refpoint_x);
251  t->SetBranchAddress("trk_refpoint_y", &trk_refpoint_y);
252  t->SetBranchAddress("trk_refpoint_z", &trk_refpoint_z);
253  t->SetBranchAddress("trk_dxyErr", &trk_dxyErr);
254  t->SetBranchAddress("trk_dzErr", &trk_dzErr);
255  t->SetBranchAddress("trk_ptErr", &trk_ptErr);
256  t->SetBranchAddress("trk_phiErr", &trk_phiErr);
257  t->SetBranchAddress("trk_lambdaErr", &trk_lambdaErr);
258 
259  std::vector<std::vector<int>>* trk_hitIdx = 0;
260  t->SetBranchAddress("trk_hitIdx", &trk_hitIdx);
261  std::vector<std::vector<int>>* trk_hitType = 0;
262  t->SetBranchAddress("trk_hitType", &trk_hitType);
263 
264  //seeds
265  std::vector<float>* see_stateTrajGlbX = 0;
266  std::vector<float>* see_stateTrajGlbY = 0;
267  std::vector<float>* see_stateTrajGlbZ = 0;
268  std::vector<float>* see_stateTrajGlbPx = 0;
269  std::vector<float>* see_stateTrajGlbPy = 0;
270  std::vector<float>* see_stateTrajGlbPz = 0;
271  std::vector<float>* see_eta = 0; //PCA parameters
272  std::vector<float>* see_pt = 0; //PCA parameters
273  std::vector<float>* see_stateCcov00 = 0;
274  std::vector<float>* see_stateCcov01 = 0;
275  std::vector<float>* see_stateCcov02 = 0;
276  std::vector<float>* see_stateCcov03 = 0;
277  std::vector<float>* see_stateCcov04 = 0;
278  std::vector<float>* see_stateCcov05 = 0;
279  std::vector<float>* see_stateCcov11 = 0;
280  std::vector<float>* see_stateCcov12 = 0;
281  std::vector<float>* see_stateCcov13 = 0;
282  std::vector<float>* see_stateCcov14 = 0;
283  std::vector<float>* see_stateCcov15 = 0;
284  std::vector<float>* see_stateCcov22 = 0;
285  std::vector<float>* see_stateCcov23 = 0;
286  std::vector<float>* see_stateCcov24 = 0;
287  std::vector<float>* see_stateCcov25 = 0;
288  std::vector<float>* see_stateCcov33 = 0;
289  std::vector<float>* see_stateCcov34 = 0;
290  std::vector<float>* see_stateCcov35 = 0;
291  std::vector<float>* see_stateCcov44 = 0;
292  std::vector<float>* see_stateCcov45 = 0;
293  std::vector<float>* see_stateCcov55 = 0;
294  std::vector<std::vector<float>>* see_stateCurvCov = 0;
295  std::vector<int>* see_q = 0;
296  std::vector<unsigned int>* see_algo = 0;
297  t->SetBranchAddress("see_stateTrajGlbX", &see_stateTrajGlbX);
298  t->SetBranchAddress("see_stateTrajGlbY", &see_stateTrajGlbY);
299  t->SetBranchAddress("see_stateTrajGlbZ", &see_stateTrajGlbZ);
300  t->SetBranchAddress("see_stateTrajGlbPx", &see_stateTrajGlbPx);
301  t->SetBranchAddress("see_stateTrajGlbPy", &see_stateTrajGlbPy);
302  t->SetBranchAddress("see_stateTrajGlbPz", &see_stateTrajGlbPz);
303  t->SetBranchAddress("see_eta", &see_eta);
304  t->SetBranchAddress("see_pt", &see_pt);
305 
306  bool hasCartCov = t->GetBranch("see_stateCcov00") != nullptr;
307  if (hasCartCov) {
308  t->SetBranchAddress("see_stateCcov00", &see_stateCcov00);
309  t->SetBranchAddress("see_stateCcov01", &see_stateCcov01);
310  t->SetBranchAddress("see_stateCcov02", &see_stateCcov02);
311  t->SetBranchAddress("see_stateCcov03", &see_stateCcov03);
312  t->SetBranchAddress("see_stateCcov04", &see_stateCcov04);
313  t->SetBranchAddress("see_stateCcov05", &see_stateCcov05);
314  t->SetBranchAddress("see_stateCcov11", &see_stateCcov11);
315  t->SetBranchAddress("see_stateCcov12", &see_stateCcov12);
316  t->SetBranchAddress("see_stateCcov13", &see_stateCcov13);
317  t->SetBranchAddress("see_stateCcov14", &see_stateCcov14);
318  t->SetBranchAddress("see_stateCcov15", &see_stateCcov15);
319  t->SetBranchAddress("see_stateCcov22", &see_stateCcov22);
320  t->SetBranchAddress("see_stateCcov23", &see_stateCcov23);
321  t->SetBranchAddress("see_stateCcov24", &see_stateCcov24);
322  t->SetBranchAddress("see_stateCcov25", &see_stateCcov25);
323  t->SetBranchAddress("see_stateCcov33", &see_stateCcov33);
324  t->SetBranchAddress("see_stateCcov34", &see_stateCcov34);
325  t->SetBranchAddress("see_stateCcov35", &see_stateCcov35);
326  t->SetBranchAddress("see_stateCcov44", &see_stateCcov44);
327  t->SetBranchAddress("see_stateCcov45", &see_stateCcov45);
328  t->SetBranchAddress("see_stateCcov55", &see_stateCcov55);
329  } else {
330  t->SetBranchAddress("see_stateCurvCov", &see_stateCurvCov);
331  }
332  t->SetBranchAddress("see_q", &see_q);
333  t->SetBranchAddress("see_algo", &see_algo);
334 
335  std::vector<std::vector<int>>* see_hitIdx = 0;
336  t->SetBranchAddress("see_hitIdx", &see_hitIdx);
337  std::vector<std::vector<int>>* see_hitType = 0;
338  t->SetBranchAddress("see_hitType", &see_hitType);
339 
340  //pixel hits
341  vector<unsigned short>* pix_det = 0;
342  vector<unsigned short>* pix_lay = 0;
343  vector<unsigned int>* pix_detId = 0;
344  vector<float>* pix_x = 0;
345  vector<float>* pix_y = 0;
346  vector<float>* pix_z = 0;
347  vector<float>* pix_xx = 0;
348  vector<float>* pix_xy = 0;
349  vector<float>* pix_yy = 0;
350  vector<float>* pix_yz = 0;
351  vector<float>* pix_zz = 0;
352  vector<float>* pix_zx = 0;
353  vector<int>* pix_csize_col = 0;
354  vector<int>* pix_csize_row = 0;
355  vector<uint64_t>* pix_usedMask = 0;
356  //these were renamed in CMSSW_9_1_0: auto-detect
357  bool has910_det_lay = t->GetBranch("pix_det") == nullptr;
358  if (has910_det_lay) {
359  t->SetBranchAddress("pix_subdet", &pix_det);
360  t->SetBranchAddress("pix_layer", &pix_lay);
361  } else {
362  t->SetBranchAddress("pix_det", &pix_det);
363  t->SetBranchAddress("pix_lay", &pix_lay);
364  }
365  t->SetBranchAddress("pix_detId", &pix_detId);
366  t->SetBranchAddress("pix_x", &pix_x);
367  t->SetBranchAddress("pix_y", &pix_y);
368  t->SetBranchAddress("pix_z", &pix_z);
369  t->SetBranchAddress("pix_xx", &pix_xx);
370  t->SetBranchAddress("pix_xy", &pix_xy);
371  t->SetBranchAddress("pix_yy", &pix_yy);
372  t->SetBranchAddress("pix_yz", &pix_yz);
373  t->SetBranchAddress("pix_zz", &pix_zz);
374  t->SetBranchAddress("pix_zx", &pix_zx);
375  t->SetBranchAddress("pix_clustSizeCol", &pix_csize_col);
376  t->SetBranchAddress("pix_clustSizeRow", &pix_csize_row);
377  if (writeHitIterMasks) {
378  t->SetBranchAddress("pix_usedMask", &pix_usedMask);
379  }
380 
381  vector<vector<int>>* pix_simHitIdx = 0;
382  t->SetBranchAddress("pix_simHitIdx", &pix_simHitIdx);
383  vector<vector<float>>* pix_chargeFraction = 0;
384  t->SetBranchAddress("pix_chargeFraction", &pix_chargeFraction);
385 
386  //strip hits
387  vector<short>* glu_isBarrel = 0;
388  vector<unsigned int>* glu_det = 0;
389  vector<unsigned int>* glu_lay = 0;
390  vector<unsigned int>* glu_detId = 0;
391  vector<int>* glu_monoIdx = 0;
392  vector<int>* glu_stereoIdx = 0;
393  vector<float>* glu_x = 0;
394  vector<float>* glu_y = 0;
395  vector<float>* glu_z = 0;
396  vector<float>* glu_xx = 0;
397  vector<float>* glu_xy = 0;
398  vector<float>* glu_yy = 0;
399  vector<float>* glu_yz = 0;
400  vector<float>* glu_zz = 0;
401  vector<float>* glu_zx = 0;
402  t->SetBranchAddress("glu_isBarrel", &glu_isBarrel);
403  if (has910_det_lay) {
404  t->SetBranchAddress("glu_subdet", &glu_det);
405  t->SetBranchAddress("glu_layer", &glu_lay);
406  } else {
407  t->SetBranchAddress("glu_det", &glu_det);
408  t->SetBranchAddress("glu_lay", &glu_lay);
409  }
410  t->SetBranchAddress("glu_detId", &glu_detId);
411  t->SetBranchAddress("glu_monoIdx", &glu_monoIdx);
412  t->SetBranchAddress("glu_stereoIdx", &glu_stereoIdx);
413  t->SetBranchAddress("glu_x", &glu_x);
414  t->SetBranchAddress("glu_y", &glu_y);
415  t->SetBranchAddress("glu_z", &glu_z);
416  t->SetBranchAddress("glu_xx", &glu_xx);
417  t->SetBranchAddress("glu_xy", &glu_xy);
418  t->SetBranchAddress("glu_yy", &glu_yy);
419  t->SetBranchAddress("glu_yz", &glu_yz);
420  t->SetBranchAddress("glu_zz", &glu_zz);
421  t->SetBranchAddress("glu_zx", &glu_zx);
422 
423  vector<short>* str_isBarrel = 0;
424  vector<short>* str_isStereo = 0;
425  vector<unsigned int>* str_det = 0;
426  vector<unsigned int>* str_lay = 0;
427  vector<unsigned int>* str_detId = 0;
428  vector<unsigned int>* str_simType = 0;
429  vector<float>* str_x = 0;
430  vector<float>* str_y = 0;
431  vector<float>* str_z = 0;
432  vector<float>* str_xx = 0;
433  vector<float>* str_xy = 0;
434  vector<float>* str_yy = 0;
435  vector<float>* str_yz = 0;
436  vector<float>* str_zz = 0;
437  vector<float>* str_zx = 0;
438  vector<float>* str_chargePerCM = 0;
439  vector<int>* str_csize = 0;
440  vector<uint64_t>* str_usedMask = 0;
441  t->SetBranchAddress("str_isBarrel", &str_isBarrel);
442  t->SetBranchAddress("str_isStereo", &str_isStereo);
443  if (has910_det_lay) {
444  t->SetBranchAddress("str_subdet", &str_det);
445  t->SetBranchAddress("str_layer", &str_lay);
446  } else {
447  t->SetBranchAddress("str_det", &str_det);
448  t->SetBranchAddress("str_lay", &str_lay);
449  }
450  t->SetBranchAddress("str_detId", &str_detId);
451  t->SetBranchAddress("str_simType", &str_simType);
452  t->SetBranchAddress("str_x", &str_x);
453  t->SetBranchAddress("str_y", &str_y);
454  t->SetBranchAddress("str_z", &str_z);
455  t->SetBranchAddress("str_xx", &str_xx);
456  t->SetBranchAddress("str_xy", &str_xy);
457  t->SetBranchAddress("str_yy", &str_yy);
458  t->SetBranchAddress("str_yz", &str_yz);
459  t->SetBranchAddress("str_zz", &str_zz);
460  t->SetBranchAddress("str_zx", &str_zx);
461  t->SetBranchAddress("str_chargePerCM", &str_chargePerCM);
462  t->SetBranchAddress("str_clustSize", &str_csize);
463  if (writeHitIterMasks) {
464  t->SetBranchAddress("str_usedMask", &str_usedMask);
465  }
466 
467  vector<vector<int>>* str_simHitIdx = 0;
468  t->SetBranchAddress("str_simHitIdx", &str_simHitIdx);
469  vector<vector<float>>* str_chargeFraction = 0;
470  t->SetBranchAddress("str_chargeFraction", &str_chargeFraction);
471 
472  // beam spot
473  float bsp_x;
474  float bsp_y;
475  float bsp_z;
476  float bsp_sigmax;
477  float bsp_sigmay;
478  float bsp_sigmaz;
479  t->SetBranchAddress("bsp_x", &bsp_x);
480  t->SetBranchAddress("bsp_y", &bsp_y);
481  t->SetBranchAddress("bsp_z", &bsp_z);
482  t->SetBranchAddress("bsp_sigmax", &bsp_sigmax);
483  t->SetBranchAddress("bsp_sigmay", &bsp_sigmay);
484  t->SetBranchAddress("bsp_sigmaz", &bsp_sigmaz);
485 
486  long long totentries = t->GetEntries();
487  long long savedEvents = 0;
488 
489  DataFile data_file;
490  int outOptions = DataFile::ES_Seeds;
491  if (writeRecTracks)
492  outOptions |= DataFile::ES_CmsswTracks;
493  if (writeHitIterMasks)
494  outOptions |= DataFile::ES_HitIterMasks;
495  outOptions |= DataFile::ES_BeamSpot;
496 
497  if (maxevt < 0)
498  maxevt = totentries;
499  data_file.openWrite(outputFileName, static_cast<int>(nTotalLayers), std::min(maxevt, totentries), outOptions);
500 
501  Event EE(0, static_cast<int>(nTotalLayers));
502 
503  int numFailCCC = 0;
504  int numTotalStr = 0;
505  // gDebug = 8;
506 
507  for (long long i = 0; savedEvents < maxevt && i < totentries && i < maxevt; ++i) {
508  EE.reset(i);
509 
510  cout << "process entry i=" << i << " out of " << totentries << ", saved so far " << savedEvents
511  << ", with max=" << maxevt << endl;
512 
513  t->GetEntry(i);
514 
515  cout << "edm event=" << event << endl;
516 
517  auto& bs = EE.beamSpot_;
518  bs.x = bsp_x;
519  bs.y = bsp_y;
520  bs.z = bsp_z;
521  bs.sigmaZ = bsp_sigmaz;
522  bs.beamWidthX = bsp_sigmax;
523  bs.beamWidthY = bsp_sigmay;
524  //dxdz and dydz are not in the trackingNtuple at the moment
525 
526  for (unsigned int istr = 0; istr < str_lay->size(); ++istr) {
527  if (str_chargePerCM->at(istr) < cutValueCCC)
528  numFailCCC++;
529  numTotalStr++;
530  }
531 
532  auto nSims = sim_q->size();
533  if (nSims == 0) {
534  cout << "branches not loaded" << endl;
535  exit(1);
536  }
537  if (verbosity > 0)
538  std::cout << __FILE__ << " " << __LINE__ << " nSims " << nSims << " nSeeds " << see_q->size() << " nRecT "
539  << trk_q->size() << std::endl;
540 
541  //find best matching tkIdx from a list of simhits indices
542  auto bestTkIdx = [&](std::vector<int> const& shs, std::vector<float> const& shfs, int rhIdx, HitType rhType) {
543  //assume that all simhits are associated
544  int ibest = -1;
545  int shbest = -1;
546  float hpbest = -1;
547  float tpbest = -1;
548  float hfbest = -1;
549 
550  float maxfrac = -1;
551  int ish = -1;
552  int nshs = shs.size();
553  for (auto const sh : shs) {
554  ish++;
555  auto tkidx = simhit_simTrkIdx->at(sh);
556  //use only sh with available TP
557  if (tkidx < 0)
558  continue;
559 
560  auto hpx = simhit_px->at(sh);
561  auto hpy = simhit_py->at(sh);
562  auto hpz = simhit_pz->at(sh);
563  auto hp = sqrt(hpx * hpx + hpy * hpy + hpz * hpz);
564 
565  //look only at hits with p> 50 MeV
566  if (hp < 0.05f)
567  continue;
568 
569  auto tpx = sim_px->at(tkidx);
570  auto tpy = sim_py->at(tkidx);
571  auto tpz = sim_pz->at(tkidx);
572  auto tp = sqrt(tpx * tpx + tpy * tpy + tpz * tpz);
573 
574  //take only hits with hp> 0.5*tp
575  if (hp < 0.5 * tp)
576  continue;
577 
578  //pick tkidx corresponding to max hp/tp; .. this is probably redundant
579  if (maxfrac < hp / tp) {
580  maxfrac = hp / tp;
581  ibest = tkidx;
582  shbest = sh;
583  hpbest = hp;
584  tpbest = tp;
585  hfbest = shfs[ish];
586  }
587  }
588 
589  //arbitration: a rechit with one matching sim is matched to sim if it's the first
590  //FIXME: SOME BETTER SELECTION CAN BE DONE (it will require some more correlated knowledge)
591  if (nshs == 1 && ibest >= 0) {
592  auto const& srhIdxV = simhit_hitIdx->at(shbest);
593  auto const& srhTypeV = simhit_hitType->at(shbest);
594  int ih = -1;
595  for (auto itype : srhTypeV) {
596  ih++;
597  if (HitType(itype) == rhType && srhIdxV[ih] != rhIdx) {
598  ibest = -1;
599  break;
600  }
601  }
602  }
603 
604  if (ibest >= 0 && false) {
605  std::cout << " best tkIdx " << ibest << " rh " << rhIdx << " for sh " << shbest << " out of " << shs.size()
606  << " hp " << hpbest << " chF " << hfbest << " tp " << tpbest << " process "
607  << simhit_process->at(shbest) << " particle " << simhit_particle->at(shbest) << std::endl;
608  if (rhType == HitType::Strip) {
609  std::cout << " sh " << simhit_x->at(shbest) << ", " << simhit_y->at(shbest) << ", " << simhit_z->at(shbest)
610  << " rh " << str_x->at(rhIdx) << ", " << str_y->at(rhIdx) << ", " << str_z->at(rhIdx) << std::endl;
611  }
612  }
613  return ibest;
614  };
615 
616  vector<Track>& simTracks_ = EE.simTracks_;
617  vector<int> simTrackIdx_(sim_q->size(), -1); //keep track of original index in ntuple
618  vector<int> seedSimIdx(see_q->size(), -1);
619  for (unsigned int isim = 0; isim < sim_q->size(); ++isim) {
620  //load sim production vertex data
621  auto iVtx = sim_parentVtxIdx->at(isim);
622  constexpr float largeValF = 9999.f;
623  float sim_prodx = iVtx >= 0 ? simvtx_x->at(iVtx) : largeValF;
624  float sim_prody = iVtx >= 0 ? simvtx_y->at(iVtx) : largeValF;
625  float sim_prodz = iVtx >= 0 ? simvtx_z->at(iVtx) : largeValF;
626  //if (fabs(sim_eta->at(isim))>0.8) continue;
627 
628  vector<int> const& trkIdxV = sim_trkIdx->at(isim);
629 
630  //if (trkIdx<0) continue;
631  //FIXME: CHECK IF THE LOOP AND BEST SELECTION IS NEEDED.
632  //Pick the first
633  const int trkIdx = trkIdxV.empty() ? -1 : trkIdxV[0];
634 
635  int nlay = 0;
636  if (trkIdx >= 0) {
637  std::vector<int> hitlay(nTotalLayers, 0);
638  auto const& hits = trk_hitIdx->at(trkIdx);
639  auto const& hitTypes = trk_hitType->at(trkIdx);
640  auto nHits = hits.size();
641  for (auto ihit = 0U; ihit < nHits; ++ihit) {
642  auto ihIdx = hits[ihit];
643  auto const ihType = HitType(hitTypes[ihit]);
644 
645  switch (ihType) {
646  case HitType::Pixel: {
647  int ipix = ihIdx;
648  if (ipix < 0)
649  continue;
650  int cmsswlay =
651  lnc.convertLayerNumber(pix_det->at(ipix), pix_lay->at(ipix), useMatched, -1, pix_z->at(ipix) > 0);
652  if (cmsswlay >= 0 && cmsswlay < static_cast<int>(nTotalLayers))
653  hitlay[cmsswlay]++;
654  break;
655  }
656  case HitType::Strip: {
657  int istr = ihIdx;
658  if (istr < 0)
659  continue;
660  int cmsswlay = lnc.convertLayerNumber(
661  str_det->at(istr), str_lay->at(istr), useMatched, str_isStereo->at(istr), str_z->at(istr) > 0);
662  if (cmsswlay >= 0 && cmsswlay < static_cast<int>(nTotalLayers))
663  hitlay[cmsswlay]++;
664  break;
665  }
666  case HitType::Glued: {
667  if (useMatched) {
668  int iglu = ihIdx;
669  if (iglu < 0)
670  continue;
671  int cmsswlay =
672  lnc.convertLayerNumber(glu_det->at(iglu), glu_lay->at(iglu), useMatched, -1, glu_z->at(iglu) > 0);
673  if (cmsswlay >= 0 && cmsswlay < static_cast<int>(nTotalLayers))
674  hitlay[cmsswlay]++;
675  }
676  break;
677  }
678  case HitType::Invalid:
679  break; //FIXME. Skip, really?
680  default:
681  throw std::logic_error("Track type can not be handled");
682  } //hit type
683  } //hits on track
684  for (unsigned int i = 0; i < nTotalLayers; i++)
685  if (hitlay[i] > 0)
686  nlay++;
687  } //count nlay layers on matching reco track
688 
689  //cout << Form("track q=%2i p=(%6.3f, %6.3f, %6.3f) x=(%6.3f, %6.3f, %6.3f) nlay=%i",sim_q->at(isim),sim_px->at(isim),sim_py->at(isim),sim_pz->at(isim),sim_prodx,sim_prody,sim_prodz,nlay) << endl;
690 
691  SVector3 pos(sim_prodx, sim_prody, sim_prodz);
692  SVector3 mom(sim_px->at(isim), sim_py->at(isim), sim_pz->at(isim));
694  err.At(0, 0) = sim_prodx * sim_prodx;
695  err.At(1, 1) = sim_prody * sim_prody;
696  err.At(2, 2) = sim_prodz * sim_prodz;
697  err.At(3, 3) = sim_px->at(isim) * sim_px->at(isim);
698  err.At(4, 4) = sim_py->at(isim) * sim_py->at(isim);
699  err.At(5, 5) = sim_pz->at(isim) * sim_pz->at(isim);
700  TrackState state(sim_q->at(isim), pos, mom, err);
701  state.convertFromCartesianToCCS();
702  //create track: store number of reco hits in place of track chi2; fill hits later
703  // set label to be its own index in the output file
704  Track track(state, float(nlay), simTracks_.size(), 0, nullptr);
705  if (sim_bunchCrossing->at(isim) == 0) { //in time
706  if (sim_event->at(isim) == 0)
707  track.setProdType(Track::ProdType::Signal);
708  else
709  track.setProdType(Track::ProdType::InTimePU);
710  } else {
711  track.setProdType(Track::ProdType::OutOfTimePU);
712  }
713  if (trkIdx >= 0) {
714  int seedIdx = trk_seedIdx->at(trkIdx);
715  // Unused: auto const& shTypes = see_hitType->at(seedIdx);
716  seedSimIdx[seedIdx] = simTracks_.size();
717  }
718  if (cleanSimTracks) {
719  if (sim_nValid->at(isim) < cleanSimTrack_minSimHits)
720  continue;
721  if (cleanSimTrack_minRecHits > 0) {
722  int nRecToSimHit = 0;
723  for (unsigned int ipix = 0; ipix < pix_lay->size() && nRecToSimHit < cleanSimTrack_minRecHits; ++ipix) {
724  int ilay = -1;
725  ilay = lnc.convertLayerNumber(pix_det->at(ipix), pix_lay->at(ipix), useMatched, -1, pix_z->at(ipix) > 0);
726  if (ilay < 0)
727  continue;
728  int simTkIdxNt = bestTkIdx(pix_simHitIdx->at(ipix), pix_chargeFraction->at(ipix), ipix, HitType::Pixel);
729  if (simTkIdxNt >= 0)
730  nRecToSimHit++;
731  }
732  if (useMatched) {
733  for (unsigned int iglu = 0; iglu < glu_lay->size() && nRecToSimHit < cleanSimTrack_minRecHits; ++iglu) {
734  if (glu_isBarrel->at(iglu) == 0)
735  continue;
736  int igluMono = glu_monoIdx->at(iglu);
737  int simTkIdxNt =
738  bestTkIdx(str_simHitIdx->at(igluMono), str_chargeFraction->at(igluMono), igluMono, HitType::Strip);
739  if (simTkIdxNt >= 0)
740  nRecToSimHit++;
741  }
742  }
743  for (unsigned int istr = 0; istr < str_lay->size() && nRecToSimHit < cleanSimTrack_minRecHits; ++istr) {
744  int ilay = -1;
745  ilay = lnc.convertLayerNumber(
746  str_det->at(istr), str_lay->at(istr), useMatched, str_isStereo->at(istr), str_z->at(istr) > 0);
747  if (useMatched && str_isBarrel->at(istr) == 1 && str_isStereo->at(istr))
748  continue;
749  if (ilay == -1)
750  continue;
751  int simTkIdxNt = bestTkIdx(str_simHitIdx->at(istr), str_chargeFraction->at(istr), istr, HitType::Strip);
752  if (simTkIdxNt >= 0)
753  nRecToSimHit++;
754  }
755  if (nRecToSimHit < cleanSimTrack_minRecHits)
756  continue;
757  } //count rec-to-sim hits
758  } //cleanSimTracks
759 
760  simTrackIdx_[isim] = simTracks_.size();
761  simTracks_.push_back(track);
762  }
763 
764  if (simTracks_.empty() and not writeAllEvents)
765  continue;
766 
767  vector<Track>& seedTracks_ = EE.seedTracks_;
768  vector<vector<int>> pixHitSeedIdx(pix_lay->size());
769  vector<vector<int>> strHitSeedIdx(str_lay->size());
770  vector<vector<int>> gluHitSeedIdx(glu_lay->size());
771  for (unsigned int is = 0; is < see_q->size(); ++is) {
772  auto isAlgo = TrackAlgorithm(see_algo->at(is));
773  if (not allSeeds)
774  if (isAlgo != TrackAlgorithm::initialStep && isAlgo != TrackAlgorithm::hltIter0)
775  continue; //select seed in acceptance
776  //if (see_pt->at(is)<0.5 || fabs(see_eta->at(is))>0.8) continue;//select seed in acceptance
777  SVector3 pos = SVector3(see_stateTrajGlbX->at(is), see_stateTrajGlbY->at(is), see_stateTrajGlbZ->at(is));
778  SVector3 mom = SVector3(see_stateTrajGlbPx->at(is), see_stateTrajGlbPy->at(is), see_stateTrajGlbPz->at(is));
780  if (hasCartCov) {
781  err.At(0, 0) = see_stateCcov00->at(is);
782  err.At(0, 1) = see_stateCcov01->at(is);
783  err.At(0, 2) = see_stateCcov02->at(is);
784  err.At(0, 3) = see_stateCcov03->at(is);
785  err.At(0, 4) = see_stateCcov04->at(is);
786  err.At(0, 5) = see_stateCcov05->at(is);
787  err.At(1, 1) = see_stateCcov11->at(is);
788  err.At(1, 2) = see_stateCcov12->at(is);
789  err.At(1, 3) = see_stateCcov13->at(is);
790  err.At(1, 4) = see_stateCcov14->at(is);
791  err.At(1, 5) = see_stateCcov15->at(is);
792  err.At(2, 2) = see_stateCcov22->at(is);
793  err.At(2, 3) = see_stateCcov23->at(is);
794  err.At(2, 4) = see_stateCcov24->at(is);
795  err.At(2, 5) = see_stateCcov25->at(is);
796  err.At(3, 3) = see_stateCcov33->at(is);
797  err.At(3, 4) = see_stateCcov34->at(is);
798  err.At(3, 5) = see_stateCcov35->at(is);
799  err.At(4, 4) = see_stateCcov44->at(is);
800  err.At(4, 5) = see_stateCcov45->at(is);
801  err.At(5, 5) = see_stateCcov55->at(is);
802  } else {
803  auto const& vCov = see_stateCurvCov->at(is);
804  assert(vCov.size() == 15);
805  auto vCovP = vCov.begin();
806  for (int i = 0; i < 5; ++i)
807  for (int j = 0; j <= i; ++j)
808  err.At(i, j) = *(vCovP++);
809  }
810  TrackState state(see_q->at(is), pos, mom, err);
811  if (hasCartCov)
812  state.convertFromCartesianToCCS();
813  else
814  state.convertFromGlbCurvilinearToCCS();
815  Track track(state, 0, seedSimIdx[is], 0, nullptr);
816  track.setAlgorithm(isAlgo);
817  auto const& shTypes = see_hitType->at(is);
818  auto const& shIdxs = see_hitIdx->at(is);
819  if (not allSeeds)
820  if (!((isAlgo == TrackAlgorithm::initialStep || isAlgo == TrackAlgorithm::hltIter0) &&
821  std::count(shTypes.begin(), shTypes.end(), int(HitType::Pixel)) >= 3))
822  continue; //check algo and nhits
823  for (unsigned int ip = 0; ip < shTypes.size(); ip++) {
824  unsigned int hidx = shIdxs[ip];
825  switch (HitType(shTypes[ip])) {
826  case HitType::Pixel: {
827  pixHitSeedIdx[hidx].push_back(seedTracks_.size());
828  break;
829  }
830  case HitType::Strip: {
831  strHitSeedIdx[hidx].push_back(seedTracks_.size());
832  break;
833  }
834  case HitType::Glued: {
835  if (not useMatched) {
836  //decompose
837  int uidx = glu_monoIdx->at(hidx);
838  strHitSeedIdx[uidx].push_back(seedTracks_.size());
839  uidx = glu_stereoIdx->at(hidx);
840  strHitSeedIdx[uidx].push_back(seedTracks_.size());
841  } else {
842  gluHitSeedIdx[hidx].push_back(seedTracks_.size());
843  }
844  break;
845  }
846  case HitType::Invalid:
847  break; //FIXME. Skip, really?
848  default:
849  throw std::logic_error("Track hit type can not be handled");
850  } //switch( HitType
851  }
852  seedTracks_.push_back(track);
853  }
854 
855  if (seedTracks_.empty() and not writeAllEvents)
856  continue;
857 
858  vector<Track>& cmsswTracks_ = EE.cmsswTracks_;
859  vector<vector<int>> pixHitRecIdx(pix_lay->size());
860  vector<vector<int>> strHitRecIdx(str_lay->size());
861  vector<vector<int>> gluHitRecIdx(glu_lay->size());
862  for (unsigned int ir = 0; ir < trk_q->size(); ++ir) {
863  //check the origin; redundant for initialStep ntuples
864  if (not allSeeds)
865  if ((trk_algoMask->at(ir) & ((1 << int(TrackAlgorithm::initialStep)) | (1 << int(TrackAlgorithm::hltIter0)))) ==
866  0) {
867  if (verbosity > 1) {
868  std::cout << "track " << ir << " failed algo selection for " << int(TrackAlgorithm::initialStep)
869  << ": mask " << trk_algoMask->at(ir) << " origAlgo " << trk_originalAlgo->at(ir) << " algo "
870  << trk_algo->at(ir) << std::endl;
871  }
872  continue;
873  }
874  //fill the state in CCS upfront
876  /*
877  vx = -dxy*sin(phi) - pt*cos(phi)/p*pz/p*dz;
878  vy = dxy*cos(phi) - pt*sin(phi)/p*pz/p*dz;
879  vz = dz*pt*pt/p/p;
880  //partial: ignores cross-terms
881  c(vx,vx) = c(dxy,dxy)*sin(phi)*sin(phi) + c(dz,dz)*pow(pt*cos(phi)/p*pz/p ,2);
882  c(vx,vy) = -c(dxy,dxy)*cos(phi)*sin(phi) + c(dz,dz)*cos(phi)*sin(phi)*pow(pt/p*pz/p, 2);
883  c(vy,vy) = c(dxy,dxy)*cos(phi)*cos(phi) + c(dz,dz)*pow(pt*sin(phi)/p*pz/p ,2);
884  c(vx,vz) = -c(dz,dz)*pt*pt/p/p*pt/p*pz/p*cos(phi);
885  c(vy,vz) = -c(dz,dz)*pt*pt/p/p*pt/p*pz/p*sin(phi);
886  c(vz,vz) = c(dz,dz)*pow(pt*pt/p/p, 2);
887  */
888  float pt = trk_pt->at(ir);
889  float pz = trk_pz->at(ir);
890  float p2 = pt * pt + pz * pz;
891  float phi = trk_phi->at(ir);
892  float sP = sin(phi);
893  float cP = cos(phi);
894  float dxyErr2 = trk_dxyErr->at(ir);
895  dxyErr2 *= dxyErr2;
896  float dzErr2 = trk_dzErr->at(ir);
897  dzErr2 *= dzErr2;
898  float dzErrF2 = trk_dzErr->at(ir) * (pt * pz / p2);
899  dzErr2 *= dzErr2;
900  err.At(0, 0) = dxyErr2 * sP * sP + dzErrF2 * cP * cP;
901  err.At(0, 1) = -dxyErr2 * cP * sP + dzErrF2 * cP * sP;
902  err.At(1, 1) = dxyErr2 * cP * cP + dzErrF2 * sP * sP;
903  err.At(0, 2) = -dzErrF2 * cP * pt / pz;
904  err.At(1, 2) = -dzErrF2 * sP * pt / pz;
905  err.At(2, 2) = dzErr2 * std::pow((pt * pt / p2), 2);
906  err.At(3, 3) = std::pow(trk_ptErr->at(ir) / pt / pt, 2);
907  err.At(4, 4) = std::pow(trk_phiErr->at(ir), 2);
908  err.At(5, 5) = std::pow(trk_lambdaErr->at(ir), 2);
909  SVector3 pos = SVector3(trk_refpoint_x->at(ir), trk_refpoint_y->at(ir), trk_refpoint_z->at(ir));
910  SVector3 mom = SVector3(1.f / pt, phi, M_PI_2 - trk_lambda->at(ir));
911  TrackState state(trk_q->at(ir), pos, mom, err);
912  Track track(state, trk_nChi2->at(ir), trk_seedIdx->at(ir), 0, nullptr); //hits are filled later
913  track.setAlgorithm(TrackAlgorithm(trk_originalAlgo->at(ir)));
914  auto const& hTypes = trk_hitType->at(ir);
915  auto const& hIdxs = trk_hitIdx->at(ir);
916  for (unsigned int ip = 0; ip < hTypes.size(); ip++) {
917  unsigned int hidx = hIdxs[ip];
918  switch (HitType(hTypes[ip])) {
919  case HitType::Pixel: {
920  //cout << "pix=" << hidx << " track=" << cmsswTracks_.size() << endl;
921  pixHitRecIdx[hidx].push_back(cmsswTracks_.size());
922  break;
923  }
924  case HitType::Strip: {
925  //cout << "pix=" << hidx << " track=" << cmsswTracks_.size() << endl;
926  strHitRecIdx[hidx].push_back(cmsswTracks_.size());
927  break;
928  }
929  case HitType::Glued: {
930  if (not useMatched)
931  throw std::logic_error("Tracks have glued hits, but matchedHit load is not configured");
932  //cout << "pix=" << hidx << " track=" << cmsswTracks_.size() << endl;
933  gluHitRecIdx[hidx].push_back(cmsswTracks_.size());
934  break;
935  }
936  case HitType::Invalid:
937  break; //FIXME. Skip, really?
938  default:
939  throw std::logic_error("Track hit type can not be handled");
940  } //switch( HitType
941  }
942  cmsswTracks_.push_back(track);
943  }
944 
945  vector<vector<Hit>>& layerHits_ = EE.layerHits_;
946  vector<vector<uint64_t>>& layerHitMasks_ = EE.layerHitMasks_;
947  vector<MCHitInfo>& simHitsInfo_ = EE.simHitsInfo_;
948  int totHits = 0;
949  layerHits_.resize(nTotalLayers);
950  layerHitMasks_.resize(nTotalLayers);
951  for (unsigned int ipix = 0; ipix < pix_lay->size(); ++ipix) {
952  int ilay = -1;
953  ilay = lnc.convertLayerNumber(pix_det->at(ipix), pix_lay->at(ipix), useMatched, -1, pix_z->at(ipix) > 0);
954  if (ilay < 0)
955  continue;
956 
957  unsigned int imoduleid = tkinfo[ilay].short_id(pix_detId->at(ipix));
958 
959  int simTkIdxNt = bestTkIdx(pix_simHitIdx->at(ipix), pix_chargeFraction->at(ipix), ipix, HitType::Pixel);
960  int simTkIdx = simTkIdxNt >= 0 ? simTrackIdx_[simTkIdxNt] : -1; //switch to index in simTracks_
961 
962  //cout << Form("pix lay=%i det=%i x=(%6.3f, %6.3f, %6.3f)",ilay+1,pix_det->at(ipix),pix_x->at(ipix),pix_y->at(ipix),pix_z->at(ipix)) << endl;
963  SVector3 pos(pix_x->at(ipix), pix_y->at(ipix), pix_z->at(ipix));
965  err.At(0, 0) = pix_xx->at(ipix);
966  err.At(1, 1) = pix_yy->at(ipix);
967  err.At(2, 2) = pix_zz->at(ipix);
968  err.At(0, 1) = pix_xy->at(ipix);
969  err.At(0, 2) = pix_zx->at(ipix);
970  err.At(1, 2) = pix_yz->at(ipix);
971  if (simTkIdx >= 0) {
972  simTracks_[simTkIdx].addHitIdx(layerHits_[ilay].size(), ilay, 0);
973  }
974  for (unsigned int is = 0; is < pixHitSeedIdx[ipix].size(); is++) {
975  //cout << "xxx ipix=" << ipix << " seed=" << pixHitSeedIdx[ipix][is] << endl;
976  seedTracks_[pixHitSeedIdx[ipix][is]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
977  }
978  for (unsigned int ir = 0; ir < pixHitRecIdx[ipix].size(); ir++) {
979  //cout << "xxx ipix=" << ipix << " recTrack=" << pixHitRecIdx[ipix][ir] << endl;
980  cmsswTracks_[pixHitRecIdx[ipix][ir]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
981  }
982  Hit hit(pos, err, totHits);
983  hit.setupAsPixel(imoduleid, pix_csize_row->at(ipix), pix_csize_col->at(ipix));
984  layerHits_[ilay].push_back(hit);
985  if (writeHitIterMasks)
986  layerHitMasks_[ilay].push_back(pix_usedMask->at(ipix));
987  MCHitInfo hitInfo(simTkIdx, ilay, layerHits_[ilay].size() - 1, totHits);
988  simHitsInfo_.push_back(hitInfo);
989  totHits++;
990  }
991 
992  if (useMatched) {
993  for (unsigned int iglu = 0; iglu < glu_lay->size(); ++iglu) {
994  if (glu_isBarrel->at(iglu) == 0)
995  continue;
996  int igluMono = glu_monoIdx->at(iglu);
997  int simTkIdxNt =
998  bestTkIdx(str_simHitIdx->at(igluMono), str_chargeFraction->at(igluMono), igluMono, HitType::Strip);
999  int simTkIdx = simTkIdxNt >= 0 ? simTrackIdx_[simTkIdxNt] : -1; //switch to index in simTracks_
1000 
1001  int ilay = lnc.convertLayerNumber(glu_det->at(iglu), glu_lay->at(iglu), useMatched, -1, glu_z->at(iglu) > 0);
1002  // cout << Form("glu lay=%i det=%i bar=%i x=(%6.3f, %6.3f, %6.3f)",ilay+1,glu_det->at(iglu),glu_isBarrel->at(iglu),glu_x->at(iglu),glu_y->at(iglu),glu_z->at(iglu)) << endl;
1003  SVector3 pos(glu_x->at(iglu), glu_y->at(iglu), glu_z->at(iglu));
1004  SMatrixSym33 err;
1005  err.At(0, 0) = glu_xx->at(iglu);
1006  err.At(1, 1) = glu_yy->at(iglu);
1007  err.At(2, 2) = glu_zz->at(iglu);
1008  err.At(0, 1) = glu_xy->at(iglu);
1009  err.At(0, 2) = glu_zx->at(iglu);
1010  err.At(1, 2) = glu_yz->at(iglu);
1011  if (simTkIdx >= 0) {
1012  simTracks_[simTkIdx].addHitIdx(layerHits_[ilay].size(), ilay, 0);
1013  }
1014  for (unsigned int ir = 0; ir < gluHitSeedIdx[iglu].size(); ir++) {
1015  //cout << "xxx iglu=" << iglu << " seed=" << gluHitSeedIdx[iglu][ir] << endl;
1016  seedTracks_[gluHitSeedIdx[iglu][ir]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
1017  }
1018  for (unsigned int ir = 0; ir < gluHitRecIdx[iglu].size(); ir++) {
1019  //cout << "xxx iglu=" << iglu << " recTrack=" << gluHitRecIdx[iglu][ir] << endl;
1020  cmsswTracks_[gluHitRecIdx[iglu][ir]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
1021  }
1022 
1023  // QQQQ module-id-in-layer, adc and phi/theta spans are not done for matched hits.
1024  // Will we ever use / need this?
1025  assert(false && "Implement module-ids, cluster adc and spans for matched hits!");
1026 
1027  Hit hit(pos, err, totHits);
1028  layerHits_[ilay].push_back(hit);
1029  MCHitInfo hitInfo(simTkIdx, ilay, layerHits_[ilay].size() - 1, totHits);
1030  simHitsInfo_.push_back(hitInfo);
1031  totHits++;
1032  }
1033  }
1034 
1035  vector<int> strIdx;
1036  strIdx.resize(str_lay->size());
1037  for (unsigned int istr = 0; istr < str_lay->size(); ++istr) {
1038  int ilay = -1;
1039  ilay = lnc.convertLayerNumber(
1040  str_det->at(istr), str_lay->at(istr), useMatched, str_isStereo->at(istr), str_z->at(istr) > 0);
1041  if (useMatched && str_isBarrel->at(istr) == 1 && str_isStereo->at(istr))
1042  continue;
1043  if (ilay == -1)
1044  continue;
1045 
1046  unsigned int imoduleid = tkinfo[ilay].short_id(str_detId->at(istr));
1047 
1048  int simTkIdxNt = bestTkIdx(str_simHitIdx->at(istr), str_chargeFraction->at(istr), istr, HitType::Strip);
1049  int simTkIdx = simTkIdxNt >= 0 ? simTrackIdx_[simTkIdxNt] : -1; //switch to index in simTracks_
1050 
1051  bool passCCC = applyCCC ? (str_chargePerCM->at(istr) > cutValueCCC) : true;
1052 
1053  //if (str_onTrack->at(istr)==0) continue;//do not consider hits that are not on track!
1054  SVector3 pos(str_x->at(istr), str_y->at(istr), str_z->at(istr));
1055  SMatrixSym33 err;
1056  err.At(0, 0) = str_xx->at(istr);
1057  err.At(1, 1) = str_yy->at(istr);
1058  err.At(2, 2) = str_zz->at(istr);
1059  err.At(0, 1) = str_xy->at(istr);
1060  err.At(0, 2) = str_zx->at(istr);
1061  err.At(1, 2) = str_yz->at(istr);
1062  if (simTkIdx >= 0) {
1063  if (passCCC)
1064  simTracks_[simTkIdx].addHitIdx(layerHits_[ilay].size(), ilay, 0);
1065  else
1066  simTracks_[simTkIdx].addHitIdx(-9, ilay, 0);
1067  }
1068  for (unsigned int ir = 0; ir < strHitSeedIdx[istr].size(); ir++) {
1069  //cout << "xxx istr=" << istr << " seed=" << strHitSeedIdx[istr][ir] << endl;
1070  if (passCCC)
1071  seedTracks_[strHitSeedIdx[istr][ir]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
1072  else
1073  seedTracks_[strHitSeedIdx[istr][ir]].addHitIdx(-9, ilay, 0);
1074  }
1075  for (unsigned int ir = 0; ir < strHitRecIdx[istr].size(); ir++) {
1076  //cout << "xxx istr=" << istr << " recTrack=" << strHitRecIdx[istr][ir] << endl;
1077  if (passCCC)
1078  cmsswTracks_[strHitRecIdx[istr][ir]].addHitIdx(layerHits_[ilay].size(), ilay, 0); //per-hit chi2 is not known
1079  else
1080  cmsswTracks_[strHitRecIdx[istr][ir]].addHitIdx(-9, ilay, 0);
1081  }
1082  if (passCCC) {
1083  Hit hit(pos, err, totHits);
1084  hit.setupAsStrip(imoduleid, str_chargePerCM->at(istr), str_csize->at(istr));
1085  layerHits_[ilay].push_back(hit);
1086  if (writeHitIterMasks)
1087  layerHitMasks_[ilay].push_back(str_usedMask->at(istr));
1088  MCHitInfo hitInfo(simTkIdx, ilay, layerHits_[ilay].size() - 1, totHits);
1089  simHitsInfo_.push_back(hitInfo);
1090  totHits++;
1091  }
1092  }
1093 
1094  // Seed % hit statistics
1095  nstot += seedTracks_.size();
1096  for (unsigned int il = 0; il < layerHits_.size(); ++il) {
1097  int nh = layerHits_[il].size();
1098  nhitstot[il] += nh;
1099  }
1100 
1101  if (verbosity > 0) {
1102  int nt = simTracks_.size();
1103 
1104  int nl = layerHits_.size();
1105 
1106  int nm = simHitsInfo_.size();
1107 
1108  int ns = seedTracks_.size();
1109 
1110  int nr = cmsswTracks_.size();
1111 
1112  printf("number of simTracks %i\n", nt);
1113  printf("number of layerHits %i\n", nl);
1114  printf("number of simHitsInfo %i\n", nm);
1115  printf("number of seedTracks %i\n", ns);
1116  printf("number of recTracks %i\n", nr);
1117 
1118  if (verbosity > 1) {
1119  printf("\n");
1120  for (int il = 0; il < nl; ++il) {
1121  int nh = layerHits_[il].size();
1122  for (int ih = 0; ih < nh; ++ih) {
1123  printf("lay=%i idx=%i mcid=%i x=(%6.3f, %6.3f, %6.3f) r=%6.3f mask=0x%lx\n",
1124  il + 1,
1125  ih,
1126  layerHits_[il][ih].mcHitID(),
1127  layerHits_[il][ih].x(),
1128  layerHits_[il][ih].y(),
1129  layerHits_[il][ih].z(),
1130  sqrt(pow(layerHits_[il][ih].x(), 2) + pow(layerHits_[il][ih].y(), 2)),
1131  writeHitIterMasks ? layerHitMasks_[il][ih] : 0);
1132  }
1133  }
1134 
1135  for (int i = 0; i < nt; ++i) {
1136  float spt = sqrt(pow(simTracks_[i].px(), 2) + pow(simTracks_[i].py(), 2));
1137  printf(
1138  "sim track id=%i q=%2i p=(%6.3f, %6.3f, %6.3f) x=(%6.3f, %6.3f, %6.3f) pT=%7.4f nTotal=%i nFound=%i \n",
1139  i,
1140  simTracks_[i].charge(),
1141  simTracks_[i].px(),
1142  simTracks_[i].py(),
1143  simTracks_[i].pz(),
1144  simTracks_[i].x(),
1145  simTracks_[i].y(),
1146  simTracks_[i].z(),
1147  spt,
1148  simTracks_[i].nTotalHits(),
1149  simTracks_[i].nFoundHits());
1150  int nh = simTracks_[i].nTotalHits();
1151  for (int ih = 0; ih < nh; ++ih) {
1152  int hidx = simTracks_[i].getHitIdx(ih);
1153  int hlay = simTracks_[i].getHitLyr(ih);
1154  float hx = layerHits_[hlay][hidx].x();
1155  float hy = layerHits_[hlay][hidx].y();
1156  float hz = layerHits_[hlay][hidx].z();
1157  printf("track #%4i hit #%2i idx=%4i lay=%2i x=(% 8.3f, % 8.3f, % 8.3f) r=%8.3f\n",
1158  i,
1159  ih,
1160  hidx,
1161  hlay,
1162  hx,
1163  hy,
1164  hz,
1165  sqrt(hx * hx + hy * hy));
1166  }
1167  }
1168 
1169  for (int i = 0; i < ns; ++i) {
1170  printf("seed id=%i label=%i algo=%i q=%2i pT=%6.3f p=(%6.3f, %6.3f, %6.3f) x=(%6.3f, %6.3f, %6.3f)\n",
1171  i,
1172  seedTracks_[i].label(),
1173  (int)seedTracks_[i].algorithm(),
1174  seedTracks_[i].charge(),
1175  seedTracks_[i].pT(),
1176  seedTracks_[i].px(),
1177  seedTracks_[i].py(),
1178  seedTracks_[i].pz(),
1179  seedTracks_[i].x(),
1180  seedTracks_[i].y(),
1181  seedTracks_[i].z());
1182  int nh = seedTracks_[i].nTotalHits();
1183  for (int ih = 0; ih < nh; ++ih)
1184  printf("seed #%i hit #%i idx=%i\n", i, ih, seedTracks_[i].getHitIdx(ih));
1185  }
1186 
1187  if (writeRecTracks) {
1188  for (int i = 0; i < nr; ++i) {
1189  float spt = sqrt(pow(cmsswTracks_[i].px(), 2) + pow(cmsswTracks_[i].py(), 2));
1190  printf(
1191  "rec track id=%i label=%i algo=%i chi2=%6.3f q=%2i p=(%6.3f, %6.3f, %6.3f) x=(%6.3f, %6.3f, %6.3f) "
1192  "pT=%7.4f nTotal=%i nFound=%i \n",
1193  i,
1194  cmsswTracks_[i].label(),
1195  (int)cmsswTracks_[i].algorithm(),
1196  cmsswTracks_[i].chi2(),
1197  cmsswTracks_[i].charge(),
1198  cmsswTracks_[i].px(),
1199  cmsswTracks_[i].py(),
1200  cmsswTracks_[i].pz(),
1201  cmsswTracks_[i].x(),
1202  cmsswTracks_[i].y(),
1203  cmsswTracks_[i].z(),
1204  spt,
1205  cmsswTracks_[i].nTotalHits(),
1206  cmsswTracks_[i].nFoundHits());
1207  int nh = cmsswTracks_[i].nTotalHits();
1208  for (int ih = 0; ih < nh; ++ih) {
1209  int hidx = cmsswTracks_[i].getHitIdx(ih);
1210  int hlay = cmsswTracks_[i].getHitLyr(ih);
1211  float hx = layerHits_[hlay][hidx].x();
1212  float hy = layerHits_[hlay][hidx].y();
1213  float hz = layerHits_[hlay][hidx].z();
1214  printf("track #%4i hit #%2i idx=%4i lay=%2i x=(% 8.3f, % 8.3f, % 8.3f) r=%8.3f\n",
1215  i,
1216  ih,
1217  hidx,
1218  hlay,
1219  hx,
1220  hy,
1221  hz,
1222  sqrt(hx * hx + hy * hy));
1223  }
1224  }
1225  } //if (writeRecTracks){
1226 
1227  } //verbosity>1
1228  } //verbosity>0
1229  EE.write_out(data_file);
1230 
1231  savedEvents++;
1232  printf("end of event %lli\n", savedEvents);
1233  }
1234 
1235  data_file.CloseWrite(savedEvents);
1236  printf("\nSaved %lli events\n\n", savedEvents);
1237 
1238  printf("Average number of seeds per event %f\n", float(nstot) / float(savedEvents));
1239  for (unsigned int il = 0; il < nhitstot.size(); ++il)
1240  printf("Average number of hits in layer %3i = %7.2f\n",
1241  il,
1242  float(nhitstot[il]) / float(savedEvents)); //Includes those that failed the cluster charge cut
1243 
1244  printf("Out of %i hits, %i failed the cut", numTotalStr, numFailCCC);
1245 
1246  //========================================================================
1247 
1248  printf("\n\n================================================================\n");
1249  printf("=== Max module id for %u layers\n", nTotalLayers);
1250  printf("================================================================\n");
1251  for (unsigned int ii = 0; ii < nTotalLayers; ++ii) {
1252  printf("Layer%2d : %d\n", ii, tkinfo[ii].n_modules());
1253  }
1254 }
void CloseWrite(int n_written)
Definition: Event.cc:978
size
Write out results.
Definition: start.py:1
void next_arg_or_die(lStr_t &args, lStr_i &i)
std::list< std::string > lStr_t
ROOT::Math::SMatrix< float, 6, 6, ROOT::Math::MatRepSym< float, 6 > > SMatrixSym66
Definition: MatrixSTypes.h:8
void read_bin_file(const std::string &fname)
Definition: TrackerInfo.cc:168
bool next_arg_option(lStr_t &args, lStr_i &i)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
ROOT::Math::SVector< float, 3 > SVector3
Definition: MatrixSTypes.h:14
#define M_PI_2
assert(be >=bs)
constexpr bool useMatched
lStr_t::iterator lStr_i
Definition: mkFit.cc:447
char const * label
constexpr int cleanSimTrack_minSimHits
T sqrt(T t)
Definition: SSEVec.h:19
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
int main(int argc, char *argv[])
int n_layers() const
Definition: TrackerInfo.h:161
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
TrackBase::TrackAlgorithm TrackAlgorithm
lStr_t::iterator lStr_i
double f[11][100]
void printHelp(const char *av0)
uint32_t nh
HitType
int nt
Definition: AMPTWrapper.h:42
ii
Definition: cuy.py:589
caConstants::TupleMultiplicity const CAHitNtupletGeneratorKernelsGPU::HitToTuple const cms::cuda::AtomicPairCounter GPUCACell const *__restrict__ uint32_t const *__restrict__ gpuPixelDoublets::CellNeighborsVector const gpuPixelDoublets::CellTracksVector const GPUCACell::OuterHitOfCell const int32_t nHits
void openWrite(const std::string &fname, int n_layers, int n_ev, int extra_sections=0)
Definition: Event.cc:930
float x
constexpr int cleanSimTrack_minRecHits
TrackAlgorithm
track algorithm; copy from TrackBase.h to keep in standalone builds
Definition: Track.h:275
std::list< std::string > lStr_t
Definition: mkFit.cc:446
ROOT::Math::SMatrix< float, 3, 3, ROOT::Math::MatRepSym< float, 3 > > SMatrixSym33
Definition: MatrixSTypes.h:13
int convertLayerNumber(int det, int lay, bool useMatched, int isStereo, bool posZ) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
Definition: event.py:1
def exit(msg="")