CMS 3D CMS Logo

Public Member Functions | Private Types | Private Attributes | Static Private Attributes

AlignmentStats Class Reference

#include <AlignmentStats.h>

Inheritance diagram for AlignmentStats:
edm::EDAnalyzer

List of all members.

Public Member Functions

 AlignmentStats (const edm::ParameterSet &iConfig)
virtual void analyze (const edm::Event &iEvent, const edm::EventSetup &iSetup)
void beginJob ()
void endJob ()
 ~AlignmentStats ()

Private Types

typedef map< uint32_t, uint32_t > DetHitMap

Private Attributes

float Chi2n [MAXTRKS_]
float Eta [MAXTRKS_]
int event_
bool firstEvent_
DetHitMap hitmap_
std::string hitsTreeName_
bool keepHitPopulation_
bool keepTrackStats_
int Nhits [MAXTRKS_][7]
unsigned int ntracks
TTree * outtree_
edm::InputTag overlapAM_
DetHitMap overlapmap_
float P [MAXTRKS_]
float Phi [MAXTRKS_]
uint32_t prescale_
float Pt [MAXTRKS_]
int run_
edm::InputTag src_
std::string statsTreeName_
uint32_t tmpPresc_
const TrackerGeometrytrackerGeometry_
TFile * treefile_

Static Private Attributes

static const int MAXTRKS_ = 200

Detailed Description

Definition at line 27 of file AlignmentStats.h.


Member Typedef Documentation

typedef map<uint32_t,uint32_t> AlignmentStats::DetHitMap [private]

Definition at line 60 of file AlignmentStats.h.


Constructor & Destructor Documentation

AlignmentStats::AlignmentStats ( const edm::ParameterSet iConfig)

Definition at line 32 of file AlignmentStats.cc.

References outtree_.

                                                             :
  src_(iConfig.getParameter<edm::InputTag>("src")),
  overlapAM_(iConfig.getParameter<edm::InputTag>("OverlapAssoMap")),
  keepTrackStats_(iConfig.getParameter<bool>("keepTrackStats")),
  keepHitPopulation_(iConfig.getParameter<bool>("keepHitStats")),
  statsTreeName_(iConfig.getParameter<string>("TrkStatsFileName")),
  hitsTreeName_(iConfig.getParameter<string>("HitStatsFileName")),
  prescale_(iConfig.getParameter<uint32_t>("TrkStatsPrescale"))
{

  //sanity checks

  //init
  outtree_=0;

}//end constructor
AlignmentStats::~AlignmentStats ( )

Definition at line 49 of file AlignmentStats.cc.

                               {
                               //
}

Member Function Documentation

void AlignmentStats::analyze ( const edm::Event iEvent,
const edm::EventSetup iSetup 
) [virtual]

Implements edm::EDAnalyzer.

Definition at line 89 of file AlignmentStats.cc.

