CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FastSimulation/TrackingRecHitProducer/src/SiTrackerGaussianSmearingRecHitConverter.cc

Go to the documentation of this file.
00001 
00008 //PAT
00009 //FastTrackerCluster
00010 #include "FastSimDataFormats/External/interface/FastTrackerCluster.h"
00011 
00012 // SiTracker Gaussian Smearing
00013 #include "FastSimulation/TrackingRecHitProducer/interface/SiTrackerGaussianSmearingRecHitConverter.h"
00014 
00015 // SiPixel Gaussian Smearing
00016 #include "FastSimulation/TrackingRecHitProducer/interface/SiPixelGaussianSmearingRecHitConverterAlgorithm.h"
00017 
00018 // SiStripGaussianSmearing
00019 #include "FastSimulation/TrackingRecHitProducer/interface/SiStripGaussianSmearingRecHitConverterAlgorithm.h"
00020 
00021 // Geometry and magnetic field
00022 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00023 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00024 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00025 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00026 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
00027 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00028 #include "MagneticField/Engine/interface/MagneticField.h"
00029 
00030 // Data Formats
00031 #include "DataFormats/Common/interface/Handle.h"
00032 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"        
00033 
00034 // Framework
00035 #include "FWCore/Framework/interface/ESHandle.h"
00036 #include "FWCore/Framework/interface/Event.h"
00037 #include "FWCore/Framework/interface/EventSetup.h"
00038 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00039 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00040 #include "FWCore/ServiceRegistry/interface/Service.h"
00041 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00042 #include "FWCore/Utilities/interface/Exception.h"
00043 
00044 // Numbering scheme
00045 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00046 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00047 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00048 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00049 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00050 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00051 #include "DataFormats/GeometryCommonDetAlgo/interface/ErrorFrameTransformer.h"
00052 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00053 
00054 //For Pileup events
00055 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
00056 
00057 // Random engine
00058 #include "FastSimulation/Utilities/interface/RandomEngine.h"
00059 
00060 // topology
00061 
00062 // the rec hit matcher
00063 #include "FastSimulation/TrackingRecHitProducer/interface/GSRecHitMatcher.h"
00064 
00065 // STL
00066 #include <memory>
00067 
00068 // ROOT
00069 #include <TFile.h>
00070 #include <TH1F.h>
00071 
00072 
00073 //#define FAMOS_DEBUG
00074 
00075 SiTrackerGaussianSmearingRecHitConverter::SiTrackerGaussianSmearingRecHitConverter(
00076   edm::ParameterSet const& conf) 
00077   : pset_(conf)
00078 {
00079   thePixelDataFile = 0;
00080   thePixelBarrelResolutionFile = 0;
00081   thePixelForwardResolutionFile = 0;
00082   thePixelBarrelParametrization = 0;
00083   thePixelEndcapParametrization = 0;
00084   theSiStripErrorParametrization = 0;
00085   numberOfDisabledModules = 0;
00086 
00087   random = 0;
00088 
00089 
00090 #ifdef FAMOS_DEBUG
00091   std::cout << "SiTrackerGaussianSmearingRecHitConverter instantiated" << std::endl;
00092 #endif
00093 
00094   // Initialize the random number generator service
00095   edm::Service<edm::RandomNumberGenerator> rng;
00096   if ( ! rng.isAvailable() ) {
00097     throw cms::Exception("Configuration")
00098       << "SiTrackerGaussianSmearingRecHitConverter requires the RandomGeneratorService\n"
00099          "which is not present in the configuration file.\n"
00100          "You must add the service in the configuration file\n"
00101          "or remove the module that requires it";
00102   }
00103 
00104   random = new RandomEngine(&(*rng));
00105   //PAT
00106   produces<FastTrackerClusterCollection>("TrackerClusters");
00107 
00108   produces<SiTrackerGSRecHit2DCollection>("TrackerGSRecHits");
00109   produces<SiTrackerGSMatchedRecHit2DCollection>("TrackerGSMatchedRecHits");
00110 
00111   //--- PSimHit Containers
00112   trackerContainers.clear();
00113   trackerContainers = conf.getParameter<std::vector<edm::InputTag> >("ROUList");
00114   //--- delta rays p cut [GeV/c] to filter PSimHits with p>
00115   deltaRaysPCut = conf.getParameter<double>("DeltaRaysMomentumCut");
00116 
00117   //--- switch to have RecHit == PSimHit
00118   trackingPSimHits = conf.getParameter<bool>("trackingPSimHits");
00119   if(trackingPSimHits) std::cout << "### trackingPSimHits chosen " << trackingPSimHits << std::endl;
00120 
00121   // switch on/off matching
00122   doMatching = conf.getParameter<bool>("doRecHitMatching");
00123 
00124   // disable/enable dead channels
00125   doDisableChannels = conf.getParameter<bool>("killDeadChannels");
00126 
00127   // Switch between old (ORCA) and new (CMSSW) pixel parameterization
00128   useCMSSWPixelParameterization = conf.getParameter<bool>("UseCMSSWPixelParametrization");
00129 #ifdef FAMOS_DEBUG
00130   std::cout << (useCMSSWPixelParameterization? "CMSSW" : "ORCA") << " pixel parametrization chosen in config file." << std::endl;
00131 #endif
00132 
00133   //Clusters
00134   GevPerElectron =  conf.getParameter<double>("GevPerElectron");
00135   ElectronsPerADC =  conf.getParameter<double>("ElectronsPerADC");
00136   
00137   //
00138   // TIB
00139   localPositionResolution_TIB1x = conf.getParameter<double>("TIB1x");
00140   localPositionResolution_TIB1y = conf.getParameter<double>("TIB1y");
00141   localPositionResolution_TIB2x = conf.getParameter<double>("TIB2x");
00142   localPositionResolution_TIB2y = conf.getParameter<double>("TIB2y");
00143   localPositionResolution_TIB3x = conf.getParameter<double>("TIB3x");
00144   localPositionResolution_TIB3y = conf.getParameter<double>("TIB3y");
00145   localPositionResolution_TIB4x = conf.getParameter<double>("TIB4x");
00146   localPositionResolution_TIB4y = conf.getParameter<double>("TIB4y");
00147   //
00148   // TID
00149   localPositionResolution_TID1x = conf.getParameter<double>("TID1x");
00150   localPositionResolution_TID1y = conf.getParameter<double>("TID1y");
00151   localPositionResolution_TID2x = conf.getParameter<double>("TID2x");
00152   localPositionResolution_TID2y = conf.getParameter<double>("TID2y");
00153   localPositionResolution_TID3x = conf.getParameter<double>("TID3x");
00154   localPositionResolution_TID3y = conf.getParameter<double>("TID3y");
00155   //
00156   // TOB
00157   localPositionResolution_TOB1x = conf.getParameter<double>("TOB1x");
00158   localPositionResolution_TOB1y = conf.getParameter<double>("TOB1y");
00159   localPositionResolution_TOB2x = conf.getParameter<double>("TOB2x");
00160   localPositionResolution_TOB2y = conf.getParameter<double>("TOB2y");
00161   localPositionResolution_TOB3x = conf.getParameter<double>("TOB3x");
00162   localPositionResolution_TOB3y = conf.getParameter<double>("TOB3y");
00163   localPositionResolution_TOB4x = conf.getParameter<double>("TOB4x");
00164   localPositionResolution_TOB4y = conf.getParameter<double>("TOB4y");
00165   localPositionResolution_TOB5x = conf.getParameter<double>("TOB5x");
00166   localPositionResolution_TOB5y = conf.getParameter<double>("TOB5y");
00167   localPositionResolution_TOB6x = conf.getParameter<double>("TOB6x");
00168   localPositionResolution_TOB6y = conf.getParameter<double>("TOB6y");
00169   //
00170   // TEC
00171   localPositionResolution_TEC1x = conf.getParameter<double>("TEC1x");
00172   localPositionResolution_TEC1y = conf.getParameter<double>("TEC1y");
00173   localPositionResolution_TEC2x = conf.getParameter<double>("TEC2x");
00174   localPositionResolution_TEC2y = conf.getParameter<double>("TEC2y");
00175   localPositionResolution_TEC3x = conf.getParameter<double>("TEC3x");
00176   localPositionResolution_TEC3y = conf.getParameter<double>("TEC3y");
00177   localPositionResolution_TEC4x = conf.getParameter<double>("TEC4x");
00178   localPositionResolution_TEC4y = conf.getParameter<double>("TEC4y");
00179   localPositionResolution_TEC5x = conf.getParameter<double>("TEC5x");
00180   localPositionResolution_TEC5y = conf.getParameter<double>("TEC5y");
00181   localPositionResolution_TEC6x = conf.getParameter<double>("TEC6x");
00182   localPositionResolution_TEC6y = conf.getParameter<double>("TEC6y");
00183   localPositionResolution_TEC7x = conf.getParameter<double>("TEC7x");
00184   localPositionResolution_TEC7y = conf.getParameter<double>("TEC7y");
00185   //
00186   localPositionResolution_z = 0.0001; // not to be changed, set to minimum (1 um), Kalman Filter will crash if errors are exactly 0, setting 1 um means 0
00187   //
00188 #ifdef FAMOS_DEBUG
00189   std::cout << "RecHit local position error set to" << "\n"
00190             << "\tTIB1\tx = " << localPositionResolution_TIB1x 
00191             << " cm\ty = " << localPositionResolution_TIB1y << " cm" << "\n"
00192             << "\tTIB2\tx = " << localPositionResolution_TIB2x 
00193             << " cm\ty = " << localPositionResolution_TIB2y << " cm" << "\n"
00194             << "\tTIB3\tx = " << localPositionResolution_TIB3x 
00195             << " cm\ty = " << localPositionResolution_TIB3y << " cm" << "\n"
00196             << "\tTIB4\tx = " << localPositionResolution_TIB4x 
00197             << " cm\ty = " << localPositionResolution_TIB4y << " cm" << "\n"
00198             << "\tTID1\tx = " << localPositionResolution_TID1x 
00199             << " cm\ty = " << localPositionResolution_TID1y << " cm" << "\n"
00200             << "\tTID2\tx = " << localPositionResolution_TID2x 
00201             << " cm\ty = " << localPositionResolution_TID2y << " cm" << "\n"
00202             << "\tTID3\tx = " << localPositionResolution_TID3x 
00203             << " cm\ty = " << localPositionResolution_TID3y << " cm" << "\n"
00204             << "\tTOB1\tx = " << localPositionResolution_TOB1x 
00205             << " cm\ty = " << localPositionResolution_TOB1y << " cm" << "\n"
00206             << "\tTOB2\tx = " << localPositionResolution_TOB2x 
00207             << " cm\ty = " << localPositionResolution_TOB2y << " cm" << "\n"
00208             << "\tTOB3\tx = " << localPositionResolution_TOB3x 
00209             << " cm\ty = " << localPositionResolution_TOB3y << " cm" << "\n"
00210             << "\tTOB4\tx = " << localPositionResolution_TOB4x 
00211             << " cm\ty = " << localPositionResolution_TOB4y << " cm" << "\n"
00212             << "\tTOB5\tx = " << localPositionResolution_TOB5x 
00213             << " cm\ty = " << localPositionResolution_TOB5y << " cm" << "\n"
00214             << "\tTOB6\tx = " << localPositionResolution_TOB6x 
00215             << " cm\ty = " << localPositionResolution_TOB6y << " cm" << "\n"
00216             << "\tTEC1\tx = " << localPositionResolution_TEC1x 
00217             << " cm\ty = " << localPositionResolution_TEC1y << " cm" << "\n"
00218             << "\tTEC2\tx = " << localPositionResolution_TEC2x 
00219             << " cm\ty = " << localPositionResolution_TEC2y << " cm" << "\n"
00220             << "\tTEC3\tx = " << localPositionResolution_TEC3x 
00221             << " cm\ty = " << localPositionResolution_TEC3y << " cm" << "\n"
00222             << "\tTEC4\tx = " << localPositionResolution_TEC4x 
00223             << " cm\ty = " << localPositionResolution_TEC4y << " cm" << "\n"
00224             << "\tTEC5\tx = " << localPositionResolution_TEC5x 
00225             << " cm\ty = " << localPositionResolution_TEC5y << " cm" << "\n"
00226             << "\tTEC6\tx = " << localPositionResolution_TEC6x 
00227             << " cm\ty = " << localPositionResolution_TEC6y << " cm" << "\n"
00228             << "\tTEC7\tx = " << localPositionResolution_TEC7x 
00229             << " cm\ty = " << localPositionResolution_TEC7y << " cm" << "\n"
00230             << "\tAll:\tz = " << localPositionResolution_z     << " cm" 
00231             << std::endl;
00232 #endif
00233 
00234   //--- Number of histograms for alpha/beta barrel/forward multiplicity
00235   if(useCMSSWPixelParameterization) {
00236     nAlphaBarrel  = conf.getParameter<int>("AlphaBarrelMultiplicityNew");
00237     nBetaBarrel   = conf.getParameter<int>("BetaBarrelMultiplicityNew");
00238     nAlphaForward = conf.getParameter<int>("AlphaForwardMultiplicityNew");
00239     nBetaForward  = conf.getParameter<int>("BetaForwardMultiplicityNew");
00240   } else {
00241     nAlphaBarrel  = conf.getParameter<int>("AlphaBarrelMultiplicity");
00242     nBetaBarrel   = conf.getParameter<int>("BetaBarrelMultiplicity");
00243     nAlphaForward = conf.getParameter<int>("AlphaForwardMultiplicity");
00244     nBetaForward  = conf.getParameter<int>("BetaForwardMultiplicity");
00245   }
00246 #ifdef FAMOS_DEBUG
00247   std::cout << "Pixel maximum multiplicity set to " 
00248             << "\nBarrel"  << "\talpha " << nAlphaBarrel  
00249             << "\tbeta " << nBetaBarrel
00250             << "\nForward" << "\talpha " << nAlphaForward 
00251             << "\tbeta " << nBetaForward
00252             << std::endl;
00253 #endif
00254   
00255   // Resolution Barrel    
00256   if(useCMSSWPixelParameterization) {
00257     resAlphaBarrel_binMin   = conf.getParameter<double>("AlphaBarrel_BinMinNew"  );
00258     resAlphaBarrel_binWidth = conf.getParameter<double>("AlphaBarrel_BinWidthNew");
00259     resAlphaBarrel_binN     = conf.getParameter<int>(   "AlphaBarrel_BinNNew"    );
00260     resBetaBarrel_binMin    = conf.getParameter<double>("BetaBarrel_BinMinNew"   );
00261     resBetaBarrel_binWidth  = conf.getParameter<double>("BetaBarrel_BinWidthNew" );
00262     resBetaBarrel_binN      = conf.getParameter<int>(   "BetaBarrel_BinNNew"     );
00263   } else {
00264     resAlphaBarrel_binMin   = conf.getParameter<double>("AlphaBarrel_BinMin"  );
00265     resAlphaBarrel_binWidth = conf.getParameter<double>("AlphaBarrel_BinWidth");
00266     resAlphaBarrel_binN     = conf.getParameter<int>(   "AlphaBarrel_BinN"    );
00267     resBetaBarrel_binMin    = conf.getParameter<double>("BetaBarrel_BinMin"   );
00268     resBetaBarrel_binWidth  = conf.getParameter<double>("BetaBarrel_BinWidth" );
00269     resBetaBarrel_binN      = conf.getParameter<int>(   "BetaBarrel_BinN"     );
00270   }
00271   
00272   // Resolution Forward
00273   if(useCMSSWPixelParameterization) {
00274     resAlphaForward_binMin   = conf.getParameter<double>("AlphaForward_BinMinNew"   );
00275     resAlphaForward_binWidth = conf.getParameter<double>("AlphaForward_BinWidthNew" );
00276     resAlphaForward_binN     = conf.getParameter<int>(   "AlphaForward_BinNNew"     );
00277     resBetaForward_binMin    = conf.getParameter<double>("BetaForward_BinMinNew"    );
00278     resBetaForward_binWidth  = conf.getParameter<double>("BetaForward_BinWidthNew"  );
00279     resBetaForward_binN      = conf.getParameter<int>(   "BetaForward_BinNNew"      );
00280   } else {
00281     resAlphaForward_binMin   = conf.getParameter<double>("AlphaForward_BinMin"   );
00282     resAlphaForward_binWidth = conf.getParameter<double>("AlphaForward_BinWidth" );
00283     resAlphaForward_binN     = conf.getParameter<int>(   "AlphaForward_BinN"     );
00284     resBetaForward_binMin    = conf.getParameter<double>("BetaForward_BinMin"    );
00285     resBetaForward_binWidth  = conf.getParameter<double>("BetaForward_BinWidth"  );
00286     resBetaForward_binN      = conf.getParameter<int>(   "BetaForward_BinN"      );
00287   }
00288 
00289   // Hit Finding Probability
00290   theHitFindingProbability_PXB  = conf.getParameter<double>("HitFindingProbability_PXB" );
00291   theHitFindingProbability_PXF  = conf.getParameter<double>("HitFindingProbability_PXF" );
00292   theHitFindingProbability_TIB1 = conf.getParameter<double>("HitFindingProbability_TIB1");
00293   theHitFindingProbability_TIB2 = conf.getParameter<double>("HitFindingProbability_TIB2");
00294   theHitFindingProbability_TIB3 = conf.getParameter<double>("HitFindingProbability_TIB3");
00295   theHitFindingProbability_TIB4 = conf.getParameter<double>("HitFindingProbability_TIB4");
00296   theHitFindingProbability_TID1 = conf.getParameter<double>("HitFindingProbability_TID1");
00297   theHitFindingProbability_TID2 = conf.getParameter<double>("HitFindingProbability_TID2");
00298   theHitFindingProbability_TID3 = conf.getParameter<double>("HitFindingProbability_TID3");
00299   theHitFindingProbability_TOB1 = conf.getParameter<double>("HitFindingProbability_TOB1");
00300   theHitFindingProbability_TOB2 = conf.getParameter<double>("HitFindingProbability_TOB2");
00301   theHitFindingProbability_TOB3 = conf.getParameter<double>("HitFindingProbability_TOB3");
00302   theHitFindingProbability_TOB4 = conf.getParameter<double>("HitFindingProbability_TOB4");
00303   theHitFindingProbability_TOB5 = conf.getParameter<double>("HitFindingProbability_TOB5");
00304   theHitFindingProbability_TOB6 = conf.getParameter<double>("HitFindingProbability_TOB6");
00305   theHitFindingProbability_TEC1 = conf.getParameter<double>("HitFindingProbability_TEC1");
00306   theHitFindingProbability_TEC2 = conf.getParameter<double>("HitFindingProbability_TEC2");
00307   theHitFindingProbability_TEC3 = conf.getParameter<double>("HitFindingProbability_TEC3");
00308   theHitFindingProbability_TEC4 = conf.getParameter<double>("HitFindingProbability_TEC4");
00309   theHitFindingProbability_TEC5 = conf.getParameter<double>("HitFindingProbability_TEC5");
00310   theHitFindingProbability_TEC6 = conf.getParameter<double>("HitFindingProbability_TEC6");
00311   theHitFindingProbability_TEC7 = conf.getParameter<double>("HitFindingProbability_TEC7");
00312   //
00313 #ifdef FAMOS_DEBUG
00314   std::cout << "RecHit finding probability set to" << "\n"
00315             << "\tPXB  = " << theHitFindingProbability_PXB  << "\n"
00316             << "\tPXF  = " << theHitFindingProbability_PXF  << "\n"
00317             << "\tTIB1 = " << theHitFindingProbability_TIB1 << "\n"
00318             << "\tTIB2 = " << theHitFindingProbability_TIB2 << "\n"
00319             << "\tTIB3 = " << theHitFindingProbability_TIB3 << "\n"
00320             << "\tTIB4 = " << theHitFindingProbability_TIB4 << "\n"
00321             << "\tTID1 = " << theHitFindingProbability_TID1 << "\n"
00322             << "\tTID2 = " << theHitFindingProbability_TID2 << "\n"
00323             << "\tTID3 = " << theHitFindingProbability_TID3 << "\n"
00324             << "\tTOB1 = " << theHitFindingProbability_TOB1 << "\n"
00325             << "\tTOB2 = " << theHitFindingProbability_TOB2 << "\n"
00326             << "\tTOB3 = " << theHitFindingProbability_TOB3 << "\n"
00327             << "\tTOB4 = " << theHitFindingProbability_TOB4 << "\n"
00328             << "\tTOB5 = " << theHitFindingProbability_TOB5 << "\n"
00329             << "\tTOB6 = " << theHitFindingProbability_TOB6 << "\n"
00330             << "\tTEC1 = " << theHitFindingProbability_TEC1 << "\n"
00331             << "\tTEC2 = " << theHitFindingProbability_TEC2 << "\n"
00332             << "\tTEC3 = " << theHitFindingProbability_TEC3 << "\n"
00333             << "\tTEC4 = " << theHitFindingProbability_TEC4 << "\n"
00334             << "\tTEC5 = " << theHitFindingProbability_TEC5 << "\n"
00335             << "\tTEC6 = " << theHitFindingProbability_TEC6 << "\n"
00336             << "\tTEC7 = " << theHitFindingProbability_TEC7 << "\n"
00337             << std::endl;
00338 #endif
00339 
00340   // Initialize the si strip error parametrization
00341   theSiStripErrorParametrization = 
00342     new SiStripGaussianSmearingRecHitConverterAlgorithm(random);
00343 
00344   // Initialization of pixel parameterization posponed to beginRun(), since it depends on the magnetic field
00345 
00346 }
00347 
00348 
00349 void SiTrackerGaussianSmearingRecHitConverter::loadPixelData() {
00350   // load multiplicity cumulative probabilities
00351   // root files
00352   thePixelDataFile              = new TFile ( edm::FileInPath( thePixelMultiplicityFileName      ).fullPath().c_str() , "READ" );
00353   thePixelBarrelResolutionFile  = new TFile ( edm::FileInPath( thePixelBarrelResolutionFileName  ).fullPath().c_str() , "READ" );
00354   thePixelForwardResolutionFile = new TFile ( edm::FileInPath( thePixelForwardResolutionFileName ).fullPath().c_str() , "READ" );
00355   //
00356 
00357   // alpha barrel
00358   loadPixelData( thePixelDataFile, 
00359                  nAlphaBarrel  , 
00360                  std::string("hist_alpha_barrel")  , 
00361                  theBarrelMultiplicityAlphaCumulativeProbabilities  );
00362   // 
00363   // beta barrel
00364   loadPixelData( thePixelDataFile, 
00365                  nBetaBarrel   , 
00366                  std::string("hist_beta_barrel")   , 
00367                  theBarrelMultiplicityBetaCumulativeProbabilities   );
00368   // 
00369   // alpha forward
00370   loadPixelData( thePixelDataFile, 
00371                  nAlphaForward , 
00372                  std::string("hist_alpha_forward") , 
00373                  theForwardMultiplicityAlphaCumulativeProbabilities );
00374   // 
00375   // beta forward
00376   loadPixelData( thePixelDataFile, 
00377                  nBetaForward  , 
00378                  std::string("hist_beta_forward")  , 
00379                  theForwardMultiplicityBetaCumulativeProbabilities  );
00380 
00381   // Load also big pixel data if CMSSW parametrization is on
00382   // They are pushed back into the vectors after the normal pixels data:
00383   // [0, ..., (size/2)-1] -> Normal pixels
00384   // [size/2, ..., size-1] -> Big pixels
00385   if(useCMSSWPixelParameterization) {
00386     // alpha barrel
00387     loadPixelData( thePixelDataFile, 
00388                    nAlphaBarrel  , 
00389                    std::string("hist_alpha_barrel_big")  , 
00390                    theBarrelMultiplicityAlphaCumulativeProbabilities,
00391                    true );
00392     // 
00393     // beta barrel
00394     loadPixelData( thePixelDataFile, 
00395                    nBetaBarrel   , 
00396                    std::string("hist_beta_barrel_big")   , 
00397                    theBarrelMultiplicityBetaCumulativeProbabilities,
00398                    true );
00399     // 
00400     // alpha forward
00401     loadPixelData( thePixelDataFile, 
00402                    nAlphaForward , 
00403                    std::string("hist_alpha_forward_big") , 
00404                    theForwardMultiplicityAlphaCumulativeProbabilities, 
00405                    true );
00406     // 
00407     // beta forward
00408     loadPixelData( thePixelDataFile, 
00409                    nBetaForward  , 
00410                    std::string("hist_beta_forward_big")  , 
00411                    theForwardMultiplicityBetaCumulativeProbabilities, 
00412                    true );
00413   }
00414   // 
00415 }
00416 
00417 void SiTrackerGaussianSmearingRecHitConverter::loadPixelData( 
00418   TFile* pixelDataFile, 
00419   unsigned int nMultiplicity, 
00420   std::string histName,
00421   std::vector<TH1F*>& theMultiplicityCumulativeProbabilities,
00422   bool bigPixels) 
00423 {
00424 
00425   std::string histName_i = histName + "_%u"; // needed to open histograms with a for
00426   if(!bigPixels)
00427     theMultiplicityCumulativeProbabilities.clear();
00428   //
00429   // What's this vector? Not needed - MG
00430 //  std::vector<double> mult; // vector with fixed multiplicity
00431   for(unsigned int i = 0; i<nMultiplicity; ++i) {
00432     TH1F addHist = *((TH1F*) pixelDataFile->Get( Form( histName_i.c_str() ,i+1 )));
00433     if(i==0) {
00434       theMultiplicityCumulativeProbabilities.push_back( new TH1F(addHist) );
00435     } else {
00436       TH1F sumHist;
00437       if(bigPixels)
00438         sumHist = *(theMultiplicityCumulativeProbabilities[nMultiplicity+i-1]);
00439       else
00440         sumHist = *(theMultiplicityCumulativeProbabilities[i-1]);
00441       sumHist.Add(&addHist);
00442       theMultiplicityCumulativeProbabilities.push_back( new TH1F(sumHist) );
00443     }
00444   }
00445 
00446   // Logger
00447 #ifdef FAMOS_DEBUG
00448   const unsigned int maxMult = theMultiplicityCumulativeProbabilities.size();
00449   unsigned int iMult, multSize;
00450   if(useCMSSWPixelParameterization) {
00451     if(bigPixels) {     
00452       iMult = maxMult / 2;
00453       multSize = maxMult ;
00454     } else {                
00455       iMult = 0;
00456       multSize = maxMult;
00457     }
00458   } else {
00459     iMult = 0;
00460     multSize = maxMult ;
00461   }
00462   std::cout << " Multiplicity cumulated probability " << histName << std::endl;
00463   for(/* void */; iMult<multSize; ++iMult) {
00464     for(int iBin = 1; iBin<=theMultiplicityCumulativeProbabilities[iMult]->GetNbinsX(); ++iBin) {
00465       std::cout
00466         << " Multiplicity " << iMult+1 
00467         << " bin " << iBin 
00468         << " low edge = " 
00469         << theMultiplicityCumulativeProbabilities[iMult]->GetBinLowEdge(iBin)
00470         << " prob = " 
00471         << (theMultiplicityCumulativeProbabilities[iMult])->GetBinContent(iBin) 
00472         // remember in ROOT bin starts from 1 (0 underflow, nBin+1 overflow)
00473         << std::endl;
00474     }
00475   }
00476 #endif
00477 
00478 }
00479 
00480 // Destructor
00481 SiTrackerGaussianSmearingRecHitConverter::~SiTrackerGaussianSmearingRecHitConverter() {
00482   theBarrelMultiplicityAlphaCumulativeProbabilities.clear();
00483   theBarrelMultiplicityBetaCumulativeProbabilities.clear();
00484   theForwardMultiplicityAlphaCumulativeProbabilities.clear();
00485   theForwardMultiplicityBetaCumulativeProbabilities.clear();
00486   
00487   if(thePixelDataFile) delete thePixelDataFile;
00488   if(thePixelBarrelResolutionFile) delete thePixelBarrelResolutionFile;
00489   if(thePixelForwardResolutionFile) delete thePixelForwardResolutionFile;
00490   if(thePixelBarrelParametrization) delete thePixelBarrelParametrization;
00491   if(thePixelEndcapParametrization) delete thePixelEndcapParametrization;
00492   if(theSiStripErrorParametrization) delete theSiStripErrorParametrization;
00493 
00494   if (numberOfDisabledModules>0) delete disabledModules;
00495 
00496   if(random) delete random;
00497 
00498 }  
00499 
00500 void 
00501 SiTrackerGaussianSmearingRecHitConverter::beginRun(edm::Run & run, const edm::EventSetup & es) 
00502 {
00503 
00504   // Initialize the Tracker Geometry
00505   edm::ESHandle<TrackerGeometry> theGeometry;
00506   es.get<TrackerDigiGeometryRecord> ().get (theGeometry);
00507   geometry = &(*theGeometry);
00508 
00509   edm::ESHandle<TrackerGeometry> theMisAlignedGeometry;
00510   es.get<TrackerDigiGeometryRecord>().get("MisAligned",theMisAlignedGeometry);
00511   misAlignedGeometry = &(*theMisAlignedGeometry);
00512 
00513   const MagneticField* magfield;
00514   edm::ESHandle<MagneticField> magField;
00515   es.get<IdealMagneticFieldRecord>().get(magField);
00516   magfield=&(*magField);
00517   GlobalPoint center(0.0, 0.0, 0.0);
00518   double magFieldAtCenter = magfield->inTesla(center).mag();
00519 
00520   // For new parameterization: select multiplicity and resolution files according to magnetic field
00521   if(useCMSSWPixelParameterization) {
00522     if(magFieldAtCenter > 3.9) {
00523       thePixelMultiplicityFileName = pset_.getParameter<std::string>( "PixelMultiplicityFile40T");
00524       thePixelBarrelResolutionFileName = pset_.getParameter<std::string>( "PixelBarrelResolutionFile40T");
00525       thePixelForwardResolutionFileName = pset_.getParameter<std::string>( "PixelForwardResolutionFile40T");
00526     } else {
00527       thePixelMultiplicityFileName = pset_.getParameter<std::string>( "PixelMultiplicityFile38T");
00528       thePixelBarrelResolutionFileName = pset_.getParameter<std::string>( "PixelBarrelResolutionFile38T");      
00529       thePixelForwardResolutionFileName = pset_.getParameter<std::string>( "PixelForwardResolutionFile38T");
00530     }
00531   } else {
00532     thePixelMultiplicityFileName = pset_.getParameter<std::string>( "PixelMultiplicityFile" );
00533     thePixelBarrelResolutionFileName = pset_.getParameter<std::string>( "PixelBarrelResolutionFile");
00534     thePixelForwardResolutionFileName = pset_.getParameter<std::string>( "PixelForwardResolutionFile");
00535   }
00536 
00537 
00538   // Reading the list of dead pixel modules from DB:
00539   edm::ESHandle<SiPixelQuality> siPixelBadModule;
00540   es.get<SiPixelQualityRcd>().get(siPixelBadModule);
00541   numberOfDisabledModules = 0;
00542   if (doDisableChannels) {
00543     disabledModules = new std::vector<SiPixelQuality::disabledModuleType> ( siPixelBadModule->getBadComponentList() );
00544     numberOfDisabledModules = disabledModules->size();
00545     size_t numberOfRecoverableModules = 0;
00546     for (size_t id=0;id<numberOfDisabledModules;id++) {
00548       //  errortype "whole" = int 0 in DB //
00549       //  errortype "tbmA" = int 1 in DB  //
00550       //  errortype "tbmB" = int 2 in DB  //
00551       //  errortype "none" = int 3 in DB  //
00553       if ( (*disabledModules)[id-numberOfRecoverableModules].errorType != 0 ){
00554         // Disable only the modules  totally in error:
00555         disabledModules->erase(disabledModules->begin()+id-numberOfRecoverableModules);
00556         numberOfRecoverableModules++;
00557       }
00558     }
00559     numberOfDisabledModules = disabledModules->size();
00560   }
00561   
00562 
00563 
00564 #ifdef FAMOS_DEBUG
00565   std::cout << "Pixel multiplicity data are taken from file " << thePixelMultiplicityFileName << std::endl;
00566 
00567   std::cout << "Pixel maximum multiplicity set to " 
00568             << "\nBarrel"  << "\talpha " << nAlphaBarrel  
00569             << "\tbeta " << nBetaBarrel
00570             << "\nForward" << "\talpha " << nAlphaForward 
00571             << "\tbeta " << nBetaForward
00572             << std::endl;
00573 
00574   std::cout << "Barrel Pixel resolution data are taken from file " 
00575             << thePixelBarrelResolutionFileName << "\n"
00576             << "Alpha bin min = " << resAlphaBarrel_binMin
00577             << "\twidth = "       << resAlphaBarrel_binWidth
00578             << "\tbins = "        << resAlphaBarrel_binN
00579             << "\n"
00580             << " Beta bin min = " << resBetaBarrel_binMin
00581             << "\twidth = "       << resBetaBarrel_binWidth
00582             << "\tbins = "        << resBetaBarrel_binN
00583             << std::endl;
00584 
00585   std::cout << "Forward Pixel resolution data are taken from file " 
00586             << thePixelForwardResolutionFileName << "\n"
00587             << "Alpha bin min = " << resAlphaForward_binMin
00588             << "\twidth = "       << resAlphaForward_binWidth
00589             << "\tbins = "        << resAlphaForward_binN
00590             << "\n"
00591             << " Beta bin min = " << resBetaForward_binMin
00592             << "\twidth = "       << resBetaForward_binWidth
00593             << "\tbins = "        << resBetaForward_binN
00594             << std::endl;
00595 #endif 
00596   //
00597 
00598   //    
00599   // load pixel data
00600   loadPixelData();
00601   //
00602 
00603   // Initialize and open relevant files for the pixel barrel error parametrization
00604   thePixelBarrelParametrization = 
00605     new SiPixelGaussianSmearingRecHitConverterAlgorithm(
00606         pset_,
00607         GeomDetEnumerators::PixelBarrel,
00608         random);
00609   // Initialize and open relevant files for the pixel forward error parametrization 
00610   thePixelEndcapParametrization = 
00611     new SiPixelGaussianSmearingRecHitConverterAlgorithm(
00612         pset_,
00613         GeomDetEnumerators::PixelEndcap,
00614         random);
00615 }
00616 
00617 void SiTrackerGaussianSmearingRecHitConverter::produce(edm::Event& e, const edm::EventSetup& es) 
00618 {
00619   
00620 
00621   // Step 0: Declare Ref and RefProd
00622   FastTrackerClusterRefProd = e.getRefBeforePut<FastTrackerClusterCollection>("TrackerClusters");
00623   
00624   // Step A: Get Inputs (PSimHit's)
00625   edm::Handle<CrossingFrame<PSimHit> > cf_simhit; 
00626   std::vector<const CrossingFrame<PSimHit> *> cf_simhitvec;
00627   for(uint32_t i=0; i<trackerContainers.size(); i++){
00628     e.getByLabel(trackerContainers[i], cf_simhit);
00629     cf_simhitvec.push_back(cf_simhit.product());
00630   }
00631   std::auto_ptr<MixCollection<PSimHit> > allTrackerHits(new MixCollection<PSimHit>(cf_simhitvec));
00632 
00633   // Step B: create temporary RecHit collection and fill it with Gaussian smeared RecHit's
00634   
00635   //NEW!!!CREATE CLUSTERS AT THE SAME TIME
00636   std::map<unsigned, edm::OwnVector<SiTrackerGSRecHit2D> > temporaryRecHits;
00637   std::map<unsigned, edm::OwnVector<FastTrackerCluster> > theClusters ;
00638  
00639   smearHits( *allTrackerHits, temporaryRecHits, theClusters);
00640   
00641  // Step C: match rechits on stereo layers
00642   std::map<unsigned, edm::OwnVector<SiTrackerGSMatchedRecHit2D> > temporaryMatchedRecHits ;
00643   if(doMatching)  matchHits(  temporaryRecHits,  temporaryMatchedRecHits, *allTrackerHits);
00644 
00645   // Step D: from the temporary RecHit collection, create the real one.
00646   std::auto_ptr<SiTrackerGSRecHit2DCollection> recHitCollection(new SiTrackerGSRecHit2DCollection);
00647   std::auto_ptr<SiTrackerGSMatchedRecHit2DCollection> recHitCollectionMatched(new SiTrackerGSMatchedRecHit2DCollection);
00648   if(doMatching){
00649     loadMatchedRecHits(temporaryMatchedRecHits, *recHitCollectionMatched);
00650     loadRecHits(temporaryRecHits, *recHitCollection);
00651   }
00652   else {
00653     //might need to have a "matched" hit collection containing the simple hits
00654     loadRecHits(temporaryRecHits, *recHitCollection);
00655   }
00656   
00657   
00658 
00659   //std::cout << "****** TrackerGSRecHits hits are =\t" <<  (*recHitCollection).size()<<std::endl;
00660   //std::cout << "****** TrackerGSRecHitsMatched hits are =\t" <<  (*recHitCollectionMatched).size()<< std::endl;
00661   
00662   
00663 
00664   // Step E: write output to file
00665   e.put(recHitCollection,"TrackerGSRecHits");
00666   e.put(recHitCollectionMatched,"TrackerGSMatchedRecHits");
00667   
00668   //STEP F: write clusters
00669   std::auto_ptr<FastTrackerClusterCollection> clusterCollection(new FastTrackerClusterCollection);
00670   loadClusters(theClusters, *clusterCollection);
00671   e.put(clusterCollection,"TrackerClusters");
00672 }
00673 
00674 
00675 
00676 void SiTrackerGaussianSmearingRecHitConverter::smearHits(MixCollection<PSimHit>& input, 
00677                                                          std::map<unsigned, edm::OwnVector<SiTrackerGSRecHit2D> >& temporaryRecHits,
00678                                                          std::map<unsigned, edm::OwnVector<FastTrackerCluster> >& theClusters)
00679 {
00680   
00681   int numberOfPSimHits = 0;
00682   
00683   //  edm::PSimHitContainer::const_iterator isim;
00684   MixCollection<PSimHit>::iterator isim = input.begin();
00685   MixCollection<PSimHit>::iterator lastSimHit = input.end();
00686   correspondingSimHit.resize(input.size());
00687   Local3DPoint position;
00688   LocalError error;
00689   LocalError inflatedError;
00690   
00691   int simHitCounter = -1;
00692   int recHitCounter = 0;
00693   
00694   // loop on PSimHits
00695 
00696   for ( ; isim != lastSimHit; ++isim ) {
00697     ++simHitCounter;
00698     
00699     DetId det((*isim).detUnitId());
00700     unsigned trackID = (*isim).trackId();
00701     uint32_t eeID = (*isim).eventId().rawId(); //get the rawId of the eeid for pileup treatment
00702 
00703 
00704     
00705 
00707 
00708     // unsigned int subdetId = det.subdetId();
00709     // unsigned int  disk  = PXFDetId(det).disk();
00710     // unsigned int  side  = PXFDetId(det).side();
00711     // std::cout<< " Pixel Forward Disk Number : "<< disk << " Side : "<<side<<std::endl; 
00712     // if(subdetId==1 || subdetId==2) std::cout<<" Pixel GSRecHits "<<std::endl;
00713     // else if(subdetId==3|| subdetId==4 || subdetId==5 || subdetId == 6) std::cout<<" Strip GSRecHits "<<std::endl;    
00714 
00715     bool isBad = false;
00716     unsigned int geoId  = det.rawId();
00717     for (size_t id=0;id<numberOfDisabledModules;id++) {
00718       if(geoId==(*disabledModules)[id].DetID){
00719         //  Already selected in the beginRun() the ones with errorType = 0
00720         //      if((*disabledModules)[id].errorType == 0) isBad = true;
00721         isBad = true;
00722         break;
00723       }
00724     }    
00725     if(isBad)      continue;
00726 
00727 
00728     /*
00729     const GeomDet* theDet = geometry->idToDet(det);
00730     std::cout << "SimHit Track/z/r as simulated : "
00731               << trackID << " " 
00732               << theDet->surface().toGlobal((*isim).localPosition()).z() << " " 
00733               << theDet->surface().toGlobal((*isim).localPosition()).perp() << std::endl;
00734     const GeomDet* theMADet = misAlignedGeometry->idToDet(det);
00735     std::cout << "SimHit Track/z/r as reconstructed : "
00736               << trackID << " " 
00737               << theMADet->surface().toGlobal((*isim).localPosition()).z() << " " 
00738               << theMADet->surface().toGlobal((*isim).localPosition()).perp() << std::endl;
00739     */
00740 
00741     ++numberOfPSimHits; 
00742     // gaussian smearing
00743     unsigned int alphaMult = 0;
00744     unsigned int betaMult  = 0;
00745     bool isCreated = gaussianSmearing(*isim, position, error, alphaMult, betaMult);
00746     // std::cout << "Error as simulated     " << error.xx() << " " << error.xy() << " " << error.yy() << std::endl;
00747     //
00748     unsigned int subdet = det.subdetId();
00749     
00750     //
00751     if(isCreated) {
00752       //      double dist = theDet->surface().toGlobal((*isim).localPosition()).mag2();
00753       // create RecHit
00754       // Fill the temporary RecHit on the current DetId collection
00755       //      temporaryRecHits[trackID].push_back(
00756       
00757       // Fix by P.Azzi, A.Schmidt:
00758       // if this hit is on a glued detector with radial topology, 
00759       // the x-coordinate needs to be translated correctly to y=0.
00760       // this is necessary as intermediate step for the matching of hits
00761 
00762       // do it only if we do rec hit matching
00763 //       if(doMatching){
00764 //      StripSubdetector specDetId=StripSubdetector(det);
00765         
00766 //      if(specDetId.glued()) if(subdet==6 || subdet==4){         // check if on double layer in TEC or TID
00767           
00768 //        const GeomDetUnit* theDetUnit = geometry->idToDetUnit(det);
00769 //        const StripTopology& topol=(const StripTopology&)theDetUnit->type().topology();      
00770           
00771 //        // check if radial topology
00772 //        if(dynamic_cast <const RadialStripTopology*>(&topol)){
00773             
00774 //          const RadialStripTopology *rtopol = dynamic_cast <const RadialStripTopology*>(&topol);
00775             
00776 //          // translate to measurement coordinates
00777 //          MeasurementPoint mpoint=rtopol->measurementPosition(position);
00778 //          // set y=0
00779 //          MeasurementPoint mpoint0=MeasurementPoint(mpoint.x(),0.);
00780 //          // translate back to local coordinates with y=0
00781 //          LocalPoint lpoint0 = rtopol->localPosition(mpoint0);
00782 //          position = Local3DPoint(lpoint0.x(),0.,0.);
00783             
00784 //        }
00785 //      } // end if( specDetId.glued()...
00786 //       } // end if(doMatching)
00787       
00788       // set y=0 in single-sided strip detectors 
00789       if(doMatching && (subdet>2)){    
00790         StripSubdetector specDetId=StripSubdetector(det);
00791         if( !specDetId.glued() ){  // this is a single-sided module
00792           position = Local3DPoint(position.x(),0.,0.); // set y to 0 on single-sided modules
00793         }
00794       }
00795       else{  if(subdet>2) position = Local3DPoint(position.x(),0.,0.);    }  // no matching, set y=0 on strips
00796 
00797       // Inflate errors in case of geometry misalignment
00798       const GeomDet* theMADet = misAlignedGeometry->idToDet(det);
00799       if ( theMADet->alignmentPositionError() != 0 ) { 
00800         LocalError lape = 
00801           ErrorFrameTransformer().transform ( theMADet->alignmentPositionError()->globalError(),
00802                                               theMADet->surface() );
00803         error = LocalError ( error.xx()+lape.xx(),
00804                              error.xy()+lape.xy(),
00805                              error.yy()+lape.yy() );
00806       }
00807       
00808       float chargeADC = (*isim).energyLoss()/(GevPerElectron * ElectronsPerADC);
00809 
00810       //create cluster
00811       if(subdet > 2) theClusters[trackID].push_back(
00812                                                     new FastTrackerCluster(LocalPoint(position.x(), 0.0, 0.0), error, det,
00813                                                                            simHitCounter, trackID,
00814                                                                            eeID,
00815                                                                            //(*isim).energyLoss())
00816                                                                            chargeADC)
00817                                                     );
00818       else theClusters[trackID].push_back(
00819                                           new FastTrackerCluster(position, error, det,
00820                                                                  simHitCounter, trackID,
00821                                                                  eeID,
00822                                                                  //(*isim).energyLoss())
00823                                                                  chargeADC)
00824                                           );
00825     
00826       //       std::cout << "CLUSTER for simhit " << simHitCounter << "\t energy loss = " <<chargeADC << std::endl;
00827       
00828       // std::cout << "Error as reconstructed " << error.xx() << " " << error.xy() << " " << error.yy() << std::endl;
00829       
00830       // create rechit
00831       temporaryRecHits[trackID].push_back(
00832                                           new SiTrackerGSRecHit2D(position, error, det, 
00833                                                                   simHitCounter, trackID, 
00834                                                                   eeID, 
00835                                                                   ClusterRef(FastTrackerClusterRefProd, simHitCounter),
00836                                                                   alphaMult, betaMult)
00837                                           );
00838       
00839        // This a correpondence map between RecHits and SimHits 
00840       // (for later  use in matchHits)
00841       correspondingSimHit[recHitCounter++] = isim; 
00842      
00843       
00844     } // end if(isCreated)
00845 
00846   } // end loop on PSimHits
00847 
00848 }
00849 
00850 bool SiTrackerGaussianSmearingRecHitConverter::gaussianSmearing(const PSimHit& simHit, 
00851                                                                 Local3DPoint& position , 
00852                                                                 LocalError& error,
00853                                                                 unsigned& alphaMult, 
00854                                                                 unsigned& betaMult) 
00855 {
00856 
00857   // A few caracteritics of the module which the SimHit belongs to.
00858   unsigned int subdet   = DetId(simHit.detUnitId()).subdetId();
00859   unsigned int detid    = DetId(simHit.detUnitId()).rawId();
00860   const GeomDetUnit* theDetUnit = geometry->idToDetUnit((DetId)simHit.detUnitId());
00861   const BoundPlane& theDetPlane = theDetUnit->surface();
00862   const Bounds& theBounds = theDetPlane.bounds();
00863   double boundX = theBounds.width()/2.;
00864   double boundY = theBounds.length()/2.;
00865   
00866 #ifdef FAMOS_DEBUG
00867   std::cout << "\tSubdetector " << subdet 
00868             << " rawid " << detid
00869             << std::endl;
00870 #endif
00871   if(trackingPSimHits) {
00872     // z is fixed for all detectors, in case of errors resolution is fixed also for x and y to 1 um (zero)
00873     // The Matrix is the Covariance Matrix, sigma^2 on diagonal!!!
00874     error = LocalError( localPositionResolution_z * localPositionResolution_z , 
00875                         0.0 , 
00876                         localPositionResolution_z * localPositionResolution_z  );
00877     //
00878     // starting from PSimHit local position
00879     position = simHit.localPosition();
00880 #ifdef FAMOS_DEBUG
00881     std::cout << " Tracking PSimHit position set to  " << position;
00882 #endif
00883     return true; // RecHit == PSimHit with 100% hit finding efficiency
00884   }
00885   //
00886   
00887   // hit finding probability --> RecHit will be created if and only if hitFindingProbability <= theHitFindingProbability_###
00888   double hitFindingProbability = random->flatShoot();
00889 #ifdef FAMOS_DEBUG
00890   std::cout << " Hit finding probability draw: " << hitFindingProbability << std::endl;;
00891 #endif
00892   
00893   switch (subdet) {
00894     // Pixel Barrel
00895   case 1:
00896     {
00897 #ifdef FAMOS_DEBUG
00898       PXBDetId module(detid);
00899       unsigned int theLayer = module.layer();
00900       std::cout << "\tPixel Barrel Layer " << theLayer << std::endl;
00901 #endif
00902       if( hitFindingProbability > theHitFindingProbability_PXB ) return false;
00903       // Hit smearing
00904       const PixelGeomDetUnit* pixelDetUnit = dynamic_cast<const PixelGeomDetUnit*>(theDetUnit);
00905       thePixelBarrelParametrization->smearHit(simHit, pixelDetUnit, boundX, boundY);
00906       position  = thePixelBarrelParametrization->getPosition();
00907       error     = thePixelBarrelParametrization->getError();
00908       alphaMult = thePixelBarrelParametrization->getPixelMultiplicityAlpha();
00909       betaMult  = thePixelBarrelParametrization->getPixelMultiplicityBeta();
00910       return true;
00911       break;
00912     }
00913     // Pixel Forward
00914   case 2:
00915     {
00916 #ifdef FAMOS_DEBUG
00917       PXFDetId module(detid);
00918       unsigned int theDisk = module.disk();
00919       std::cout << "\tPixel Forward Disk " << theDisk << std::endl;
00920 #endif
00921       if( hitFindingProbability > theHitFindingProbability_PXF ) return false;
00922       // Hit smearing
00923       const PixelGeomDetUnit* pixelDetUnit = dynamic_cast<const PixelGeomDetUnit*>(theDetUnit);
00924       thePixelEndcapParametrization->smearHit(simHit, pixelDetUnit, boundX, boundY);
00925       position = thePixelEndcapParametrization->getPosition();
00926       error    = thePixelEndcapParametrization->getError();
00927       alphaMult = thePixelEndcapParametrization->getPixelMultiplicityAlpha();
00928       betaMult  = thePixelEndcapParametrization->getPixelMultiplicityBeta();
00929       return true;
00930       break;
00931     }
00932     // TIB
00933   case 3:
00934     {
00935       TIBDetId module(detid);
00936       unsigned int theLayer  = module.layer();
00937 #ifdef FAMOS_DEBUG
00938       std::cout << "\tTIB Layer " << theLayer << std::endl;
00939 #endif
00940       //
00941       double resolutionX, resolutionY, resolutionZ;
00942       resolutionZ = localPositionResolution_z;
00943       
00944       switch (theLayer) {
00945       case 1:
00946         {
00947           resolutionX = localPositionResolution_TIB1x;
00948           resolutionY = localPositionResolution_TIB1y;
00949           if( hitFindingProbability > theHitFindingProbability_TIB1 ) return false;
00950           break;
00951         }
00952       case 2:
00953         {
00954           resolutionX = localPositionResolution_TIB2x;
00955           resolutionY = localPositionResolution_TIB2y;
00956           if( hitFindingProbability > theHitFindingProbability_TIB2 ) return false;
00957           break;
00958         }
00959       case 3:
00960         {
00961           resolutionX = localPositionResolution_TIB3x;
00962           resolutionY = localPositionResolution_TIB3y;
00963           if( hitFindingProbability > theHitFindingProbability_TIB3 ) return false;
00964           break;
00965         }
00966       case 4:
00967         {
00968           resolutionX = localPositionResolution_TIB4x;
00969           resolutionY = localPositionResolution_TIB4y;
00970           if( hitFindingProbability > theHitFindingProbability_TIB4 ) return false;
00971           break;
00972         }
00973       default:
00974         {
00975           edm::LogError ("SiTrackerGaussianSmearingRecHits") 
00976             << "\tTIB Layer not valid " << theLayer << std::endl;
00977           return false;
00978           break;
00979         }
00980       }
00981 
00982       // Gaussian smearing
00983       theSiStripErrorParametrization->smearHit(simHit, resolutionX, resolutionY, resolutionZ, boundX, boundY);
00984       position = theSiStripErrorParametrization->getPosition();
00985       error    = theSiStripErrorParametrization->getError();
00986       alphaMult = 0;
00987       betaMult  = 0;
00988       return true;
00989       break;
00990     } // TIB
00991     
00992     // TID
00993   case 4:
00994     {
00995       TIDDetId module(detid);
00996       unsigned int theRing  = module.ring();
00997       double resolutionFactorY = 
00998         1. - simHit.localPosition().y() / theDetPlane.position().perp(); 
00999 
01000 #ifdef FAMOS_DEBUG
01001       std::cout << "\tTID Ring " << theRing << std::endl;
01002 #endif
01003       double resolutionX, resolutionY, resolutionZ;
01004       resolutionZ = localPositionResolution_z;
01005       
01006       switch (theRing) {
01007       case 1:
01008         {
01009           resolutionX = localPositionResolution_TID1x * resolutionFactorY;
01010           resolutionY = localPositionResolution_TID1y;
01011           if( hitFindingProbability > theHitFindingProbability_TID1 ) return false;
01012           break;
01013         }
01014       case 2:
01015         {
01016           resolutionX = localPositionResolution_TID2x * resolutionFactorY;
01017           resolutionY = localPositionResolution_TID2y;
01018           if( hitFindingProbability > theHitFindingProbability_TID2 ) return false;
01019           break;
01020         }
01021       case 3:
01022         {
01023           resolutionX = localPositionResolution_TID3x * resolutionFactorY;
01024           resolutionY = localPositionResolution_TID3y;
01025           if( hitFindingProbability > theHitFindingProbability_TID3 ) return false;
01026           break;
01027         }
01028       default:
01029         {
01030           edm::LogError ("SiTrackerGaussianSmearingRecHits") 
01031             << "\tTID Ring not valid " << theRing << std::endl;
01032           return false;
01033           break;
01034         }
01035       }
01036 
01037       boundX *=  resolutionFactorY;
01038 
01039       theSiStripErrorParametrization->smearHit(simHit, resolutionX, resolutionY, resolutionZ, boundX, boundY);
01040       position = theSiStripErrorParametrization->getPosition();
01041       error    = theSiStripErrorParametrization->getError();
01042       alphaMult = 0;
01043       betaMult  = 0;
01044       return true;
01045       break;
01046     } // TID
01047     
01048     // TOB
01049   case 5:
01050     {
01051       TOBDetId module(detid);
01052       unsigned int theLayer  = module.layer();
01053 #ifdef FAMOS_DEBUG
01054       std::cout << "\tTOB Layer " << theLayer << std::endl;
01055 #endif
01056       double resolutionX, resolutionY, resolutionZ;
01057       resolutionZ = localPositionResolution_z;
01058       
01059       switch (theLayer) {
01060       case 1:
01061         {
01062           resolutionX = localPositionResolution_TOB1x;
01063           resolutionY = localPositionResolution_TOB1y;
01064           if( hitFindingProbability > theHitFindingProbability_TOB1 ) return false;
01065           break;
01066         }
01067       case 2:
01068         {
01069           resolutionX = localPositionResolution_TOB2x;
01070           resolutionY = localPositionResolution_TOB2y;
01071           if( hitFindingProbability > theHitFindingProbability_TOB2 ) return false;
01072           break;
01073         }
01074       case 3:
01075         {
01076           resolutionX = localPositionResolution_TOB3x;
01077           resolutionY = localPositionResolution_TOB3y;
01078           if( hitFindingProbability > theHitFindingProbability_TOB3 ) return false;
01079           break;
01080         }
01081       case 4:
01082         {
01083           resolutionX = localPositionResolution_TOB4x;
01084           resolutionY = localPositionResolution_TOB4y;
01085           if( hitFindingProbability > theHitFindingProbability_TOB4 ) return false;
01086           break;
01087         }
01088       case 5:
01089         {
01090           resolutionX = localPositionResolution_TOB5x;
01091           resolutionY = localPositionResolution_TOB5y;
01092           if( hitFindingProbability > theHitFindingProbability_TOB5 ) return false;
01093           break;
01094         }
01095       case 6:
01096         {
01097           resolutionX = localPositionResolution_TOB6x;
01098           resolutionY = localPositionResolution_TOB6y;
01099           if( hitFindingProbability > theHitFindingProbability_TOB6 ) return false;
01100           break;
01101         }
01102       default:
01103         {
01104           edm::LogError ("SiTrackerGaussianSmearingRecHits") 
01105             << "\tTOB Layer not valid " << theLayer << std::endl;
01106           return false;
01107           break;
01108         }
01109       }
01110       theSiStripErrorParametrization->smearHit(simHit, resolutionX, resolutionY, resolutionZ, boundX, boundY);
01111       position = theSiStripErrorParametrization->getPosition();
01112       error    = theSiStripErrorParametrization->getError();
01113       alphaMult = 0;
01114       betaMult  = 0;
01115       return true;
01116       break;
01117     } // TOB
01118     
01119     // TEC
01120   case 6:
01121     {
01122       TECDetId module(detid);
01123       unsigned int theRing  = module.ring();
01124       double resolutionFactorY = 
01125         1. - simHit.localPosition().y() / theDetPlane.position().perp(); 
01126 
01127 #ifdef FAMOS_DEBUG
01128       std::cout << "\tTEC Ring " << theRing << std::endl;
01129 #endif
01130       double resolutionX, resolutionY, resolutionZ;
01131       resolutionZ = localPositionResolution_z * localPositionResolution_z;
01132       
01133       switch (theRing) {
01134       case 1:
01135         {
01136           resolutionX = localPositionResolution_TEC1x * resolutionFactorY;
01137           resolutionY = localPositionResolution_TEC1y;
01138           if( hitFindingProbability > theHitFindingProbability_TEC1 ) return false;
01139           break;
01140         }
01141       case 2:
01142         {
01143           resolutionX = localPositionResolution_TEC2x * resolutionFactorY;
01144           resolutionY = localPositionResolution_TEC2y;
01145           if( hitFindingProbability > theHitFindingProbability_TEC2 ) return false;
01146           break;
01147         }
01148       case 3:
01149         {
01150           resolutionX = localPositionResolution_TEC3x * resolutionFactorY;
01151           resolutionY = localPositionResolution_TEC3y;
01152           if( hitFindingProbability > theHitFindingProbability_TEC3 ) return false;
01153           break;
01154         }
01155       case 4:
01156         {
01157           resolutionX = localPositionResolution_TEC4x * resolutionFactorY;
01158           resolutionY = localPositionResolution_TEC4y;
01159           if( hitFindingProbability > theHitFindingProbability_TEC4 ) return false;
01160           break;
01161         }
01162       case 5:
01163         {
01164           resolutionX = localPositionResolution_TEC5x * resolutionFactorY;
01165           resolutionY = localPositionResolution_TEC5y;
01166           if( hitFindingProbability > theHitFindingProbability_TEC5 ) return false;
01167           break;
01168         }
01169       case 6:
01170         {
01171           resolutionX = localPositionResolution_TEC6x * resolutionFactorY;
01172           resolutionY = localPositionResolution_TEC6y;
01173           if( hitFindingProbability > theHitFindingProbability_TEC6 ) return false;
01174           break;
01175         }
01176       case 7:
01177         {
01178           resolutionX = localPositionResolution_TEC7x * resolutionFactorY;
01179           resolutionY = localPositionResolution_TEC7y;
01180           if( hitFindingProbability > theHitFindingProbability_TEC7 ) return false;
01181           break;
01182         }
01183       default:
01184         {
01185           edm::LogError ("SiTrackerGaussianSmearingRecHits") 
01186             << "\tTEC Ring not valid " << theRing << std::endl;
01187           return false;
01188           break;
01189         }
01190       }
01191 
01192       boundX *= resolutionFactorY;
01193       theSiStripErrorParametrization->smearHit(simHit, resolutionX, resolutionY, resolutionZ, boundX, boundY);
01194       position = theSiStripErrorParametrization->getPosition();
01195       error    = theSiStripErrorParametrization->getError();
01196       alphaMult = 0;
01197       betaMult  = 0;
01198       return true;
01199       break;
01200     } // TEC
01201     
01202   default:
01203     {
01204       edm::LogError ("SiTrackerGaussianSmearingRecHits") << "\tTracker subdetector not valid " << subdet << std::endl;
01205       return false;
01206       break;
01207     }
01208     
01209   } // subdetector case
01210     //
01211 }   
01212 
01213 
01214 
01215 void 
01216     SiTrackerGaussianSmearingRecHitConverter::loadClusters(
01217     std::map<unsigned,edm::OwnVector<FastTrackerCluster> >& theClusterMap, 
01218     FastTrackerClusterCollection& theClusterCollection) const
01219 {
01220   std::map<unsigned,edm::OwnVector<FastTrackerCluster> >::const_iterator 
01221       it = theClusterMap.begin();
01222   std::map<unsigned,edm::OwnVector<FastTrackerCluster> >::const_iterator 
01223       lastCluster = theClusterMap.end();
01224   
01225   for( ; it != lastCluster ; ++it ) { 
01226     theClusterCollection.put(it->first,(it->second).begin(),(it->second).end());
01227   }
01228 }
01229 
01230 
01231 
01232 void 
01233 SiTrackerGaussianSmearingRecHitConverter::loadRecHits(
01234      std::map<unsigned,edm::OwnVector<SiTrackerGSRecHit2D> >& theRecHits, 
01235      SiTrackerGSRecHit2DCollection& theRecHitCollection) const
01236 {
01237   std::map<unsigned,edm::OwnVector<SiTrackerGSRecHit2D> >::const_iterator 
01238     it = theRecHits.begin();
01239   std::map<unsigned,edm::OwnVector<SiTrackerGSRecHit2D> >::const_iterator 
01240     lastRecHit = theRecHits.end();
01241 
01242   for( ; it != lastRecHit ; ++it ) { 
01243     theRecHitCollection.put(it->first,(it->second).begin(),(it->second).end());
01244   }
01245 
01246 }
01247 
01248 void 
01249 SiTrackerGaussianSmearingRecHitConverter::loadMatchedRecHits(
01250      std::map<unsigned,edm::OwnVector<SiTrackerGSMatchedRecHit2D> >& theRecHits, 
01251      SiTrackerGSMatchedRecHit2DCollection& theRecHitCollection) const
01252 {
01253   std::map<unsigned,edm::OwnVector<SiTrackerGSMatchedRecHit2D> >::const_iterator 
01254     it = theRecHits.begin();
01255   std::map<unsigned,edm::OwnVector<SiTrackerGSMatchedRecHit2D> >::const_iterator 
01256     lastRecHit = theRecHits.end();
01257 
01258   for( ; it != lastRecHit ; ++it ) { 
01259     theRecHitCollection.put(it->first,(it->second).begin(),(it->second).end());
01260   }
01261 
01262 }
01263 
01264 
01265 void
01266 SiTrackerGaussianSmearingRecHitConverter::matchHits(
01267   std::map<unsigned, edm::OwnVector<SiTrackerGSRecHit2D> >& theRecHits, 
01268   std::map<unsigned, edm::OwnVector<SiTrackerGSMatchedRecHit2D> >& matchedMap, 
01269   MixCollection<PSimHit>& simhits ) {
01270 
01271   std::map<unsigned, edm::OwnVector<SiTrackerGSRecHit2D> >::iterator it = theRecHits.begin();
01272   std::map<unsigned, edm::OwnVector<SiTrackerGSRecHit2D> >::iterator lastTrack = theRecHits.end();
01273 
01274 
01275   int recHitCounter = 0;
01276 
01277   //loop over single sided tracker RecHit
01278   for(  ; it !=  lastTrack; ++it ) {
01279 
01280     edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator rit = it->second.begin();
01281     edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator firstRecHit = it->second.begin();
01282     edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator lastRecHit = it->second.end();
01283 
01284     //loop over rechits in track
01285     for ( ; rit != lastRecHit; ++rit,++recHitCounter){
01286 
01287       DetId detid = rit->geographicalId();
01288       unsigned int subdet = detid.subdetId();
01289       // if in the strip (subdet>2)
01290       if(subdet>2){
01291 
01292         StripSubdetector specDetId=StripSubdetector(detid);
01293 
01294         // if this is on a glued, then place only one hit in vector
01295         if(specDetId.glued()){
01296 
01297           // get the track direction from the simhit
01298           LocalVector simtrackdir = correspondingSimHit[recHitCounter]->localDirection();           
01299 
01300           const GluedGeomDet* gluedDet = (const GluedGeomDet*)geometry->idToDet(DetId(specDetId.glued()));
01301           const StripGeomDetUnit* stripdet =(StripGeomDetUnit*) gluedDet->stereoDet();
01302           
01303           // global direction of track
01304           GlobalVector globaldir= stripdet->surface().toGlobal(simtrackdir);
01305           LocalVector gluedsimtrackdir=gluedDet->surface().toLocal(globaldir);
01306 
01307           // get partner layer, it is the next one or previous one in the vector
01308           edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator partner = rit;
01309           edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator partnerNext = rit;
01310           edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator partnerPrev = rit;
01311           partnerNext++; partnerPrev--;
01312           
01313           // check if this hit is on a stereo layer (== the second layer of a double sided module)
01314           if(   specDetId.stereo()  ) {
01315         
01316             int partnersFound = 0;
01317             // check next one in vector
01318             // safety check first
01319             if(partnerNext != it->second.end() ) 
01320               if( StripSubdetector( partnerNext->geographicalId() ).partnerDetId() == detid.rawId() )   {
01321                 partner= partnerNext;
01322                 partnersFound++;
01323               }
01324             // check prevoius one in vector     
01325             if( rit !=  it->second.begin()) 
01326               if(  StripSubdetector( partnerPrev->geographicalId() ).partnerDetId() == detid.rawId() ) {
01327                 partnersFound++;
01328                 partner= partnerPrev;
01329               }
01330             
01331             
01332             // in case partner has not been found this way, need to loop over all rechits in track to be sure
01333             // (rare cases fortunately)
01334             if(partnersFound==0){
01335               for(edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator iter = firstRecHit; iter != lastRecHit; ++iter  ){
01336                 if( StripSubdetector( iter->geographicalId() ).partnerDetId() == detid.rawId()){
01337                   partnersFound++;
01338                   partner = iter;
01339                 }
01340               }
01341             }
01342 
01343             if(partnersFound == 1) {
01344               SiTrackerGSMatchedRecHit2D * theMatchedHit =GSRecHitMatcher().match( &(*partner),  &(*rit),  gluedDet  , gluedsimtrackdir );
01345 
01346 
01347               //              std::cout << "Matched  hit: isMatched =\t" << theMatchedHit->isMatched() << ", "
01348               //        <<  theMatchedHit->monoHit() << ", " <<  theMatchedHit->stereoHit() << std::endl;
01349 
01350               matchedMap[it->first].push_back( theMatchedHit );
01351             } 
01352             else{
01353               // no partner to match, place projected one in map
01354               SiTrackerGSMatchedRecHit2D * theProjectedHit = GSRecHitMatcher().projectOnly( &(*rit), geometry->idToDet(detid),gluedDet, gluedsimtrackdir  );
01355               matchedMap[it->first].push_back( theProjectedHit );
01356               //there is no partner here
01357             }
01358           } // end if stereo
01359           else {   // we are on a mono layer
01360             // usually this hit is already matched, but not if stereo hit is missing (rare cases)
01361             // check if stereo hit is missing
01362             int partnersFound = 0;
01363             // check next one in vector
01364             // safety check first
01365             if(partnerNext != it->second.end() ) 
01366               if( StripSubdetector( partnerNext->geographicalId() ).partnerDetId() == detid.rawId() )   {
01367                 partnersFound++;
01368               }
01369             // check prevoius one in vector     
01370             if( rit !=  it->second.begin()) 
01371               if(  StripSubdetector( partnerPrev->geographicalId() ).partnerDetId() == detid.rawId() ) {
01372                 partnersFound++;
01373               }
01374 
01375             if(partnersFound==0){ // no partner hit found this way, need to iterate over all hits to be sure (rare cases)
01376               for(edm::OwnVector<SiTrackerGSRecHit2D>::const_iterator iter = firstRecHit; iter != lastRecHit; ++iter  ){
01377                 if( StripSubdetector( iter->geographicalId() ).partnerDetId() == detid.rawId()){
01378                   partnersFound++;
01379                 }
01380               }
01381             }       
01382             
01383             
01384             if(partnersFound==0){ // no partner hit found 
01385               // no partner to match, place projected one one in map
01386               SiTrackerGSMatchedRecHit2D * theProjectedHit = 
01387                 GSRecHitMatcher().projectOnly( &(*rit), geometry->idToDet(detid),gluedDet, gluedsimtrackdir  );
01388               
01389               //std::cout << "Projected hit: isMatched =\t" << theProjectedHit->isMatched() << ", "
01390               //        <<  theProjectedHit->monoHit() << ", " <<  theProjectedHit->stereoHit() << std::endl;
01391               
01392               matchedMap[it->first].push_back( theProjectedHit );
01393             }       
01394           } // end we are on a a mono layer
01395         } // end if glued 
01396         // else matchedMap[it->first].push_back( rit->clone() );  // if not glued place the original one in vector 
01397         else{ //need to copy the original in a "matched" type rechit
01398 
01399           SiTrackerGSMatchedRecHit2D* rit_copy = new SiTrackerGSMatchedRecHit2D(rit->localPosition(), rit->localPositionError(),
01400                                                                                 rit->geographicalId(), 
01401                                                                                 rit->simhitId(), rit->simtrackId(), rit->eeId(),
01402                                                                                 rit->cluster(),
01403                                                                                 rit->simMultX(), rit->simMultY()); 
01404           //std::cout << "Simple hit  hit: isMatched =\t" << rit_copy->isMatched() << ", "
01405           //    <<  rit_copy->monoHit() << ", " <<  rit_copy->stereoHit() << std::endl;
01406           
01407           matchedMap[it->first].push_back( rit_copy );  // if not strip place the original one in vector (making it into a matched)     
01408         }
01409         
01410       }// end if strip
01411       //      else  matchedMap[it->first].push_back( rit->clone() );  // if not strip place the original one in vector
01412       else { //need to copy the original in a "matched" type rechit
01413 
01414         SiTrackerGSMatchedRecHit2D* rit_copy = new SiTrackerGSMatchedRecHit2D(rit->localPosition(), rit->localPositionError(),
01415                                                                               rit->geographicalId(), 
01416                                                                               rit->simhitId(), rit->simtrackId(), rit->eeId(), 
01417                                                                               rit->cluster(),
01418                                                                               rit->simMultX(), rit->simMultY());        
01419         
01420         //std::cout << "Simple hit  hit: isMatched =\t" << rit_copy->isMatched() << ", "
01421         //        <<  rit_copy->monoHit() << ", " <<  rit_copy->stereoHit() << std::endl;
01422         matchedMap[it->first].push_back( rit_copy );  // if not strip place the original one in vector (makining it into a matched)
01423      }
01424       
01425     } // end loop over rechits
01426     
01427   }// end loop over tracks
01428   
01429   
01430 }