CMS 3D CMS Logo

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

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