00001 #include "Alignment/CommonAlignmentMonitor/plugins/AlignmentStats.h"
00002
00003 #include "DataFormats/Common/interface/View.h"
00004 #include "DataFormats/DetId/interface/DetId.h"
00005 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
00006 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00007 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00008 #include "Alignment/CommonAlignment/interface/Alignable.h"
00009 #include "Alignment/CommonAlignment/interface/Utilities.h"
00010
00011 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
00012 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
00013 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
00014 #include "RecoTracker/TransientTrackingRecHit/interface/TSiStripRecHit2DLocalPos.h"
00015 #include "RecoTracker/TransientTrackingRecHit/interface/TSiPixelRecHit.h"
00016
00017 #include "DataFormats/Alignment/interface/AlignmentClusterFlag.h"
00018 #include "DataFormats/Alignment/interface/AliClusterValueMap.h"
00019
00020 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00021 #include "DataFormats/TrackReco/interface/Track.h"
00022 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00023 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00024 #include "Utilities/General/interface/ClassName.h"
00025
00026 using namespace std;
00027
00028 AlignmentStats::AlignmentStats(const edm::ParameterSet &iConfig) :
00029 src_(iConfig.getParameter<edm::InputTag>("src")),
00030 overlapAM_(iConfig.getParameter<edm::InputTag>("OverlapAssoMap")),
00031 keepTrackStats_(iConfig.getParameter<bool>("keepTrackStats")),
00032 keepHitPopulation_(iConfig.getParameter<bool>("keepHitStats")),
00033 statsTreeName_(iConfig.getParameter<string>("TrkStatsFileName")),
00034 hitsTreeName_(iConfig.getParameter<string>("HitStatsFileName")),
00035 prescale_(iConfig.getParameter<uint32_t>("TrkStatsPrescale")),
00036 lastSetup_(nullptr)
00037 {
00038
00039
00040
00041
00042 outtree_=0;
00043
00044 }
00045
00046 AlignmentStats::~AlignmentStats(){
00047
00048 }
00049
00050 void AlignmentStats::beginJob(){
00051
00052
00053 treefile_=new TFile(statsTreeName_.c_str(),"RECREATE");
00054 treefile_->cd();
00055 outtree_=new TTree("AlignmentTrackStats","Statistics of Tracks used for Alignment");
00056
00057
00058
00059 outtree_->Branch("Ntracks" ,&ntracks ,"Ntracks/i");
00060 outtree_->Branch("Run_" ,&run_ ,"Run_Nr/I");
00061 outtree_->Branch("Event" ,&event_ ,"EventNr/I");
00062 outtree_->Branch("Eta" ,&Eta ,"Eta[Ntracks]/F");
00063 outtree_->Branch("Phi" ,&Phi ,"Phi[Ntracks]/F");
00064 outtree_->Branch("P" ,&P ,"P[Ntracks]/F");
00065 outtree_->Branch("Pt" ,&Pt ,"Pt[Ntracks]/F");
00066 outtree_->Branch("Chi2n" ,&Chi2n ,"Chi2n[Ntracks]/F");
00067 outtree_->Branch("Nhits" ,&Nhits ,"Nhits[Ntracks][7]/I");
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 tmpPresc_=prescale_;
00078 firstEvent_=true;
00079
00080
00081
00082
00083 }
00084
00085
00086 void AlignmentStats::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup){
00087
00088 lastSetup_ = &iSetup;
00089
00090
00091
00092 if(firstEvent_){
00093 edm::ESHandle<TrackerGeometry> tmpTkGeometry;
00094 iSetup.get<TrackerDigiGeometryRecord>().get(tmpTkGeometry);
00095 trackerGeometry_=&(*tmpTkGeometry);
00096 firstEvent_=false;
00097 }
00098
00099
00100
00101
00102 edm::Handle<reco::TrackCollection> Tracks;
00103 iEvent.getByLabel(src_, Tracks);
00104
00105
00106 edm::Handle<AliClusterValueMap> hMap;
00107 iEvent.getByLabel(overlapAM_, hMap);
00108 const AliClusterValueMap &OverlapMap=*hMap;
00109
00110
00111 run_=1;
00112 event_=1;
00113 ntracks=0;
00114 run_=iEvent.id().run();
00115 event_=iEvent.id().event();
00116 ntracks=Tracks->size();
00117
00118
00119 unsigned int trk_cnt=0;
00120
00121 for(int j=0;j<MAXTRKS_;j++){
00122 Eta[j]=-9999.0;
00123 Phi[j]=-8888.0;
00124 P[j] =-7777.0;
00125 Pt[j] =-6666.0;
00126 Chi2n[j]=-2222.0;
00127 for(int k=0;k<7;k++){
00128 Nhits[j][k]=0;
00129 }
00130 }
00131
00132
00133
00134
00135 for(std::vector<reco::Track>::const_iterator ittrk = Tracks->begin(), edtrk = Tracks->end(); ittrk != edtrk; ++ittrk){
00136 Eta[trk_cnt]=ittrk->eta();
00137 Phi[trk_cnt]=ittrk->phi();
00138 Chi2n[trk_cnt]=ittrk->normalizedChi2();
00139 P[trk_cnt]=ittrk->p();
00140 Pt[trk_cnt]=ittrk->pt();
00141 Nhits[trk_cnt][0]=ittrk->numberOfValidHits();
00142
00143
00144
00145 int nhit=0;
00146
00147
00148 for (trackingRecHit_iterator ith = ittrk->recHitsBegin(), edh = ittrk->recHitsEnd(); ith != edh; ++ith) {
00149
00150 const TrackingRecHit *hit = ith->get();
00151 if(! hit->isValid())continue;
00152 DetId detid = hit->geographicalId();
00153 int subDet = detid.subdetId();
00154 uint32_t rawId = hit->geographicalId().rawId();
00155
00156
00157
00158
00159 DetHitMap::iterator mapiter;
00160 mapiter= hitmap_.find(rawId);
00161 if(mapiter!=hitmap_.end() ){
00162
00163 ++(hitmap_[rawId]);
00164 }
00165 else{
00166 hitmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
00167 }
00168
00169 AlignmentClusterFlag inval;
00170
00171 bool hitInPixel= (subDet==PixelSubdetector::PixelBarrel)||(subDet==PixelSubdetector::PixelEndcap);
00172 bool hitInStrip=(subDet==SiStripDetId::TIB) || (subDet==SiStripDetId::TID) ||(subDet==SiStripDetId::TOB) ||(subDet==SiStripDetId::TEC);
00173
00174 if(!(hitInPixel||hitInStrip)){
00175
00176 edm::LogError("AlignmentStats")<<"Hit not belonging neither to pixels nor to strips! Skipping it. SubDet= "<<subDet;
00177 continue;
00178 }
00179
00180
00181 if (hitInStrip){
00182 const std::type_info &type = typeid(*hit);
00183
00184 if (type == typeid(SiStripRecHit1D)) {
00185
00186 const SiStripRecHit1D* striphit=dynamic_cast<const SiStripRecHit1D*>(hit);
00187 if(striphit!=0){
00188 SiStripRecHit1D::ClusterRef stripclust(striphit->cluster());
00189 inval = OverlapMap[stripclust];
00190 }
00191 else{
00192
00193 throw cms::Exception("NullPointerError")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit1D failed! TypeId of the RecHit: "<<className(*hit);
00194 }
00195 }
00196 if (type == typeid(SiStripRecHit2D)) {
00197 const SiStripRecHit2D* striphit=dynamic_cast<const SiStripRecHit2D*>(hit);
00198 if(striphit!=0){
00199 SiStripRecHit2D::ClusterRef stripclust(striphit->cluster());
00200 inval = OverlapMap[stripclust];
00201
00202 }
00203 else{
00204 throw cms::Exception("NullPointerError")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit2D failed! TypeId of the RecHit: "<<className(*hit);
00205
00206 }
00207 }
00208
00209 }
00210 else{
00211 const SiPixelRecHit* pixelhit= dynamic_cast<const SiPixelRecHit*>(hit);
00212 if(pixelhit!=0){
00213 SiPixelClusterRefNew pixclust(pixelhit->cluster());
00214 inval = OverlapMap[pixclust];
00215
00216 }
00217 else{
00218 edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Pixel RecHit failed! TypeId of the RecHit: "<<className(*hit);
00219 }
00220 }
00221
00222
00223 bool isOverlapHit(inval.isOverlap());
00224
00225 if( isOverlapHit ){
00226
00227 DetHitMap::iterator overlapiter;
00228 overlapiter=overlapmap_.find(rawId);
00229
00230 if(overlapiter!=overlapmap_.end() ){
00231 overlapmap_[rawId]=overlapmap_[rawId]+1;
00232 }
00233 else{
00234 overlapmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
00235 }
00236 }
00237
00238
00239
00240
00241 int subdethit= static_cast<int>(hit->geographicalId().subdetId());
00242
00243 Nhits[trk_cnt][subdethit]=Nhits[trk_cnt][subdethit]+1;
00244 nhit++;
00245 }
00246 trk_cnt++;
00247
00248 }
00249
00250
00251
00252 tmpPresc_--;
00253 if(tmpPresc_<1){
00254 outtree_->Fill();
00255 tmpPresc_=prescale_;
00256 }
00257 if(trk_cnt!=ntracks)edm::LogError("AlignmentStats")<<"\nERROR! trk_cnt="<<trk_cnt<<" ntracks="<<ntracks;
00258 trk_cnt=0;
00259
00260 return;
00261 }
00262
00263 void AlignmentStats::endJob(){
00264
00265 treefile_->cd();
00266 edm::LogInfo("AlignmentStats")<<"Writing out the TrackStatistics in "<<gDirectory->GetPath()<<std::endl;
00267 outtree_->Write();
00268 delete outtree_;
00269
00270
00271
00272 TFile *hitsfile=new TFile(hitsTreeName_.c_str(),"RECREATE");
00273 hitsfile->cd();
00274 TTree *hitstree=new TTree("AlignmentHitMap","Maps of Hits used for Alignment");
00275
00276 unsigned int id=0,nhits=0,noverlaps=0;
00277 float posX(-99999.0),posY(-77777.0),posZ(-88888.0);
00278 float posEta(-6666.0),posPhi(-5555.0),posR(-4444.0);
00279 int subdet=0;
00280 unsigned int layer=0;
00281 bool is2D=false,isStereo=false;
00282 hitstree->Branch("DetId", &id , "DetId/i");
00283 hitstree->Branch("Nhits", &nhits , "Nhits/i");
00284 hitstree->Branch("Noverlaps",&noverlaps,"Noverlaps/i");
00285 hitstree->Branch("SubDet", &subdet, "SubDet/I");
00286 hitstree->Branch("Layer", &layer, "Layer/i");
00287 hitstree->Branch("is2D" , &is2D, "is2D/B");
00288 hitstree->Branch("isStereo", &isStereo, "isStereo/B");
00289 hitstree->Branch("posX", &posX, "posX/F");
00290 hitstree->Branch("posY", &posY, "posY/F");
00291 hitstree->Branch("posZ", &posZ, "posZ/F");
00292 hitstree->Branch("posR", &posR, "posR/F");
00293 hitstree->Branch("posEta", &posEta, "posEta/F");
00294 hitstree->Branch("posPhi", &posPhi, "posPhi/F");
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 edm::ESHandle<TrackerTopology> tTopoHandle;
00314 lastSetup_->get<IdealGeometryRecord>().get(tTopoHandle);
00315 const TrackerTopology* const tTopo = tTopoHandle.product();
00316
00317 AlignableTracker* theAliTracker=new AlignableTracker(&(*trackerGeometry_), tTopo);
00318 const std::vector<Alignable*>& Detunitslist=theAliTracker->deepComponents();
00319 int ndetunits=Detunitslist.size();
00320 edm::LogInfo("AlignmentStats")<<"Number of DetUnits in the AlignableTracker: "<< ndetunits<<std::endl;
00321
00322 for(int det_cnt=0;det_cnt< ndetunits;++det_cnt){
00323
00324
00325 id=0;
00326 nhits=0;
00327 noverlaps=0;
00328 posX=-99999.0;
00329 posY=-77777.0;
00330 posZ=-88888.0;
00331 posEta=-6666.0;
00332 posPhi=-5555.0;
00333 posR=-4444.0;
00334 subdet=0;
00335 layer=0;
00336 is2D=false;
00337 isStereo=false;
00338
00339
00340
00341 id=static_cast <uint32_t>( Detunitslist[det_cnt]->id() );
00342 if( hitmap_.find(id) != hitmap_.end() ){
00343 nhits=hitmap_[id];
00344 }
00345
00346 else{
00347 nhits=0;
00348 hitmap_.insert(pair<uint32_t, uint32_t>(id, 0));
00349 }
00350
00351 if( overlapmap_.find(id) != overlapmap_.end() ){
00352 noverlaps=overlapmap_[id];
00353 }
00354
00355 else{
00356 noverlaps=0;
00357 overlapmap_.insert(pair<uint32_t, uint32_t>(id, 0));
00358 }
00359
00360
00361 posX= Detunitslist[det_cnt]->globalPosition().x();
00362 posY= Detunitslist[det_cnt]->globalPosition().y();
00363 posZ= Detunitslist[det_cnt]->globalPosition().z();
00364
00365 align::GlobalVector vec(posX,posY,posZ);
00366 posR = vec.perp();
00367 posPhi = vec.phi();
00368 posEta = vec.eta();
00369
00370
00371 DetId detid(id);
00372 subdet=detid.subdetId();
00373
00374
00375 if(subdet==PixelSubdetector::PixelBarrel){
00376
00377 layer=tTopo->pxbLayer(id);
00378 is2D=true;
00379 isStereo=false;
00380 }
00381 else if(subdet==PixelSubdetector::PixelEndcap){
00382
00383 layer=tTopo->pxfDisk(id);
00384 is2D=true;
00385 isStereo=false;
00386 }
00387 else if(subdet==SiStripDetId::TIB){
00388
00389 layer=tTopo->tibLayer(id);
00390 is2D=tTopo->tibIsDoubleSide(id);
00391 isStereo=tTopo->tibIsStereo(id);
00392 }
00393 else if(subdet==SiStripDetId::TID){
00394
00395 layer=tTopo->tidWheel(id);
00396 is2D=tTopo->tidIsDoubleSide(id);
00397 isStereo=tTopo->tidIsStereo(id);
00398 }
00399 else if(subdet==SiStripDetId::TOB){
00400
00401 layer=tTopo->tobLayer(id);
00402 is2D=tTopo->tobIsDoubleSide(id);
00403 isStereo=tTopo->tobIsStereo(id);
00404 }
00405 else if(subdet==SiStripDetId::TEC){
00406
00407 layer=tTopo->tecWheel(id);
00408 is2D=tTopo->tecIsDoubleSide(id);
00409 isStereo=tTopo->tecIsStereo(id);
00410 }
00411 else{
00412 edm::LogError("AlignmentStats")<<"Detector not belonging neither to pixels nor to strips! Skipping it. SubDet= "<<subdet;
00413 }
00414
00415
00416 hitstree->Fill();
00417 }
00418
00419
00420
00421 hitstree->Write();
00422 delete hitstree;
00423
00424 hitmap_.clear();
00425 overlapmap_.clear();
00426 delete hitsfile;
00427 }
00428
00429 #include "FWCore/PluginManager/interface/ModuleDef.h"
00430 #include "FWCore/Framework/interface/MakerMacros.h"
00431 DEFINE_FWK_MODULE(AlignmentStats);