CMS 3D CMS Logo

SiStripLAProfileBooker.cc

Go to the documentation of this file.
00001 #include <string>
00002 #include <iostream>
00003 #include <fstream>
00004 #include <functional>
00005 
00006 
00007 #include "CalibTracker/SiStripLorentzAngle/interface/SiStripLAProfileBooker.h"
00008 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00009 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00010 
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 #include "FWCore/Framework/interface/EventSetup.h"
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00018 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00019 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00020 #include "Geometry/CommonTopologies/interface/StripTopology.h"
00021 
00022 #include "DataFormats/Common/interface/Handle.h"
00023 #include "DataFormats/TrackReco/interface/Track.h"
00024 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00025 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00026 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00027 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00028 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00029 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
00030 #include "DataFormats/TrackerRecHit2D/interface/ProjectedSiStripRecHit2D.h"
00031 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00032 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00033 
00034 #include "DQM/SiStripCommon/interface/ExtractTObject.h"
00035 #include "DQM/SiStripCommon/interface/SiStripHistoId.h"
00036 #include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
00037 #include "DQMServices/Core/interface/DQMStore.h"
00038 
00039 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00040 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00041 
00042 #include <TProfile.h>
00043 #include <TF1.h>
00044 
00045  
00046 class DetIdLess 
00047   : public std::binary_function< const SiStripRecHit2D*,const SiStripRecHit2D*,bool> {
00048 public:
00049   
00050   DetIdLess() {}
00051   
00052   bool operator()( const SiStripRecHit2D* a, const SiStripRecHit2D* b) const {
00053     return *(a->cluster())<*(b->cluster());
00054   }
00055 };
00056   
00057 
00058   //Constructor
00059 
00060 SiStripLAProfileBooker::SiStripLAProfileBooker(edm::ParameterSet const& conf) : 
00061   conf_(conf)
00062 {
00063 }
00064 
00065   //BeginJob
00066 
00067 void SiStripLAProfileBooker::beginJob(const edm::EventSetup& c){
00068 
00069  
00070   //get magnetic field and geometry from ES
00071   edm::ESHandle<MagneticField> esmagfield;
00072   c.get<IdealMagneticFieldRecord>().get(esmagfield);
00073   const MagneticField *  magfield=&(*esmagfield);
00074   
00075   edm::ESHandle<TrackerGeometry> estracker;
00076   c.get<TrackerDigiGeometryRecord>().get(estracker);
00077   tracker=&(*estracker); 
00078 
00079   std::vector<uint32_t> activeDets;
00080   edm::ESHandle<SiStripDetCabling> tkmechstruct=0;
00081   if (conf_.getParameter<bool>("UseStripCablingDB")){ 
00082     c.get<SiStripDetCablingRcd>().get(tkmechstruct);
00083     activeDets.clear();
00084     tkmechstruct->addActiveDetectorsRawIds(activeDets);
00085   }
00086   else {
00087     const TrackerGeometry::DetIdContainer& Id = estracker->detIds();
00088     TrackerGeometry::DetIdContainer::const_iterator Iditer;    
00089     activeDets.clear();
00090     for(Iditer=Id.begin();Iditer!=Id.end();Iditer++){
00091       activeDets.push_back(Iditer->rawId());
00092     }
00093   }
00094 
00095   edm::InputTag TkTag = conf_.getParameter<edm::InputTag>("Tracks");
00096   //Get Ids;
00097   double ModuleRangeMin=conf_.getParameter<double>("ModuleXMin");
00098   double ModuleRangeMax=conf_.getParameter<double>("ModuleXMax");
00099   double TIBRangeMin=conf_.getParameter<double>("TIBXMin");
00100   double TIBRangeMax=conf_.getParameter<double>("TIBXMax");
00101   double TOBRangeMin=conf_.getParameter<double>("TOBXMin");
00102   double TOBRangeMax=conf_.getParameter<double>("TOBXMax");
00103   
00104   hFile = new TFile (conf_.getUntrackedParameter<std::string>("treeName").c_str(), "RECREATE" );
00105   
00106   HitsTree = new TTree("HitsTree", "HitsTree");
00107   
00108   HitsTree->Branch("RunNumber", &RunNumber, "RunNumber/I");
00109   HitsTree->Branch("EventNumber", &EventNumber, "EventNumber/I");
00110   HitsTree->Branch("TanTrackAngle", &TanTrackAngle, "TanTrackAngle/F");
00111   HitsTree->Branch("ClSize", &ClSize, "ClSize/I");
00112   HitsTree->Branch("HitCharge", &HitCharge, "HitCharge/I");
00113   HitsTree->Branch("Hit_Std_Dev", &hit_std_dev, "hit_std_dev/F");
00114   HitsTree->Branch("Type", &Type, "Type/I");
00115   HitsTree->Branch("Layer", &Layer, "Layer/I");
00116   HitsTree->Branch("Wheel", &Wheel, "Wheel/I");
00117   HitsTree->Branch("bw_fw", &bw_fw, "bw_fw/I");
00118   HitsTree->Branch("Ext_Int", &Ext_Int, "Ext_Int/I");
00119   HitsTree->Branch("MonoStereo", &MonoStereo, "MonoStereo/I");
00120   HitsTree->Branch("MagField", &MagField, "MagField/F");
00121   HitsTree->Branch("SignCorrection", &SignCorrection, "SignCorrection/F");
00122   HitsTree->Branch("XGlobal", &XGlobal, "XGlobal/F");
00123   HitsTree->Branch("YGlobal", &YGlobal, "YGlobal/F");
00124   HitsTree->Branch("ZGlobal", &ZGlobal, "ZGlobal/F");
00125   HitsTree->Branch("ParticleCharge", &ParticleCharge, "ParticleCharge/I");
00126   HitsTree->Branch("Momentum", &Momentum, "Momentum/F");
00127   HitsTree->Branch("pt", &pt, "pt/F");
00128   HitsTree->Branch("chi2norm", &chi2norm, "chi2norm/F");
00129   HitsTree->Branch("EtaTrack", &EtaTrack, "EtaTrack/F");
00130   HitsTree->Branch("TrajSize", &trajsize, "trajsize/I");
00131   HitsTree->Branch("HitNr", &HitNr, "HitNr/I");
00132   HitsTree->Branch("HitPerTrack", &HitPerTrack, "HitPerTrack/I");
00133       
00134   // use SistripHistoId for producing histogram id (and title)
00135   SiStripHistoId hidmanager;
00136 
00137   // create SiStripFolderOrganizer
00138   SiStripFolderOrganizer folder_organizer;
00139 
00140   dbe_ = edm::Service<DQMStore>().operator->();
00141   
00142   //get all detids
00143   
00144   MonitorElement * check_histo=dbe_->book1D("CrossCheck","CrossCheck",100,0,100);
00145   histos[1] = check_histo;
00146 
00147   for(std::vector<uint32_t>::const_iterator Id = activeDets.begin(); Id!=activeDets.end(); Id++){
00148 
00149     //  for(Iditer=Id.begin();Iditer!=Id.end();Iditer++){ //loop on detids
00150     DetId Iditero=DetId(*Id);
00151     DetId *Iditer=&Iditero;
00152     if((Iditer->subdetId() == int(StripSubdetector::TIB)) || (Iditer->subdetId() == int(StripSubdetector::TOB))){ //include only barrel
00153 
00154       // create a TProfile for each module
00155       StripSubdetector subid(*Iditer);
00156       std::string hid;
00157       //Mono single sided detectors
00158       LocalPoint p;
00159       const GeomDetUnit * stripdet=dynamic_cast<const GeomDetUnit*>(tracker->idToDet(subid));
00160       if(stripdet==0)continue;
00161       const StripTopology& topol=(StripTopology&)stripdet->topology();
00162       float thickness=stripdet->specificSurface().bounds().thickness();
00163                 
00164       folder_organizer.setDetectorFolder(Iditer->rawId());
00165       hid = hidmanager.createHistoId(TkTag.label().c_str(),"det",Iditer->rawId());
00166       MonitorElement * profile=dbe_->bookProfile(hid,hid,30,ModuleRangeMin,ModuleRangeMax,20,0,5,"");
00167       detparameters *param=new detparameters;
00168       histos[Iditer->rawId()] = profile;
00169       detmap[Iditer->rawId()] = param;
00170       param->thickness = thickness*10000;
00171       param->pitch = topol.localPitch(p)*10000;
00172 
00173 
00174       const GlobalPoint globalp = (stripdet->surface()).toGlobal(p);
00175       GlobalVector globalmagdir = magfield->inTesla(globalp);
00176       param->magfield=(stripdet->surface()).toLocal(globalmagdir);
00177 
00178       profile->setAxisTitle("tan(#theta_{t})",1);
00179       profile->setAxisTitle("Cluster size",2);
00180 
00181       // create a summary histo if it does not exist already
00182       std::string name;
00183       unsigned int layerid;
00184       getlayer(subid,name,layerid);
00185       name+=TkTag.label().c_str();
00186       if(summaryhisto.find(layerid)==(summaryhisto.end())){
00187         folder_organizer.setSiStripFolder();
00188         MonitorElement * summaryprofile=0;
00189         if (subid.subdetId()==int (StripSubdetector::TIB)||subid.subdetId()==int (StripSubdetector::TID))
00190           summaryprofile=dbe_->bookProfile(name,name,30,TIBRangeMin,TIBRangeMax,20,0,5,"");
00191         else if (subid.subdetId()==int (StripSubdetector::TOB)||subid.subdetId()==int (StripSubdetector::TEC))
00192           summaryprofile=dbe_->bookProfile(name,name,30,TOBRangeMin,TOBRangeMax,20,0,5,"");
00193         if(summaryprofile){
00194           detparameters *summaryparam=new detparameters;
00195           summaryhisto[layerid] = summaryprofile;
00196           summarydetmap[layerid] = summaryparam;
00197           summaryparam->thickness = thickness*10000;
00198           summaryparam->pitch = topol.localPitch(p)*10000;
00199           summaryprofile->setAxisTitle("tan(#theta_{t})",1);
00200           summaryprofile->setAxisTitle("Cluster size",2);
00201         }
00202       }
00203       
00204       //tracksnumber=0;
00205       //tracksnumber=dbe_->book1D("TracksNumber","Number of reconstructed tracks",100,0,100);
00206       
00207     } 
00208   } 
00209   
00210   trackcollsize = 0;
00211   trajsize = 0;
00212   RunNumber = 0;
00213   EventNumber = 0;
00214   hitcounter = 0;
00215   hitcounter_2ndloop = 0;
00216   worse_double_hit = 0;
00217   better_double_hit = 0;
00218   eventcounter = 0;
00219   trajcounter = 0;
00220 
00221 }
00222 
00223 SiStripLAProfileBooker::~SiStripLAProfileBooker() {  
00224   detparmap::iterator detpariter;
00225   for( detpariter=detmap.begin(); detpariter!=detmap.end();++detpariter)delete detpariter->second;
00226   for( detpariter=summarydetmap.begin(); detpariter!=summarydetmap.end();++detpariter)delete detpariter->second;
00227   fitmap::iterator  fitpar;
00228   for( fitpar=summaryfits.begin(); fitpar!=summaryfits.end();++fitpar)delete fitpar->second;
00229   delete hFile;
00230 }  
00231 
00232 // Analyzer: Functions that gets called by framework every event
00233 
00234 void SiStripLAProfileBooker::analyze(const edm::Event& e, const edm::EventSetup& es)
00235 {
00236   
00237   RunNumber = e.id().run();
00238   EventNumber = e.id().event();
00239   
00240   eventcounter++;
00241   
00242   //Analysis of Trajectory-RecHits
00243         
00244   edm::InputTag TkTag = conf_.getParameter<edm::InputTag>("Tracks");
00245   
00246   edm::Handle<reco::TrackCollection> trackCollection;
00247   e.getByLabel(TkTag,trackCollection);
00248     
00249   edm::Handle<std::vector<Trajectory> > TrajectoryCollection;
00250   e.getByLabel(TkTag,TrajectoryCollection);
00251   
00252   edm::Handle<TrajTrackAssociationCollection> TrajTrackMap;
00253   e.getByLabel(TkTag, TrajTrackMap);
00254   
00255   const reco::TrackCollection *tracks=trackCollection.product();
00256    
00257   std::map<const SiStripRecHit2D*,std::pair<float,float>,DetIdLess> hitangleassociation;
00258     
00259   trackcollsize=tracks->size();
00260   trajsize=TrajectoryCollection->size();
00261   
00262   edm::LogInfo("SiStripLAProfileBooker::analyze") <<" Number of tracks in event = "<<trackcollsize<<"\n";
00263   edm::LogInfo("SiStripLAProfileBooker::analyze") <<" Number of trajectories in event = "<<trajsize<<"\n";
00264   
00265   TrajTrackAssociationCollection::const_iterator TrajTrackIter;
00266   
00267   //std::vector<Trajectory>::const_iterator theTraj;
00268   
00269   for(TrajTrackIter = TrajTrackMap->begin(); TrajTrackIter!= TrajTrackMap->end(); TrajTrackIter++){ //loop on trajectories
00270     
00271     if(TrajTrackIter->key->foundHits()>=5){
00272     
00273       trajcounter++;
00274     
00275       ParticleCharge = -99;
00276       Momentum = -99;
00277       pt = -99;
00278       chi2norm = -99;
00279       HitPerTrack = -99;
00280       EtaTrack = -99;
00281       
00282       ParticleCharge = TrajTrackIter->val->charge();
00283       pt = TrajTrackIter->val->pt();
00284       Momentum = TrajTrackIter->val->p();
00285       chi2norm = TrajTrackIter->val->normalizedChi2();
00286       EtaTrack = TrajTrackIter->val->eta();
00287       HitPerTrack = TrajTrackIter->key->foundHits();
00288           
00289       std::vector<TrajectoryMeasurement> TMeas=TrajTrackIter->key->measurements();
00290       std::vector<TrajectoryMeasurement>::iterator itm;
00291       
00292       for (itm=TMeas.begin();itm!=TMeas.end();itm++){ //loop on hits
00293       
00294       TanTrackAngle = -99;
00295       ClSize = -99;
00296       HitCharge = 0;
00297       Type = -99;
00298       Layer = -99;
00299       Wheel = -99;
00300       bw_fw = -99;
00301       Ext_Int = -99;
00302       MonoStereo = -99;
00303       MagField = -99;
00304       SignCorrection = -99;
00305       XGlobal = -99;
00306       YGlobal = -99;
00307       ZGlobal = -99;
00308       barycenter = -99;
00309       hit_std_dev = -99;
00310       sumx = 0;
00311       nstrip = 0;
00312       
00313       HitNr = 1;    
00314       
00315         TrajectoryStateOnSurface tsos=itm->updatedState();
00316         const TransientTrackingRecHit::ConstRecHitPointer thit=itm->recHit();
00317         if((thit->geographicalId().subdetId() == int(StripSubdetector::TIB)) ||  thit->geographicalId().subdetId()== int(StripSubdetector::TOB)){ //include only barrel
00318           const SiStripMatchedRecHit2D* matchedhit=dynamic_cast<const SiStripMatchedRecHit2D*>((*thit).hit());
00319           const ProjectedSiStripRecHit2D* phit=dynamic_cast<const ProjectedSiStripRecHit2D*>((*thit).hit());
00320           const SiStripRecHit2D* hit=dynamic_cast<const SiStripRecHit2D*>((*thit).hit());
00321           if(phit) hit=&(phit->originalHit());
00322           
00323           LocalVector trackdirection=tsos.localDirection();
00324           
00325           if(matchedhit){//if matched hit...
00326             
00327             GluedGeomDet * gdet=(GluedGeomDet *)tracker->idToDet(matchedhit->geographicalId());
00328             
00329             GlobalVector gtrkdir=gdet->toGlobal(trackdirection);        
00330             
00331             // THIS THE POINTER TO THE MONO HIT OF A MATCHED HIT 
00332             
00333             const SiStripRecHit2D *monohit=matchedhit->monoHit();    
00334             const SiStripRecHit2D::ClusterRef & monocluster=monohit->cluster();
00335             const GeomDetUnit * monodet=gdet->monoDet();    
00336             const LocalPoint monoposition = monohit->localPosition();    
00337             StripSubdetector detid=(StripSubdetector)monohit->geographicalId();
00338             const GlobalPoint monogposition = (monodet->surface()).toGlobal(monoposition);
00339             ClSize = (monocluster->amplitudes()).size();
00340             
00341             const std::vector<uint8_t> amplitudes = monocluster->amplitudes();
00342             barycenter = monocluster->barycenter()- 0.5; 
00343             uint16_t FirstStrip = monocluster->firstStrip();
00344             std::vector<uint8_t>::const_iterator idigi;
00345             std::vector<uint8_t>::const_iterator begin=amplitudes.begin();
00346             nstrip=0;
00347             for(idigi=begin; idigi!=amplitudes.end(); idigi++){
00348             sumx+=pow(((FirstStrip+idigi-begin)-barycenter),2)*(*idigi);
00349             HitCharge+=*idigi;
00350             //if(*idigi!=0){nstrip+=1;}
00351             }
00352             //if(nstrip!=1){
00353             //hit_std_dev = sqrt(sumx*nstrip/((nstrip-1)*HitCharge));
00354             //}else{
00355             hit_std_dev = sqrt(sumx/HitCharge);
00356             //}
00357                     
00358             XGlobal = monogposition.x();
00359             YGlobal = monogposition.y();
00360             ZGlobal = monogposition.z();
00361             
00362             Type = detid.subdetId();
00363             MonoStereo=detid.stereo();
00364             
00365             if(detid.subdetId() == int (StripSubdetector::TIB)){
00366             TIBDetId TIBid=TIBDetId(detid);
00367             Layer = TIBid.layer();
00368             bw_fw = TIBid.string()[0];
00369             Ext_Int = TIBid.string()[1];
00370             }
00371             if(detid.subdetId() == int (StripSubdetector::TOB)){
00372             TOBDetId TOBid=TOBDetId(detid);
00373             Layer = TOBid.layer();
00374             bw_fw = TOBid.rod()[0];
00375             }
00376             if(detid.subdetId() == int (StripSubdetector::TID)){
00377             TIDDetId TIDid=TIDDetId(detid);
00378             Wheel = TIDid.wheel();
00379             bw_fw = TIDid.module()[0];
00380             }
00381             if(detid.subdetId() == int (StripSubdetector::TEC)){
00382             TECDetId TECid=TECDetId(detid);
00383             Wheel = TECid.wheel();
00384             bw_fw = TECid.petal()[0];
00385             }
00386             
00387             
00388             LocalVector monotkdir=monodet->toLocal(gtrkdir);
00389            
00390             if(monotkdir.z()!=0){
00391               
00392               // THE LOCAL ANGLE (MONO)
00393               float tanangle = monotkdir.x()/monotkdir.z();
00394               
00395               TanTrackAngle = tanangle;
00396               detparmap::iterator TheDet=detmap.find(detid.rawId());
00397               LocalVector localmagdir;
00398               if(TheDet!=detmap.end())localmagdir=TheDet->second->magfield;
00399               MagField = localmagdir.mag();
00400               if(MagField != 0.){
00401               LocalVector monoylocal(0,1,0);
00402               float signcorrection = (localmagdir * monoylocal)/(MagField);
00403               if(signcorrection!=0)SignCorrection=1/signcorrection;}
00404               
00405               std::map<const SiStripRecHit2D *,std::pair<float,float>,DetIdLess>::iterator alreadystored=hitangleassociation.find(monohit);
00406               
00407               
00408               if(alreadystored != hitangleassociation.end()){//decide which hit take
00409               if(itm->estimate() >  alreadystored->second.first){
00410                 worse_double_hit++;}
00411                 if(itm->estimate() <  alreadystored->second.first){
00412                 better_double_hit++;
00413                 hitangleassociation.insert(std::make_pair(monohit, std::make_pair(itm->estimate(),tanangle)));
00414                 //HitsTree->Fill();
00415                 //hitcounter++;
00416                 }}
00417               else{
00418               hitangleassociation.insert(make_pair(monohit, std::make_pair(itm->estimate(),tanangle)));
00419               HitsTree->Fill();
00420               hitcounter++;}
00421                   
00422               // THIS THE POINTER TO THE STEREO HIT OF A MATCHED HIT 
00423               
00424             const SiStripRecHit2D *stereohit=matchedhit->stereoHit();
00425             const SiStripRecHit2D::ClusterRef & stereocluster=stereohit->cluster();
00426             const GeomDetUnit * stereodet=gdet->stereoDet();
00427             const LocalPoint stereoposition = stereohit->localPosition();    
00428             StripSubdetector detid=(StripSubdetector)stereohit->geographicalId();
00429             const GlobalPoint stereogposition = (stereodet->surface()).toGlobal(stereoposition);
00430             
00431             ClSize = (stereocluster->amplitudes()).size();
00432             
00433             const std::vector<uint8_t> amplitudes = stereocluster->amplitudes();
00434             barycenter = stereocluster->barycenter()- 0.5; 
00435             uint16_t FirstStrip = stereocluster->firstStrip();
00436             std::vector<uint8_t>::const_iterator idigi;
00437             std::vector<uint8_t>::const_iterator begin=amplitudes.begin();
00438             nstrip=0;
00439             for(idigi=begin; idigi!=amplitudes.end(); idigi++){
00440             sumx+=pow(((FirstStrip+idigi-begin)-barycenter),2)*(*idigi);
00441             HitCharge+=*idigi;
00442             //if(*idigi!=0){nstrip+=1;}
00443             }
00444             //if(nstrip!=1){
00445             //hit_std_dev = sqrt(sumx*nstrip/((nstrip-1)*HitCharge));
00446             //}else{
00447             hit_std_dev = sqrt(sumx/HitCharge);
00448             //}
00449             
00450             XGlobal = stereogposition.x();
00451             YGlobal = stereogposition.y();
00452             ZGlobal = stereogposition.z();
00453             
00454             Type = detid.subdetId();
00455             MonoStereo=detid.stereo();
00456             
00457             if(detid.subdetId() == int (StripSubdetector::TIB)){
00458             TIBDetId TIBid=TIBDetId(detid);
00459             Layer = TIBid.layer();
00460             bw_fw = TIBid.string()[0];
00461             Ext_Int = TIBid.string()[1];
00462             }
00463             if(detid.subdetId() == int (StripSubdetector::TOB)){
00464             TOBDetId TOBid=TOBDetId(detid);
00465             Layer = TOBid.layer();
00466             bw_fw = TOBid.rod()[0];
00467             }
00468             if(detid.subdetId() == int (StripSubdetector::TID)){
00469             TIDDetId TIDid=TIDDetId(detid);
00470             Wheel = TIDid.wheel();
00471             bw_fw = TIDid.module()[0];
00472             }
00473             if(detid.subdetId() == int (StripSubdetector::TEC)){
00474             TECDetId TECid=TECDetId(detid);
00475             Wheel = TECid.wheel();
00476             bw_fw = TECid.petal()[0];
00477             }
00478               
00479               
00480               LocalVector stereotkdir=stereodet->toLocal(gtrkdir);
00481               
00482               if(stereotkdir.z()!=0){
00483                 
00484                 // THE LOCAL ANGLE (STEREO)
00485                 float tanangle = stereotkdir.x()/stereotkdir.z();
00486                 TanTrackAngle = tanangle;
00487                 detparmap::iterator TheDet=detmap.find(detid.rawId());
00488                 LocalVector localmagdir;
00489                 if(TheDet!=detmap.end())localmagdir=TheDet->second->magfield;
00490                 MagField = localmagdir.mag();
00491                 LocalVector stereoylocal(0,1,0);
00492                 if(MagField != 0.){
00493                 float signcorrection = (localmagdir * stereoylocal)/(MagField);
00494                 if(signcorrection!=0)SignCorrection=1/signcorrection;}
00495                 
00496                 std::map<const SiStripRecHit2D *,std::pair<float,float>,DetIdLess>::iterator alreadystored=hitangleassociation.find(stereohit);
00497                 
00498                 
00499                 if(alreadystored != hitangleassociation.end()){//decide which hit take
00500                 if(itm->estimate() >  alreadystored->second.first){
00501                 worse_double_hit++;}
00502                   if(itm->estimate() <  alreadystored->second.first){
00503                   better_double_hit++;
00504                   hitangleassociation.insert(std::make_pair(stereohit, std::make_pair(itm->estimate(),tanangle)));
00505                   //HitsTree->Fill();
00506                   //hitcounter++;
00507                   }}
00508                 else{
00509                 hitangleassociation.insert(std::make_pair(stereohit, std::make_pair(itm->estimate(),tanangle)));
00510                 HitsTree->Fill();
00511                 hitcounter++;}
00512                                   
00513               }
00514             }
00515           }
00516           else if(hit){
00517           
00518           
00519             //  hit= POINTER TO THE RECHIT
00520             
00521             const SiStripRecHit2D::ClusterRef & cluster=hit->cluster();
00522            
00523             GeomDet * gdet=(GeomDet *)tracker->idToDet(hit->geographicalId());
00524             const LocalPoint position = hit->localPosition();    
00525             StripSubdetector detid=(StripSubdetector)hit->geographicalId();
00526             const GlobalPoint gposition = (gdet->surface()).toGlobal(position);
00527             
00528             ClSize = (cluster->amplitudes()).size();
00529             
00530             const std::vector<uint8_t> amplitudes = cluster->amplitudes();
00531             barycenter = cluster->barycenter()- 0.5; 
00532             uint16_t FirstStrip = cluster->firstStrip();
00533             std::vector<uint8_t>::const_iterator idigi;
00534             std::vector<uint8_t>::const_iterator begin=amplitudes.begin();
00535             nstrip=0;
00536             for(idigi=begin; idigi!=amplitudes.end(); idigi++){
00537             sumx+=pow(((FirstStrip+idigi-begin)-barycenter),2)*(*idigi);
00538             HitCharge+=*idigi;
00539             //if(*idigi!=0){nstrip+=1;}
00540             }
00541             //if(nstrip!=1){
00542             //hit_std_dev = sqrt(sumx*nstrip/((nstrip-1)*HitCharge));
00543             //}else{
00544             hit_std_dev = sqrt(sumx/HitCharge);
00545             //}
00546             
00547             XGlobal = gposition.x();
00548             YGlobal = gposition.y();
00549             ZGlobal = gposition.z();
00550             
00551             Type = detid.subdetId();
00552             MonoStereo=detid.stereo();
00553             
00554             if(detid.subdetId() == int (StripSubdetector::TIB)){
00555             TIBDetId TIBid=TIBDetId(detid);
00556             Layer = TIBid.layer();
00557             bw_fw = TIBid.string()[0];
00558             Ext_Int = TIBid.string()[1];
00559             }
00560             if(detid.subdetId() == int (StripSubdetector::TOB)){
00561             TOBDetId TOBid=TOBDetId(detid);
00562             Layer = TOBid.layer();
00563             bw_fw = TOBid.rod()[0];
00564             }
00565             if(detid.subdetId() == int (StripSubdetector::TID)){
00566             TIDDetId TIDid=TIDDetId(detid);
00567             Wheel = TIDid.wheel();
00568             bw_fw = TIDid.module()[0];
00569             }
00570             if(detid.subdetId() == int (StripSubdetector::TEC)){
00571             TECDetId TECid=TECDetId(detid);
00572             Wheel = TECid.wheel();
00573             bw_fw = TECid.petal()[0];
00574             }
00575                     
00576             if(trackdirection.z()!=0){
00577             
00578               // THE LOCAL ANGLE 
00579               float tanangle = trackdirection.x()/trackdirection.z();
00580               TanTrackAngle = tanangle;
00581               detparmap::iterator TheDet=detmap.find(detid.rawId());
00582               LocalVector localmagdir;
00583               if(TheDet!=detmap.end())localmagdir=TheDet->second->magfield;
00584               MagField = localmagdir.mag();
00585               if(MagField != 0.){
00586               LocalVector ylocal(0,1,0);
00587               float signcorrection = (localmagdir * ylocal)/(MagField);
00588               if(signcorrection!=0)SignCorrection=1/signcorrection;}
00589                 
00590               std::map<const SiStripRecHit2D *,std::pair<float,float>, DetIdLess>::iterator alreadystored=hitangleassociation.find(hit);
00591               
00592               
00593               if(alreadystored != hitangleassociation.end()){//decide which hit take
00594               if(itm->estimate() >  alreadystored->second.first){
00595               worse_double_hit++;}
00596                 if(itm->estimate() <  alreadystored->second.first){
00597                 better_double_hit++;
00598                 hitangleassociation.insert(std::make_pair(hit, std::make_pair(itm->estimate(),tanangle)));
00599                 //HitsTree->Fill();
00600                 //hitcounter++;
00601                 }}
00602               else{
00603               hitangleassociation.insert(std::make_pair(hit,std::make_pair(itm->estimate(), tanangle) ) );
00604               HitsTree->Fill();
00605               hitcounter++;}
00606               
00607               
00608             }
00609           }
00610         }
00611       }
00612     }
00613   }
00614     std::map<const SiStripRecHit2D *,std::pair<float,float>,DetIdLess>::iterator hitsiter;
00615     
00616         
00617     for(hitsiter=hitangleassociation.begin();hitsiter!=hitangleassociation.end();hitsiter++){
00618     
00619     hitcounter_2ndloop++;
00620     
00621     const SiStripRecHit2D* hit=hitsiter->first;
00622     const SiStripRecHit2D::ClusterRef & cluster=hit->cluster();
00623 
00624     size=(cluster->amplitudes()).size();
00625     
00626     const LocalPoint position = hit->localPosition();    
00627     StripSubdetector detid=(StripSubdetector)hit->geographicalId();  
00628     
00629     const GeomDetUnit * StripDet=dynamic_cast<const GeomDetUnit*>(tracker->idToDet(detid));
00630     const GlobalPoint gposition = (StripDet->surface()).toGlobal(position);
00631     
00632     //Cross Check DQM - Tree 
00633     
00634     int count = 1;
00635     histos[1]->Fill(count);
00636     
00637     float tangent = hitsiter->second.second;
00638           
00639     //Sign and XZ plane projection correction applied in TrackLocalAngle (TIB|TOB layers)
00640       
00641     detparmap::iterator thedet=detmap.find(detid.rawId());
00642     LocalVector localmagdir;
00643     if(thedet!=detmap.end())localmagdir=thedet->second->magfield;
00644     float localmagfield = localmagdir.mag();
00645         
00646     if(localmagfield != 0.){
00647       
00648       LocalVector ylocal(0,1,0);
00649       
00650       float normprojection = (localmagdir * ylocal)/(localmagfield);
00651       
00652       if(normprojection == 0.)LogDebug("SiStripLAProfileBooker::analyze")<<"Error: YBprojection = 0";
00653       
00654       else{
00655         float signprojcorrection = 1/normprojection;
00656         tangent*=signprojcorrection;
00657       }
00658     }
00659           
00660     //Filling histograms
00661     
00662     histomap::iterator thehisto=histos.find(detid.rawId());
00663    
00664     if(thehisto==histos.end())edm::LogError("SiStripLAProfileBooker::analyze")<<"Error: the profile associated to"<<detid.rawId()<<"does not exist! ";    
00665     else thehisto->second->Fill(tangent,size);
00666     
00667     //Summary histograms
00668     std::string name;
00669     unsigned int layerid;
00670     getlayer(detid,name,layerid);
00671     histomap::iterator thesummaryhisto=summaryhisto.find(layerid);
00672     if(thesummaryhisto==summaryhisto.end())edm::LogError("SiStripLAProfileBooker::analyze")<<"Error: the profile associated to subdet "<<name<<"does not exist! ";   
00673     else thesummaryhisto->second->Fill(tangent,size);
00674     
00675     //}
00676     
00677   }
00678     
00679         
00680 }
00681 
00682  
00683 //Makename function
00684  
00685 void SiStripLAProfileBooker::getlayer(const DetId & detid, std::string &name,unsigned int &layerid){
00686     int layer=0;
00687     std::stringstream layernum;
00688 
00689     if(detid.subdetId() == int (StripSubdetector::TIB)){
00690       TIBDetId TIBid=TIBDetId(detid);
00691       name+="TIB_Layer_";
00692       layer = TIBid.layer();
00693     }
00694 
00695     else if(detid.subdetId() == int (StripSubdetector::TID)){
00696       TIDDetId TIDid=TIDDetId(detid);
00697       name+="TID_Ring_";
00698       layer = TIDid.ring();
00699     }
00700 
00701     else if(detid.subdetId() == int (StripSubdetector::TOB)){
00702       TOBDetId TOBid=TOBDetId(detid);
00703       name+="TOB_Layer_";
00704       layer = TOBid.layer();
00705 
00706     }
00707 
00708     else if(detid.subdetId() == int (StripSubdetector::TEC)){
00709       TECDetId TECid=TECDetId(detid);
00710       name+="TEC_Ring_";
00711       layer = TECid.ring();
00712     }
00713     layernum<<layer;
00714     name+=layernum.str();
00715     layerid=detid.subdetId()*10+layer;
00716   
00717 }
00718  
00719 void SiStripLAProfileBooker::endJob(){
00720   fitmap fits;
00721 
00722   //Histograms fit
00723   TF1 *fitfunc=0;
00724   double ModuleRangeMin=conf_.getParameter<double>("ModuleFitXMin");
00725   double ModuleRangeMax=conf_.getParameter<double>("ModuleFitXMax");
00726   double TIBRangeMin=conf_.getParameter<double>("TIBFitXMin");
00727   double TIBRangeMax=conf_.getParameter<double>("TIBFitXMax");
00728   double TOBRangeMin=conf_.getParameter<double>("TOBFitXMin");
00729   double TOBRangeMax=conf_.getParameter<double>("TOBFitXMax");
00730   
00731   histomap::iterator hist_it;
00732   fitfunc= new TF1("fitfunc","([4]/[3])*[1]*(TMath::Abs(x-[0]))+[2]",-1,1);
00733     
00734   for(hist_it=histos.begin();hist_it!=histos.end(); hist_it++){
00735     if(hist_it->first != 1){
00736     if(hist_it->second->getEntries()>100){
00737       float thickness=0,pitch=-1;
00738       detparmap::iterator detparit=detmap.find(hist_it->first);
00739       if(detparit!=detmap.end()){
00740         thickness = detparit->second->thickness;
00741         pitch = detparit->second->pitch;
00742       }
00743       
00744       fitfunc->SetParameter(0, 0);
00745       fitfunc->SetParameter(1, 0);
00746       fitfunc->SetParameter(2, 1);
00747       fitfunc->FixParameter(3, pitch);
00748       fitfunc->FixParameter(4, thickness);
00749       int fitresult=-1;
00750       TProfile* theProfile=ExtractTObject<TProfile>().extract(hist_it->second);
00751       fitresult=theProfile->Fit(fitfunc,"N","",ModuleRangeMin, ModuleRangeMax);
00752       detparmap::iterator thedet=detmap.find(hist_it->first);
00753       LocalVector localmagdir;
00754       if(thedet!=detmap.end())localmagdir=thedet->second->magfield;
00755       float localmagfield = localmagdir.mag();
00756 
00757       histofit *fit= new histofit;
00758       fits[hist_it->first] =fit;
00759       
00760       fit->chi2 = fitfunc->GetChisquare();
00761       fit->ndf  = fitfunc->GetNDF();
00762       fit->p0   = fitfunc->GetParameter(0)/localmagfield;
00763       fit->p1   = fitfunc->GetParameter(1);
00764       fit->p2   = fitfunc->GetParameter(2);
00765       fit->errp0   = fitfunc->GetParError(0)/localmagfield;
00766       fit->errp1   = fitfunc->GetParError(1);
00767       fit->errp2   = fitfunc->GetParError(2);
00768     }
00769   }
00770   }
00771     
00772   histomap::iterator summaryhist_it;
00773   
00774   for(summaryhist_it=summaryhisto.begin();summaryhist_it!=summaryhisto.end(); summaryhist_it++){
00775     if(summaryhist_it->second->getEntries()>100){
00776       float thickness=0,pitch=-1;
00777       detparmap::iterator detparit=summarydetmap.find(summaryhist_it->first);
00778       if(detparit!=summarydetmap.end()){
00779         thickness = detparit->second->thickness;
00780         pitch = detparit->second->pitch;
00781       }
00782       
00783       fitfunc->SetParameter(0, 0);
00784       fitfunc->SetParameter(1, 0);
00785       fitfunc->SetParameter(2, 1);
00786       fitfunc->FixParameter(3, pitch);
00787       fitfunc->FixParameter(4, thickness);
00788       int fitresult=-1;
00789       TProfile* thesummaryProfile=ExtractTObject<TProfile>().extract(summaryhist_it->second);
00790       if ((summaryhist_it->first)/10==int (StripSubdetector::TIB)||(summaryhist_it->first)/10==int (StripSubdetector::TID))
00791         fitresult=thesummaryProfile->Fit(fitfunc,"N","",TIBRangeMin, TIBRangeMax);
00792       else if ((summaryhist_it->first)/10==int (StripSubdetector::TOB)||(summaryhist_it->first)/10==int (StripSubdetector::TEC))
00793         fitresult=thesummaryProfile->Fit(fitfunc,"N","",TOBRangeMin, TOBRangeMax);
00794       //if(fitresult==0){
00795         histofit * summaryfit=new histofit;
00796         summaryfits[summaryhist_it->first] = summaryfit;
00797         summaryfit->chi2 = fitfunc->GetChisquare();
00798         summaryfit->ndf  = fitfunc->GetNDF();
00799         summaryfit->p0   = fitfunc->GetParameter(0);
00800         summaryfit->p1   = fitfunc->GetParameter(1);
00801         summaryfit->p2   = fitfunc->GetParameter(2);
00802         summaryfit->errp0   = fitfunc->GetParError(0);
00803         summaryfit->errp1   = fitfunc->GetParError(1);
00804         summaryfit->errp2   = fitfunc->GetParError(2);
00805         // }
00806     }
00807   }
00808     
00809   delete fitfunc;
00810   
00811   //File with fit parameters  
00812   
00813   std::string fitName=conf_.getParameter<std::string>("fitName");
00814   fitName+=".txt";
00815   
00816   ofstream fit;
00817   fit.open(fitName.c_str());
00818   
00819   //  fit<<">>> ANALYZED RUNS = ";
00820   //for(int n=0;n!=runcounter;n++){
00821     //fit<<runvector[n]<<", ";}
00822   //fit<<endl;
00823   
00824   fit<<">>> TOTAL EVENTS = "<<eventcounter<<std::endl;
00825   
00826   fit<<">>> NUMBER OF TRACJECTORIES = "<<trajcounter<<std::endl;
00827   
00828   fit<<">>> WORSE DOUBLE HITS = "<<worse_double_hit<<std::endl;
00829   
00830   fit<<">>> BETTER DOUBLE HITS (not substitued in the tree) = "<<better_double_hit<<std::endl;
00831   
00832   fit<<">>> NUMBER OF RECHITS = "<<hitcounter<<std::endl;
00833   
00834   fit<<">>> NUMBER OF RECHITS (2ndLoop) = "<<hitcounter_2ndloop<<std::endl;
00835   
00836   fit<<">>> NUMBER OF DETECTOR HISTOGRAMS = "<<histos.size()<<std::endl;
00837      
00838   std::string subdetector;
00839   for(summaryhist_it=summaryhisto.begin();summaryhist_it!=summaryhisto.end(); summaryhist_it++){
00840     if ((summaryhist_it->first)/10==int (StripSubdetector::TIB))subdetector="TIB";
00841     else if ((summaryhist_it->first)/10==int (StripSubdetector::TID))subdetector="TID";
00842     else if ((summaryhist_it->first)/10==int (StripSubdetector::TOB))subdetector="TOB";
00843     else if ((summaryhist_it->first)/10==int (StripSubdetector::TEC))subdetector="TEC";
00844     float thickness=0,pitch=-1;
00845     detparmap::iterator detparit=summarydetmap.find(summaryhist_it->first);
00846     if(detparit!=summarydetmap.end()){
00847       thickness = detparit->second->thickness;
00848       pitch = detparit->second->pitch;
00849     }    
00850     fitmap::iterator  fitpar=summaryfits.find(summaryhist_it->first);
00851     
00852     fit<<std::endl<<"--------------------------- SUMMARY FIT: "<<subdetector<<" LAYER/RING "<<(summaryhist_it->first%10)<<" -------------------------"<<std::endl<<std::endl;
00853     fit<<"Number of entries = "<<summaryhist_it->second->getEntries()<<std::endl<<std::endl;
00854     fit<<"Detector thickness = "<<thickness<<" um "<<std::endl;
00855     fit<<"Detector pitch = "<<pitch<<" um "<<std::endl<<std::endl;    
00856     if(fitpar!=summaryfits.end()){
00857       fit<<"Chi Square/ndf = "<<(fitpar->second->chi2)/(fitpar->second->ndf)<<std::endl;
00858       fit<<"NdF        = "<<fitpar->second->ndf<<std::endl;
00859       fit<<"p0 = "<<fitpar->second->p0<<"     err p0 = "<<fitpar->second->errp0<<std::endl;
00860       fit<<"p1 = "<<fitpar->second->p1<<"     err p1 = "<<fitpar->second->errp1<<std::endl;
00861       fit<<"p2 = "<<fitpar->second->p2<<"     err p2 = "<<fitpar->second->errp2<<std::endl<<std::endl;
00862     }
00863     else fit<<"no fit parameters available"<<std::endl;
00864   }
00865     
00866   for(hist_it=histos.begin();hist_it!=histos.end(); hist_it++){   
00867   if(hist_it->first != 1){
00868     float thickness=0,pitch=-1;
00869     detparmap::iterator detparit=detmap.find(hist_it->first);
00870     if(detparit!=detmap.end()){
00871       thickness = detparit->second->thickness;
00872       pitch = detparit->second->pitch;
00873     }    
00874     fitmap::iterator  fitpar=fits.find(hist_it->first);
00875     if(hist_it->second->getEntries()>0){
00876       fit<<std::endl<<"-------------------------- MODULE HISTOGRAM FIT ------------------------"<<std::endl<<std::endl;
00877       DetId id= DetId(hist_it->first);
00878       fit<<"Module id= "<<id.rawId()<<std::endl<<std::endl;
00879       fit<<"Number of entries = "<<hist_it->second->getEntries()<<std::endl<<std::endl;
00880       fit<<"Detector thickness = "<<thickness<<" um "<<std::endl;
00881       fit<<"Detector pitch = "<<pitch<<" um "<<std::endl<<std::endl;
00882       if(fitpar!=fits.end()){
00883         fit<<"Chi Square/ndf = "<<(fitpar->second->chi2)/(fitpar->second->ndf)<<std::endl;
00884         fit<<"NdF        = "<<fitpar->second->ndf<<std::endl;
00885         fit<<"p0 = "<<fitpar->second->p0<<"     err p0 = "<<fitpar->second->errp0<<std::endl;
00886         fit<<"p1 = "<<fitpar->second->p1<<"     err p1 = "<<fitpar->second->errp1<<std::endl;
00887         fit<<"p2 = "<<fitpar->second->p2<<"     err p2 = "<<fitpar->second->errp2<<std::endl<<std::endl;
00888       }    
00889     }
00890   }
00891   }
00892     
00893   fit.close(); 
00894   std::string outputFile_ =conf_.getUntrackedParameter<std::string>("fileName", "LorentzAngle.root");
00895   dbe_->save(outputFile_);
00896   
00897   hFile->Write();
00898   hFile->Close();
00899 }

Generated on Tue Jun 9 17:25:51 2009 for CMSSW by  doxygen 1.5.4