CMS 3D CMS Logo

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