References Chi2n, className(), cond::rpcobgas::detid, Eta, edm::EventID::event(), event_, Exception, firstEvent_, TrackingRecHit::geographicalId(), edm::EventSetup::get(), edm::Event::getByLabel(), hitmap_, edm::EventBase::id(), AlignmentClusterFlag::isOverlap(), TrackingRecHit::isValid(), j, gen::k, MAXTRKS_, Nhits, ntracks, outtree_, overlapAM_, overlapmap_, P, Phi, GeomDetEnumerators::PixelBarrel, PixelSubdetector::PixelEndcap, prescale_, Pt, DetId::rawId(), edm::EventID::run(), run_, src_, DetId::subdetId(), SiStripDetId::TEC, sistripsummary::TIB, SiStripDetId::TID, tmpPresc_, sistripsummary::TOB, and trackerGeometry_.

                                                                               {

  //load list of detunits needed then in endJob
 
  if(firstEvent_){
    edm::ESHandle<TrackerGeometry> tmpTkGeometry;
    iSetup.get<TrackerDigiGeometryRecord>().get(tmpTkGeometry);
    trackerGeometry_=&(*tmpTkGeometry);
    firstEvent_=false;
  }


  //take trajectories and tracks to loop on
  // edm::Handle<TrajTrackAssociationCollection> TrackAssoMap;
  edm::Handle<reco::TrackCollection> Tracks;
  iEvent.getByLabel(src_, Tracks);

  //take overlap HitAssomap
  edm::Handle<AliClusterValueMap> hMap;
  iEvent.getByLabel(overlapAM_, hMap);
  const AliClusterValueMap &OverlapMap=*hMap;

  // Initialise
  run_=1;
  event_=1;
  ntracks=0;
  run_=iEvent.id().run();
  event_=iEvent.id().event();
  ntracks=Tracks->size();
  //  if(ntracks>1) std::cout<<"~~~~~~~~~~~~\n For this event processing "<<ntracks<<" tracks"<<std::endl;

  unsigned int trk_cnt=0;
  
  for(int j=0;j<MAXTRKS_;j++){
    Eta[j]=-9999.0;
    Phi[j]=-8888.0;
    P[j]  =-7777.0;
    Pt[j] =-6666.0;
    Chi2n[j]=-2222.0;
    for(int k=0;k<7;k++){
      Nhits[j][k]=0;
    }
  }
  
  // int npxbhits=0;

  //loop on tracks
  for(std::vector<reco::Track>::const_iterator ittrk = Tracks->begin(), edtrk = Tracks->end(); ittrk != edtrk; ++ittrk){
    Eta[trk_cnt]=ittrk->eta();
    Phi[trk_cnt]=ittrk->phi();
    Chi2n[trk_cnt]=ittrk->normalizedChi2();
    P[trk_cnt]=ittrk->p();
    Pt[trk_cnt]=ittrk->pt();
    Nhits[trk_cnt][0]=ittrk->numberOfValidHits();
 
    //   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;
   
    int nhit=0;
    //loop on tracking rechits
    //std::cout << "   loop on hits of track #" << (itt - tracks->begin()) << std::endl;
    for (trackingRecHit_iterator ith = ittrk->recHitsBegin(), edh = ittrk->recHitsEnd(); ith != edh; ++ith) {

      const TrackingRecHit *hit = ith->get(); // ith is an iterator on edm::Ref to rechit
      if(! hit->isValid())continue;
      DetId detid = hit->geographicalId();
      int subDet = detid.subdetId();
      uint32_t rawId = hit->geographicalId().rawId();

      //  if(subDet==1)npxbhits++;

      //look if you find this detid in the map
      DetHitMap::iterator mapiter;
      mapiter= hitmap_.find(rawId);
      if(mapiter!=hitmap_.end() ){//present, increase its value by one
        //      hitmap_[rawId]=hitmap_[rawId]+1;
        ++(hitmap_[rawId]);
      }
      else{//not present, let's add this key to the map with value=1
        hitmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
      }
      
      AlignmentClusterFlag inval;

      bool hitInPixel= (subDet==PixelSubdetector::PixelBarrel)||(subDet==PixelSubdetector::PixelEndcap);
      bool hitInStrip=(subDet==SiStripDetId::TIB) || (subDet==SiStripDetId::TID) ||(subDet==SiStripDetId::TOB) ||(subDet==SiStripDetId::TEC);

      if(!(hitInPixel||hitInStrip)){
        //skip only this hit, don't stop everything throwing an exception
        edm::LogError("AlignmentStats")<<"Hit not belonging neither to pixels nor to strips! Skipping it. SubDet= "<<subDet;
        continue;
      }

      //check also if the hit is an overlap. If yes fill a dedicated hitmap
      if (hitInStrip){
        const std::type_info &type = typeid(*hit);

        if (type == typeid(SiStripRecHit1D)) {
          //Notice the difference respect to when one loops on Trajectories: the recHit is a TrackingRecHit and not a TransientTrackingRecHit
          const SiStripRecHit1D* striphit=dynamic_cast<const  SiStripRecHit1D*>(hit);
          if(striphit!=0){
            SiStripRecHit1D::ClusterRef stripclust(striphit->cluster());
            inval = OverlapMap[stripclust];
          }
          else{
            //  edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit1D failed!   TypeId of the RecHit: "<<className(*hit);
            throw cms::Exception("NullPointerError")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit1D failed!   TypeId of the RecHit: "<<className(*hit);
          }
        }//end if sistriprechit1D
        if (type == typeid(SiStripRecHit2D)) {
          const SiStripRecHit2D* striphit=dynamic_cast<const  SiStripRecHit2D*>(hit);
          if(striphit!=0){
            SiStripRecHit2D::ClusterRef stripclust(striphit->cluster());
            inval = OverlapMap[stripclust];
            //cout<<"Taken the Strip Cluster with ProdId "<<stripclust.id() <<"; the Value in the map is "<<inval<<"  (DetId is "<<hit->geographicalId().rawId()<<")"<<endl;
          }
          else{
            throw cms::Exception("NullPointerError")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit2D failed!   TypeId of the RecHit: "<<className(*hit);
            //    edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Strip RecHit2D failed!   TypeId of the RecHit: "<<className(*hit);
          }
        }//end if sistriprechit2D

      }//end if hit in Strips
      else{
        const SiPixelRecHit* pixelhit= dynamic_cast<const SiPixelRecHit*>(hit);
        if(pixelhit!=0){
          SiPixelClusterRefNew pixclust(pixelhit->cluster());
          inval = OverlapMap[pixclust];
          //cout<<"Taken the Pixel Cluster with ProdId "<<pixclust.id() <<"; the Value in the map is "<<inval<<"  (DetId is "<<hit->geographicalId().rawId()<<")"<<endl;
        }
        else{
          edm::LogError("AlignmentStats")<<"ERROR in <AlignmentStats::analyze>: Dynamic cast of Pixel RecHit failed!   TypeId of the RecHit: "<<className(*hit);
        }
      }//end else hit is in Pixel


      bool isOverlapHit(inval.isOverlap());
      
      if( isOverlapHit ){
        //cout<<"This hit is an overlap !"<<endl;
        DetHitMap::iterator overlapiter;
        overlapiter=overlapmap_.find(rawId);
        
        if(overlapiter!=overlapmap_.end() ){//the det already collected at least an overlap, increase its value by one
            overlapmap_[rawId]=overlapmap_[rawId]+1;
        }
        else{//first overlap on det unit, let's add it to the map
          overlapmap_.insert(pair<uint32_t, uint32_t>(rawId, 1));
        }
      }//end if the hit is an overlap
      



      int subdethit= static_cast<int>(hit->geographicalId().subdetId());
      // if(ntracks>1)std::cout<<"Hit in SubDet="<<subdethit<<std::endl;
      Nhits[trk_cnt][subdethit]=Nhits[trk_cnt][subdethit]+1;
      nhit++;
    }//end loop on trackingrechits
    trk_cnt++;

  }//end loop on tracks

  //  cout<<"Total number of pixel hits is "<<npxbhits<<endl;

  tmpPresc_--;
  if(tmpPresc_<1){
    outtree_->Fill();
    tmpPresc_=prescale_;
  }
  if(trk_cnt!=ntracks)edm::LogError("AlignmentStats")<<"\nERROR! trk_cnt="<<trk_cnt<<"   ntracks="<<ntracks;
  trk_cnt=0;

  return;
}
void AlignmentStats::beginJob ( void  ) [virtual]

