CMS 3D CMS Logo

AlignmentStats.cc
Go to the documentation of this file.
2 
10 
16 
19 
25 
26 using namespace std;
27 
29  : esTokenTTopo_(esConsumes()),
30  esTokenTkGeo_(esConsumes()),
31  src_(iConfig.getParameter<edm::InputTag>("src")),
32  overlapAM_(iConfig.getParameter<edm::InputTag>("OverlapAssoMap")),
33  keepTrackStats_(iConfig.getParameter<bool>("keepTrackStats")),
34  keepHitPopulation_(iConfig.getParameter<bool>("keepHitStats")),
35  statsTreeName_(iConfig.getParameter<string>("TrkStatsFileName")),
36  hitsTreeName_(iConfig.getParameter<string>("HitStatsFileName")),
37  prescale_(iConfig.getParameter<uint32_t>("TrkStatsPrescale")) {
38  //sanity checks
39 
40  //init
41  outtree_ = nullptr;
42 
43 } //end constructor
44 
46  //
47 }
48 
49 void AlignmentStats::beginJob() { // const edm::EventSetup &iSetup
50 
51  //book track stats tree
52  treefile_ = new TFile(statsTreeName_.c_str(), "RECREATE");
53  treefile_->cd();
54  outtree_ = new TTree("AlignmentTrackStats", "Statistics of Tracks used for Alignment");
55  // int nHitsinPXB[MAXTRKS_], nHitsinPXE[MAXTRKS_], nHitsinTEC[MAXTRKS_], nHitsinTIB[MAXTRKS_],nHitsinTOB[MAXTRKS_],nHitsinTID[MAXTRKS_];
56 
57  outtree_->Branch("Ntracks", &ntracks, "Ntracks/i");
58  outtree_->Branch("Run_", &run_, "Run_Nr/I");
59  outtree_->Branch("Event", &event_, "EventNr/I");
60  outtree_->Branch("Eta", &Eta, "Eta[Ntracks]/F");
61  outtree_->Branch("Phi", &Phi, "Phi[Ntracks]/F");
62  outtree_->Branch("P", &P, "P[Ntracks]/F");
63  outtree_->Branch("Pt", &Pt, "Pt[Ntracks]/F");
64  outtree_->Branch("Chi2n", &Chi2n, "Chi2n[Ntracks]/F");
65  outtree_->Branch("Nhits", &Nhits, "Nhits[Ntracks][7]/I");
66  /*
67  outtree_->Branch("NhitsPXB" , ,);
68  outtree_->Branch("NhitsPXE" , ,);
69  outtree_->Branch("NhitsTIB" , ,);
70  outtree_->Branch("NhitsTID" , ,);
71  outtree_->Branch("NhitsTOB" , ,);
72  outtree_->Branch("NhitsTOB" , ,);
73  */
74 
76 } //end beginJob
77 
79  //load list of detunits needed then in endJob
80  if (!trackerGeometry_) {
81  trackerGeometry_ = std::make_unique<TrackerGeometry>(iSetup.getData(esTokenTkGeo_));
82  }
83 
84  if (!trackerTopology_) {
85  trackerTopology_ = std::make_unique<TrackerTopology>(iSetup.getData(esTokenTTopo_));
86  }
87 
88  //take trajectories and tracks to loop on
89  // edm::Handle<TrajTrackAssociationCollection> TrackAssoMap;
91  iEvent.getByLabel(src_, Tracks);
92 
93  //take overlap HitAssomap
95  iEvent.getByLabel(overlapAM_, hMap);
96  const AliClusterValueMap &OverlapMap = *hMap;
97 
98  // Initialise
99  run_ = 1;
100  event_ = 1;
101  ntracks = 0;
102  run_ = iEvent.id().run();
103  event_ = iEvent.id().event();
104  ntracks = Tracks->size();
105  // if(ntracks>1) std::cout<<"~~~~~~~~~~~~\n For this event processing "<<ntracks<<" tracks"<<std::endl;
106 
107  unsigned int trk_cnt = 0;
108 
109  for (int j = 0; j < MAXTRKS_; j++) {
110  Eta[j] = -9999.0;
111  Phi[j] = -8888.0;
112  P[j] = -7777.0;
113  Pt[j] = -6666.0;
114  Chi2n[j] = -2222.0;
115  for (int k = 0; k < 7; k++) {
116  Nhits[j][k] = 0;
117  }
118  }
119 
120  // int npxbhits=0;
121 
122  //loop on tracks
123  for (std::vector<reco::Track>::const_iterator ittrk = Tracks->begin(), edtrk = Tracks->end(); ittrk != edtrk;
124  ++ittrk) {
125  Eta[trk_cnt] = ittrk->eta();
126  Phi[trk_cnt] = ittrk->phi();
127  Chi2n[trk_cnt] = ittrk->normalizedChi2();
128  P[trk_cnt] = ittrk->p();
129  Pt[trk_cnt] = ittrk->pt();
130  Nhits[trk_cnt][0] = ittrk->numberOfValidHits();
131 
132  // if(ntracks>1)std::cout<<"Track #"<<trk_cnt+1<<" params: Eta="<< Eta[trk_cnt]<<" Phi="<< Phi[trk_cnt]<<" P="<<P[trk_cnt]<<" Nhits="<<Nhits[trk_cnt][0]<<std::endl;
133 
134  int nhit = 0;
135  //loop on tracking rechits
136  //std::cout << " loop on hits of track #" << (itt - tracks->begin()) << std::endl;
137  for (auto const &hit : ittrk->recHits()) {
138  if (!hit->isValid())
139  continue;
140  DetId detid = hit->geographicalId();
141  int subDet = detid.subdetId();
142  uint32_t rawId = hit->geographicalId().rawId();
143 
144  // if(subDet==1)npxbhits++;
145 
146  //look if you find this detid in the map
147  DetHitMap::iterator mapiter;
148  mapiter = hitmap_.find(rawId);
149  if (mapiter != hitmap_.end()) { //present, increase its value by one
150  // hitmap_[rawId]=hitmap_[rawId]+1;
151  ++(hitmap_[rawId]);
152  } else { //not present, let's add this key to the map with value=1
153  hitmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
154  }
155 
156  AlignmentClusterFlag inval;
157 
158  bool hitInPixel = (subDet == PixelSubdetector::PixelBarrel) || (subDet == PixelSubdetector::PixelEndcap);
159  bool hitInStrip = (subDet == SiStripDetId::TIB) || (subDet == SiStripDetId::TID) ||
160  (subDet == SiStripDetId::TOB) || (subDet == SiStripDetId::TEC);
161 
162  if (!(hitInPixel || hitInStrip)) {
163  //skip only this hit, don't stop everything throwing an exception
164  edm::LogError("AlignmentStats") << "Hit not belonging neither to pixels nor to strips! Skipping it. SubDet= "
165  << subDet;
166  continue;
167  }
168 
169  //check also if the hit is an overlap. If yes fill a dedicated hitmap
170  if (hitInStrip) {
171  const std::type_info &type = typeid(*hit);
172 
173  if (type == typeid(SiStripRecHit1D)) {
174  //Notice the difference respect to when one loops on Trajectories: the recHit is a TrackingRecHit and not a TransientTrackingRecHit
175  const SiStripRecHit1D *striphit = dynamic_cast<const SiStripRecHit1D *>(hit);
176  if (striphit != nullptr) {
177  SiStripRecHit1D::ClusterRef stripclust(striphit->cluster());
178  inval = OverlapMap[stripclust];
179  } else {
180  // edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit1D failed! TypeId of the RecHit: "<<className(*hit);
181  throw cms::Exception("NullPointerError")
182  << "ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit1D failed! TypeId of the RecHit: "
183  << className(*hit);
184  }
185  } //end if sistriprechit1D
186  if (type == typeid(SiStripRecHit2D)) {
187  const SiStripRecHit2D *striphit = dynamic_cast<const SiStripRecHit2D *>(hit);
188  if (striphit != nullptr) {
189  SiStripRecHit2D::ClusterRef stripclust(striphit->cluster());
190  inval = OverlapMap[stripclust];
191  //cout<<"Taken the Strip Cluster with ProdId "<<stripclust.id() <<"; the Value in the map is "<<inval<<" (DetId is "<<hit->geographicalId().rawId()<<")"<<endl;
192  } else {
193  throw cms::Exception("NullPointerError")
194  << "ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit2D failed! TypeId of the RecHit: "
195  << className(*hit);
196  // edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit2D failed! TypeId of the RecHit: "<<className(*hit);
197  }
198  } //end if sistriprechit2D
199 
200  } //end if hit in Strips
201  else {
202  const SiPixelRecHit *pixelhit = dynamic_cast<const SiPixelRecHit *>(hit);
203  if (pixelhit != nullptr) {
204  SiPixelClusterRefNew pixclust(pixelhit->cluster());
205  inval = OverlapMap[pixclust];
206  //cout<<"Taken the Pixel Cluster with ProdId "<<pixclust.id() <<"; the Value in the map is "<<inval<<" (DetId is "<<hit->geographicalId().rawId()<<")"<<endl;
207  } else {
208  edm::LogError("AlignmentStats")
209  << "ERROR in <AlignmentStats::analyze>: Dynamic cast of Pixel RecHit failed! TypeId of the RecHit: "
210  << className(*hit);
211  }
212  } //end else hit is in Pixel
213 
214  bool isOverlapHit(inval.isOverlap());
215 
216  if (isOverlapHit) {
217  //cout<<"This hit is an overlap !"<<endl;
218  DetHitMap::iterator overlapiter;
219  overlapiter = overlapmap_.find(rawId);
220 
221  if (overlapiter !=
222  overlapmap_.end()) { //the det already collected at least an overlap, increase its value by one
223  overlapmap_[rawId] = overlapmap_[rawId] + 1;
224  } else { //first overlap on det unit, let's add it to the map
225  overlapmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
226  }
227  } //end if the hit is an overlap
228 
229  int subdethit = static_cast<int>(hit->geographicalId().subdetId());
230  // if(ntracks>1)std::cout<<"Hit in SubDet="<<subdethit<<std::endl;
231  Nhits[trk_cnt][subdethit] = Nhits[trk_cnt][subdethit] + 1;
232  nhit++;
233  } //end loop on trackingrechits
234  trk_cnt++;
235 
236  } //end loop on tracks
237 
238  // cout<<"Total number of pixel hits is "<<npxbhits<<endl;
239 
240  tmpPresc_--;
241  if (tmpPresc_ < 1) {
242  outtree_->Fill();
244  }
245  if (trk_cnt != ntracks)
246  edm::LogError("AlignmentStats") << "\nERROR! trk_cnt=" << trk_cnt << " ntracks=" << ntracks;
247 
248  return;
249 }
250 
252  treefile_->cd();
253  edm::LogInfo("AlignmentStats") << "Writing out the TrackStatistics in " << gDirectory->GetPath() << std::endl;
254  outtree_->Write();
255  delete outtree_;
256 
257  //create tree with hit maps (hitstree)
258  //book track stats tree
259  TFile *hitsfile = new TFile(hitsTreeName_.c_str(), "RECREATE");
260  hitsfile->cd();
261  TTree *hitstree = new TTree("AlignmentHitMap", "Maps of Hits used for Alignment");
262 
263  unsigned int id = 0, nhits = 0, noverlaps = 0;
264  float posX(-99999.0), posY(-77777.0), posZ(-88888.0);
265  float posEta(-6666.0), posPhi(-5555.0), posR(-4444.0);
266  int subdet = 0;
267  unsigned int layer = 0;
268  bool is2D = false, isStereo = false;
269  hitstree->Branch("DetId", &id, "DetId/i");
270  hitstree->Branch("Nhits", &nhits, "Nhits/i");
271  hitstree->Branch("Noverlaps", &noverlaps, "Noverlaps/i");
272  hitstree->Branch("SubDet", &subdet, "SubDet/I");
273  hitstree->Branch("Layer", &layer, "Layer/i");
274  hitstree->Branch("is2D", &is2D, "is2D/B");
275  hitstree->Branch("isStereo", &isStereo, "isStereo/B");
276  hitstree->Branch("posX", &posX, "posX/F");
277  hitstree->Branch("posY", &posY, "posY/F");
278  hitstree->Branch("posZ", &posZ, "posZ/F");
279  hitstree->Branch("posR", &posR, "posR/F");
280  hitstree->Branch("posEta", &posEta, "posEta/F");
281  hitstree->Branch("posPhi", &posPhi, "posPhi/F");
282 
283  /*
284  TTree *overlapstree=new TTree("OverlapHitMap","Maps of Overlaps used for Alignment");
285  hitstree->Branch("DetId", &id , "DetId/i");
286  hitstree->Branch("NOverlaps", &nhits , "Nhits/i");
287  hitstree->Branch("SubDet", &subdet, "SubDet/I");
288  hitstree->Branch("Layer", &layer, "Layer/i");
289  hitstree->Branch("is2D" , &is2D, "is2D/B");
290  hitstree->Branch("isStereo",&isStereo,"isStereo/B");
291  hitstree->Branch("posX", &posX, "posX/F");
292  hitstree->Branch("posY", &posY, "posY/F");
293  hitstree->Branch("posZ", &posZ, "posZ/F");
294  hitstree->Branch("posR", &posR, "posR/F");
295  hitstree->Branch("posEta", &posEta, "posEta/F");
296  hitstree->Branch("posPhi", &posPhi, "posPhi/F");
297  */
298 
299  std::unique_ptr<AlignableTracker> theAliTracker =
300  std::make_unique<AlignableTracker>(trackerGeometry_.get(), trackerTopology_.get());
301  const auto &Detunitslist = theAliTracker->deepComponents();
302  int ndetunits = Detunitslist.size();
303  edm::LogInfo("AlignmentStats") << "Number of DetUnits in the AlignableTracker: " << ndetunits << std::endl;
304 
305  for (int det_cnt = 0; det_cnt < ndetunits; ++det_cnt) {
306  //re-initialize for safety
307  id = 0;
308  nhits = 0;
309  noverlaps = 0;
310  posX = -99999.0;
311  posY = -77777.0;
312  posZ = -88888.0;
313  posEta = -6666.0;
314  posPhi = -5555.0;
315  posR = -4444.0;
316  subdet = 0;
317  layer = 0;
318  is2D = false;
319  isStereo = false;
320 
321  //if detunit in vector is found also in the map, look for how many hits were collected
322  //and save in the tree this number
323  id = static_cast<uint32_t>(Detunitslist[det_cnt]->id());
324  if (hitmap_.find(id) != hitmap_.end()) {
325  nhits = hitmap_[id];
326  }
327  //if not, save nhits=0
328  else {
329  nhits = 0;
330  hitmap_.insert(pair<uint32_t, uint32_t>(id, 0));
331  }
332 
333  if (overlapmap_.find(id) != overlapmap_.end()) {
334  noverlaps = overlapmap_[id];
335  }
336  //if not, save nhits=0
337  else {
338  noverlaps = 0;
339  overlapmap_.insert(pair<uint32_t, uint32_t>(id, 0));
340  }
341 
342  //take other geometrical infos from the det
343  posX = Detunitslist[det_cnt]->globalPosition().x();
344  posY = Detunitslist[det_cnt]->globalPosition().y();
345  posZ = Detunitslist[det_cnt]->globalPosition().z();
346 
347  align::GlobalVector vec(posX, posY, posZ);
348  posR = vec.perp();
349  posPhi = vec.phi();
350  posEta = vec.eta();
351  // posPhi = atan2(posY,posX);
352 
353  DetId detid(id);
354  subdet = detid.subdetId();
355 
356  //get layers, petals, etc...
357  if (subdet == PixelSubdetector::PixelBarrel) { //PXB
358 
359  layer = trackerTopology_->pxbLayer(id);
360  is2D = true;
361  isStereo = false;
362  } else if (subdet == PixelSubdetector::PixelEndcap) {
363  layer = trackerTopology_->pxfDisk(id);
364  is2D = true;
365  isStereo = false;
366  } else if (subdet == SiStripDetId::TIB) {
367  layer = trackerTopology_->tibLayer(id);
368  is2D = trackerTopology_->tibIsDoubleSide(id);
369  isStereo = trackerTopology_->tibIsStereo(id);
370  } else if (subdet == SiStripDetId::TID) {
371  layer = trackerTopology_->tidWheel(id);
372  is2D = trackerTopology_->tidIsDoubleSide(id);
373  isStereo = trackerTopology_->tidIsStereo(id);
374  } else if (subdet == SiStripDetId::TOB) {
375  layer = trackerTopology_->tobLayer(id);
376  is2D = trackerTopology_->tobIsDoubleSide(id);
377  isStereo = trackerTopology_->tobIsStereo(id);
378  } else if (subdet == SiStripDetId::TEC) {
379  layer = trackerTopology_->tecWheel(id);
380  is2D = trackerTopology_->tecIsDoubleSide(id);
381  isStereo = trackerTopology_->tecIsStereo(id);
382  } else {
383  edm::LogError("AlignmentStats") << "Detector not belonging neither to pixels nor to strips! Skipping it. SubDet= "
384  << subdet;
385  }
386 
387  //write in the hitstree
388  hitstree->Fill();
389  } //end loop over detunits
390 
391  //save hitstree
392  hitstree->Write();
393  delete hitstree;
394  //delete Detunitslist;
395  hitmap_.clear();
396  overlapmap_.clear();
397  delete hitsfile;
398 }
399 // ========= MODULE DEF ==============
float Eta[MAXTRKS_]
uint32_t prescale_
DetHitMap hitmap_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T perp() const
Definition: PV3DBase.h:69
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > esTokenTkGeo_
static constexpr auto TID
Definition: SiStripDetId.h:38
edm::InputTag src_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
DetHitMap overlapmap_
T eta() const
Definition: PV3DBase.h:73
float Chi2n[MAXTRKS_]
Log< level::Error, false > LogError
ClusterRef cluster() const
std::unique_ptr< TrackerGeometry > trackerGeometry_
constexpr std::array< uint8_t, layerIndexSize > layer
int iEvent
Definition: GenABIO.cc:224
uint32_t tmpPresc_
bool is2D(HitType hitType)
float Phi[MAXTRKS_]
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
bool getData(T &iHolder) const
Definition: EventSetup.h:122
static constexpr auto TOB
Definition: SiStripDetId.h:39
Log< level::Info, false > LogInfo
Definition: DetId.h:17
std::string statsTreeName_
AlignmentStats(const edm::ParameterSet &iConfig)
static const int MAXTRKS_
std::pair< OmniClusterRef, TrackingParticleRef > P
static constexpr auto TIB
Definition: SiStripDetId.h:37
unsigned int ntracks
int Nhits[MAXTRKS_][7]
HLT enums.
std::string hitsTreeName_
float Pt[MAXTRKS_]
void beginJob() override
~AlignmentStats() override
ClusterRef cluster() const
edm::InputTag overlapAM_
std::unique_ptr< TrackerTopology > trackerTopology_
static constexpr auto TEC
Definition: SiStripDetId.h:40
std::string className(const T &t)
Definition: ClassName.h:31
void endJob() override
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > esTokenTTopo_
Our base class.
Definition: SiPixelRecHit.h:23
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override