CMS 3D CMS Logo

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