Reimplemented from edm::EDAnalyzer.

Definition at line 53 of file AlignmentStats.cc.

References Chi2n, Eta, event_, firstEvent_, Nhits, ntracks, outtree_, P, Phi, prescale_, Pt, run_, statsTreeName_, tmpPresc_, and treefile_.

                             {// const edm::EventSetup &iSetup

  //book track stats tree
  treefile_=new TFile(statsTreeName_.c_str(),"RECREATE");
  treefile_->cd();
  outtree_=new TTree("AlignmentTrackStats","Statistics of Tracks used for Alignment");
  // int nHitsinPXB[MAXTRKS_], nHitsinPXE[MAXTRKS_], nHitsinTEC[MAXTRKS_], nHitsinTIB[MAXTRKS_],nHitsinTOB[MAXTRKS_],nHitsinTID[MAXTRKS_];
  

  outtree_->Branch("Ntracks"    ,&ntracks ,"Ntracks/i");
  outtree_->Branch("Run_"        ,&run_     ,"Run_Nr/I");
  outtree_->Branch("Event"      ,&event_   ,"EventNr/I");
  outtree_->Branch("Eta"        ,&Eta     ,"Eta[Ntracks]/F");
  outtree_->Branch("Phi"        ,&Phi     ,"Phi[Ntracks]/F");
  outtree_->Branch("P"          ,&P       ,"P[Ntracks]/F");
  outtree_->Branch("Pt"         ,&Pt      ,"Pt[Ntracks]/F");
  outtree_->Branch("Chi2n"      ,&Chi2n   ,"Chi2n[Ntracks]/F");
  outtree_->Branch("Nhits"      ,&Nhits   ,"Nhits[Ntracks][7]/I");
  /*
    outtree_->Branch("NhitsPXB"       , ,);
    outtree_->Branch("NhitsPXE"       , ,);
    outtree_->Branch("NhitsTIB"       , ,);
    outtree_->Branch("NhitsTID"       , ,);
    outtree_->Branch("NhitsTOB"       , ,);
    outtree_->Branch("NhitsTOB"       , ,);
  */

  tmpPresc_=prescale_;
  firstEvent_=true;

  //load the tracker geometry from the EventSetup
  //  iSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometry_);
 
}//end beginJob
void AlignmentStats::endJob ( void  ) [virtual]

