CMS 3D CMS Logo

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