CMS 3D CMS Logo

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