Reimplemented from edm::EDAnalyzer.

Definition at line 264 of file AlignmentStats.cc.

References Alignable::deepComponents(), cond::rpcobgas::detid, PXFDetId::disk(), TIDDetId::diskNumber(), PV3DBase< T, PVType, FrameType >::eta(), hitmap_, hitsTreeName_, TIBDetId::isDoubleSide(), TOBDetId::isDoubleSide(), TECDetId::isDoubleSide(), TIDDetId::isDoubleSide(), TIDDetId::isStereo(), TIBDetId::isStereo(), TOBDetId::isStereo(), TECDetId::isStereo(), PXBDetId::layer(), TOBDetId::layerNumber(), TIBDetId::layerNumber(), outtree_, overlapmap_, PV3DBase< T, PVType, FrameType >::perp(), PV3DBase< T, PVType, FrameType >::phi(), PixelSubdetector::PixelBarrel, PixelSubdetector::PixelEndcap, DetId::subdetId(), SiStripDetId::TEC, SiStripDetId::TIB, SiStripDetId::TID, SiStripDetId::TOB, trackerGeometry_, treefile_, and TECDetId::wheelNumber().

                           {

  treefile_->cd();
  edm::LogInfo("AlignmentStats")<<"Writing out the TrackStatistics in "<<gDirectory->GetPath()<<std::endl;
  outtree_->Write();
  delete outtree_;

  //create tree with hit maps (hitstree)
  //book track stats tree
  TFile *hitsfile=new TFile(hitsTreeName_.c_str(),"RECREATE");
  hitsfile->cd();
  TTree *hitstree=new TTree("AlignmentHitMap","Maps of Hits used for Alignment");

  unsigned int id=0,nhits=0,noverlaps=0;
  float posX(-99999.0),posY(-77777.0),posZ(-88888.0);
  float posEta(-6666.0),posPhi(-5555.0),posR(-4444.0);
  int subdet=0;
  unsigned int layer=0; 
  bool is2D=false,isStereo=false;
  hitstree->Branch("DetId",    &id ,      "DetId/i");
  hitstree->Branch("Nhits",    &nhits ,   "Nhits/i");
  hitstree->Branch("Noverlaps",&noverlaps,"Noverlaps/i");
  hitstree->Branch("SubDet",   &subdet,   "SubDet/I");
  hitstree->Branch("Layer",    &layer,    "Layer/i");
  hitstree->Branch("is2D" ,    &is2D,     "is2D/B");
  hitstree->Branch("isStereo", &isStereo, "isStereo/B");
  hitstree->Branch("posX",     &posX,     "posX/F");
  hitstree->Branch("posY",     &posY,     "posY/F");
  hitstree->Branch("posZ",     &posZ,     "posZ/F");
  hitstree->Branch("posR",     &posR,     "posR/F");
  hitstree->Branch("posEta",   &posEta,   "posEta/F");
  hitstree->Branch("posPhi",   &posPhi,   "posPhi/F");

  /*
  TTree *overlapstree=new TTree("OverlapHitMap","Maps of Overlaps used for Alignment");
  hitstree->Branch("DetId",   &id ,     "DetId/i");
  hitstree->Branch("NOverlaps",   &nhits ,  "Nhits/i");
  hitstree->Branch("SubDet",  &subdet,  "SubDet/I");
  hitstree->Branch("Layer",   &layer,   "Layer/i");
  hitstree->Branch("is2D" ,   &is2D,    "is2D/B");
  hitstree->Branch("isStereo",&isStereo,"isStereo/B");
  hitstree->Branch("posX",    &posX,    "posX/F");
  hitstree->Branch("posY",    &posY,    "posY/F");
  hitstree->Branch("posZ",    &posZ,    "posZ/F");
  hitstree->Branch("posR",    &posR,    "posR/F");
  hitstree->Branch("posEta",  &posEta,  "posEta/F");
  hitstree->Branch("posPhi",  &posPhi,  "posPhi/F");
  */


  AlignableTracker* theAliTracker=new AlignableTracker(&(*trackerGeometry_));
  const std::vector<Alignable*>& Detunitslist=theAliTracker->deepComponents();
  int ndetunits=Detunitslist.size();
  edm::LogInfo("AlignmentStats")<<"Number of DetUnits in the AlignableTracker: "<< ndetunits<<std::endl;

  for(int det_cnt=0;det_cnt< ndetunits;++det_cnt){
    
    //re-initialize for safety
    id=0;
    nhits=0;
    noverlaps=0;
    posX=-99999.0;
    posY=-77777.0;
    posZ=-88888.0;
    posEta=-6666.0;
    posPhi=-5555.0;
    posR=-4444.0;
    subdet=0;
    layer=0; 
    is2D=false;
    isStereo=false;

    //if detunit in vector is found also in the map, look for how many hits were collected 
    //and save in the tree this number
    id=static_cast <uint32_t>( Detunitslist[det_cnt]->id() );
    if( hitmap_.find(id) != hitmap_.end() ){
      nhits=hitmap_[id];
    }
    //if not, save nhits=0
    else{
      nhits=0;
      hitmap_.insert(pair<uint32_t, uint32_t>(id, 0));
    }

    if( overlapmap_.find(id) != overlapmap_.end() ){
      noverlaps=overlapmap_[id];
    }
    //if not, save nhits=0
    else{
      noverlaps=0;
      overlapmap_.insert(pair<uint32_t, uint32_t>(id, 0));
    }

  //take other geometrical infos from the det
   posX= Detunitslist[det_cnt]->globalPosition().x();
   posY= Detunitslist[det_cnt]->globalPosition().y();
   posZ= Detunitslist[det_cnt]->globalPosition().z();

   align::GlobalVector vec(posX,posY,posZ);
   posR = vec.perp();
   posPhi = vec.phi();
   posEta = vec.eta();
   //   posPhi = atan2(posY,posX);      

   DetId detid(id);
   subdet=detid.subdetId();

   //get layers, petals, etc...
   if(subdet==PixelSubdetector::PixelBarrel){//PXB
     PXBDetId pxbdet=(id);
     layer=pxbdet.layer();
     is2D=true;
     isStereo=false;
   }
   else if(subdet==PixelSubdetector::PixelEndcap){
     PXFDetId pxfdet(id);
     layer=pxfdet.disk();
     is2D=true;
     isStereo=false;
   }
   else if(subdet==SiStripDetId::TIB){
     TIBDetId tibdet(id);
     layer=tibdet.layerNumber();
     is2D=tibdet.isDoubleSide();
     isStereo=tibdet.isStereo();
   }
   else if(subdet==SiStripDetId::TID){
     TIDDetId tiddet(id);
     layer=tiddet.diskNumber();
     is2D=tiddet.isDoubleSide();
     isStereo=tiddet.isStereo();
   }
   else if(subdet==SiStripDetId::TOB){
     TOBDetId tobdet(id);
     layer=tobdet.layerNumber();
     is2D=tobdet.isDoubleSide();
     isStereo=tobdet.isStereo();
   }
   else if(subdet==SiStripDetId::TEC){
     TECDetId tecdet(id);
     layer=tecdet.wheelNumber();
     is2D=tecdet.isDoubleSide();
     isStereo=tecdet.isStereo();
   }
   else{
     edm::LogError("AlignmentStats")<<"Detector not belonging neither to pixels nor to strips! Skipping it. SubDet= "<<subdet;
   }

  //write in the hitstree
    hitstree->Fill();
  }//end loop over detunits


  //save hitstree
  hitstree->Write();
  delete hitstree;
  //delete Detunitslist;
  hitmap_.clear();
  overlapmap_.clear();
  delete hitsfile;
}

