CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CalibTracker/SiStripESProducers/plugins/fake/SiStripNoiseNormalizedWithApvGainBuilder.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiStripESProducers/plugins/fake/SiStripNoiseNormalizedWithApvGainBuilder.h"
00002 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
00003 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
00004 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00005 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
00006 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00007 #include "FWCore/Framework/interface/EventSetup.h"
00008 #include <iostream>
00009 #include <fstream>
00010 #include <vector>
00011 #include <algorithm>
00012 
00013 SiStripNoiseNormalizedWithApvGainBuilder::SiStripNoiseNormalizedWithApvGainBuilder( const edm::ParameterSet& iConfig ):
00014   fp_(iConfig.getUntrackedParameter<edm::FileInPath>("file",edm::FileInPath("CalibTracker/SiStripCommon/data/SiStripDetInfo.dat"))),
00015   printdebug_(iConfig.getUntrackedParameter<uint32_t>("printDebug",1)),
00016   pset_(iConfig),
00017   electronsPerADC_(0.),
00018   minimumPosValue_(0.),
00019   stripLengthMode_(true),
00020   printDebug_(0)
00021 {}
00022 
00023 void SiStripNoiseNormalizedWithApvGainBuilder::analyze(const edm::Event& evt, const edm::EventSetup& iSetup)
00024 {
00025   //Retrieve tracker topology from geometry
00026   edm::ESHandle<TrackerTopology> tTopoHandle;
00027   iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
00028   const TrackerTopology* const tTopo = tTopoHandle.product();
00029  
00030   // Read the gain from the given tag
00031   edm::ESHandle<SiStripApvGain> inputApvGain;
00032   iSetup.get<SiStripApvGainRcd>().get( inputApvGain );
00033   std::vector<uint32_t> inputDetIds;
00034   inputApvGain->getDetIds(inputDetIds);
00035 
00036   // Prepare the new object
00037   SiStripNoises* obj = new SiStripNoises();
00038 
00039   SiStripDetInfoFileReader reader(fp_.fullPath());
00040 
00041 
00042 
00043   stripLengthMode_ = pset_.getParameter<bool>("StripLengthMode");
00044   
00045   //parameters for random noise generation. not used if Strip length mode is chosen
00046   std::map<int, std::vector<double> > meanNoise;
00047   fillParameters(meanNoise, "MeanNoise");
00048   std::map<int, std::vector<double> > sigmaNoise;
00049   fillParameters(sigmaNoise, "SigmaNoise");
00050   minimumPosValue_ = pset_.getParameter<double>("MinPositiveNoise");
00051 
00052   //parameters for strip length proportional noise generation. not used if random mode is chosen
00053   std::map<int, std::vector<double> > noiseStripLengthLinearSlope;
00054   fillParameters(noiseStripLengthLinearSlope, "NoiseStripLengthSlope");
00055   std::map<int, std::vector<double> > noiseStripLengthLinearQuote;
00056   fillParameters(noiseStripLengthLinearQuote, "NoiseStripLengthQuote");
00057   electronsPerADC_ = pset_.getParameter<double>("electronPerAdc");        
00058 
00059   printDebug_ = pset_.getUntrackedParameter<uint32_t>("printDebug", 5);
00060 
00061   unsigned int count = 0;
00062   const std::map<uint32_t, SiStripDetInfoFileReader::DetInfo > DetInfos = reader.getAllData();
00063   for(std::map<uint32_t, SiStripDetInfoFileReader::DetInfo >::const_iterator it = DetInfos.begin(); it != DetInfos.end(); it++) {
00064 
00065     // Find if this DetId is in the input tag and if so how many are the Apvs for which it contains information
00066     SiStripApvGain::Range inputRange(inputApvGain->getRange(it->first));
00067 
00068     //Generate Noises for det detid
00069     SiStripNoises::InputVector theSiStripVector;
00070     float noise = 0.;
00071     uint32_t detId = it->first;
00072     std::pair<int, int> sl = subDetAndLayer(detId, tTopo);
00073     unsigned short nApvs = it->second.nApvs;
00074 
00075     if(stripLengthMode_) {
00076       // Use strip length
00077       double linearSlope = noiseStripLengthLinearSlope[sl.first][sl.second];
00078       double linearQuote = noiseStripLengthLinearQuote[sl.first][sl.second];
00079       double stripLength = it->second.stripLength;
00080       for( unsigned short j=0; j<nApvs; ++j ) {
00081 
00082         double gain = inputApvGain->getApvGain(j, inputRange);
00083 
00084         for( unsigned short stripId = 0; stripId < 128; ++stripId ) {
00085           noise = ( ( linearSlope*stripLength + linearQuote) / electronsPerADC_ ) * gain;
00086           if( count<printDebug_ ) printLog(detId, stripId+128*j, noise);
00087           obj->setData(noise, theSiStripVector);
00088         }
00089       }
00090     }
00091     else {
00092       // Use random generator
00093       double meanN = meanNoise[sl.first][sl.second];
00094       double sigmaN = sigmaNoise[sl.first][sl.second];
00095       for( unsigned short j=0; j<nApvs; ++j ) {
00096 
00097         double gain = inputApvGain->getApvGain(j, inputRange);
00098 
00099         for( unsigned short stripId = 0; stripId < 128; ++stripId ) {
00100           noise = ( CLHEP::RandGauss::shoot(meanN, sigmaN) ) * gain;
00101           if( noise<=minimumPosValue_ ) noise = minimumPosValue_;
00102           if( count<printDebug_ ) printLog(detId, stripId+128*j, noise);
00103           obj->setData(noise, theSiStripVector);
00104         }
00105       }
00106     }
00107     ++count;
00108     
00109     if ( ! obj->put(it->first,theSiStripVector) ) {
00110       edm::LogError("SiStripNoisesFakeESSource::produce ")<<" detid already exists"<<std::endl;
00111     }
00112   }
00113   
00114   //End now write data in DB
00115   edm::Service<cond::service::PoolDBOutputService> mydbservice;
00116   
00117   if( mydbservice.isAvailable() ){
00118     if( mydbservice->isNewTagRequest("SiStripNoisesRcd") ){
00119       mydbservice->createNewIOV<SiStripNoises>(obj,mydbservice->beginOfTime(),mydbservice->endOfTime(),"SiStripNoisesRcd");
00120     }
00121     else {
00122       mydbservice->appendSinceTime<SiStripNoises>(obj,mydbservice->currentTime(),"SiStripNoisesRcd");
00123     }
00124   }
00125   else {
00126     edm::LogError("SiStripNoiseNormalizedWithApvGainBuilder")<<"Service is unavailable"<<std::endl;
00127   }
00128 }
00129 
00130 std::pair<int, int> SiStripNoiseNormalizedWithApvGainBuilder::subDetAndLayer(const uint32_t detId, const TrackerTopology* tTopo) const
00131 {
00132   int layerId = 0;
00133 
00134   StripSubdetector subid(detId);
00135   int subId = subid.subdetId();
00136 
00137   if( subId == int(StripSubdetector::TIB)) {
00138     layerId = tTopo->tibLayer(detId) - 1;
00139   }
00140   else if(subId == int(StripSubdetector::TOB)) {
00141     layerId = tTopo->tobLayer(detId) - 1;
00142   }
00143   else if(subId == int(StripSubdetector::TID)) {
00144     layerId = tTopo->tidRing(detId) - 1;
00145   }
00146   if(subId == int(StripSubdetector::TEC)) {
00147     layerId = tTopo->tecRing(detId) - 1;
00148   }
00149   return std::make_pair(subId, layerId);
00150 }
00151 
00152 void SiStripNoiseNormalizedWithApvGainBuilder::fillParameters(std::map<int, std::vector<double> > & mapToFill, const std::string & parameterName) const
00153 {
00154   int layersTIB = 4;
00155   int ringsTID = 3;
00156   int layersTOB = 6;
00157   int ringsTEC = 7;
00158 
00159   fillSubDetParameter( mapToFill, pset_.getParameter<std::vector<double> >(parameterName+"TIB"), int(StripSubdetector::TIB), layersTIB );
00160   fillSubDetParameter( mapToFill, pset_.getParameter<std::vector<double> >(parameterName+"TID"), int(StripSubdetector::TID), ringsTID );
00161   fillSubDetParameter( mapToFill, pset_.getParameter<std::vector<double> >(parameterName+"TOB"), int(StripSubdetector::TOB), layersTOB );
00162   fillSubDetParameter( mapToFill, pset_.getParameter<std::vector<double> >(parameterName+"TEC"), int(StripSubdetector::TEC), ringsTEC );
00163 }
00164 
00165 void SiStripNoiseNormalizedWithApvGainBuilder::fillSubDetParameter(std::map<int, std::vector<double> > & mapToFill, const std::vector<double> & v, const int subDet, const unsigned short layers) const
00166 {
00167   if( v.size() == layers ) {
00168     mapToFill.insert(std::make_pair( subDet, v ));
00169   }
00170   else if( v.size() == 1 ) {
00171     std::vector<double> parV(layers, v[0]);
00172     mapToFill.insert(std::make_pair( subDet, parV ));
00173   }
00174   else {
00175     throw cms::Exception("Configuration") << "ERROR: number of parameters for subDet " << subDet << " are " << v.size() << ". They must be either 1 or " << layers << std::endl;
00176   }
00177 }