CMS 3D CMS Logo

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