Member Data Documentation

float AlignmentStats::Chi2n[MAXTRKS_] [private]

Definition at line 56 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

float AlignmentStats::Eta[MAXTRKS_] [private]

Definition at line 56 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

int AlignmentStats::event_ [private]

Definition at line 54 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

Definition at line 48 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

Definition at line 61 of file AlignmentStats.h.

Referenced by analyze(), and endJob().

std::string AlignmentStats::hitsTreeName_ [private]

Definition at line 44 of file AlignmentStats.h.

Referenced by endJob().

Definition at line 42 of file AlignmentStats.h.

Definition at line 41 of file AlignmentStats.h.

const int AlignmentStats::MAXTRKS_ = 200 [static, private]

Definition at line 53 of file AlignmentStats.h.

Referenced by analyze().

int AlignmentStats::Nhits[MAXTRKS_][7] [private]

Definition at line 57 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

unsigned int AlignmentStats::ntracks [private]

Definition at line 55 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

TTree* AlignmentStats::outtree_ [private]

Definition at line 52 of file AlignmentStats.h.

Referenced by AlignmentStats(), analyze(), beginJob(), and endJob().

Definition at line 40 of file AlignmentStats.h.

Referenced by analyze().

Definition at line 62 of file AlignmentStats.h.

Referenced by analyze(), and endJob().

float AlignmentStats::P[MAXTRKS_] [private]

Definition at line 56 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

float AlignmentStats::Phi[MAXTRKS_] [private]

Definition at line 56 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

uint32_t AlignmentStats::prescale_ [private]

Definition at line 45 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

float AlignmentStats::Pt[MAXTRKS_] [private]

Definition at line 56 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

int AlignmentStats::run_ [private]

Definition at line 54 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

Definition at line 39 of file AlignmentStats.h.

Referenced by analyze().

std::string AlignmentStats::statsTreeName_ [private]

Definition at line 43 of file AlignmentStats.h.

Referenced by beginJob().

uint32_t AlignmentStats::tmpPresc_ [private]

Definition at line 47 of file AlignmentStats.h.

Referenced by analyze(), and beginJob().

Definition at line 65 of file AlignmentStats.h.

Referenced by analyze(), and endJob().

TFile* AlignmentStats::treefile_ [private]

Definition at line 51 of file AlignmentStats.h.

Referenced by beginJob(), and endJob().