CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/SimCalorimetry/HcalSimAlgos/src/HPDIonFeedbackSim.cc

Go to the documentation of this file.
00001 
00002 // --------------------------------------------------------
00003 // A class to simulated HPD ion feedback noise.
00004 // The deliverable of the class is the ion feedback noise
00005 // for an HcalDetId units of fC or GeV
00006 //
00007 // Project: HPD ion feedback
00008 // Author: T.Yetkin University of Iowa, Feb. 16, 2010
00009 // $Id: HPDIonFeedbackSim.cc,v 1.4 2010/10/01 14:51:50 sunanda Exp $
00010 // --------------------------------------------------------
00011 
00012 #include "SimCalorimetry/HcalSimAlgos/interface/HPDIonFeedbackSim.h"
00013 #include "SimCalorimetry/HcalSimAlgos/interface/HcalShape.h"
00014 #include "CondFormats/HcalObjects/interface/HcalGain.h"
00015 #include "CondFormats/HcalObjects/interface/HcalGainWidth.h"
00016 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00018 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00019 #include "FWCore/ServiceRegistry/interface/Service.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include "FWCore/PluginManager/interface/PluginManager.h"
00022 #include "FWCore/PluginManager/interface/standard.h"
00023 
00024 
00025 using namespace edm;
00026 using namespace std;
00027 
00028 // constants for simulation/parameterization
00029 double pe2Charge = 0.333333;    // fC/p.e.
00030 
00031 HPDIonFeedbackSim::HPDIonFeedbackSim(const edm::ParameterSet & iConfig)
00032 : theDbService(0), theShape(0),
00033  theRandBinomial(0), theRandFlat(0), theRandGauss(0), theRandPoissonQ(0)
00034 {
00035 }
00036 
00037 HPDIonFeedbackSim::~HPDIonFeedbackSim() 
00038 {
00039     if(theRandBinomial) delete theRandBinomial;
00040     if (theRandFlat) delete theRandFlat;
00041     if (theRandGauss) delete theRandGauss;
00042     if (theRandPoissonQ) delete theRandPoissonQ;
00043 }
00044 
00045 
00046 void HPDIonFeedbackSim::setRandomEngine(CLHEP::HepRandomEngine & engine) 
00047 {
00048     if(theRandBinomial) delete theRandBinomial;
00049     if (theRandFlat) delete theRandFlat;
00050     if (theRandGauss) delete theRandGauss;
00051     if (theRandPoissonQ) delete theRandPoissonQ;
00052 
00053     theRandBinomial = new CLHEP::RandBinomial(engine);
00054     theRandFlat = new CLHEP::RandFlat(engine);
00055     theRandGauss = new CLHEP::RandGaussQ(engine);
00056     theRandPoissonQ = new CLHEP::RandPoissonQ(engine);
00057 }
00058 
00059 double HPDIonFeedbackSim::getIonFeedback(DetId detId, double signal, double pedWidth, bool doThermal, bool isInGeV) 
00060 {
00061     
00062     HcalDetId id = detId;
00063     
00064     double GeVperfC = 1.;
00065     if(isInGeV) GeVperfC = 1./fCtoGeV(detId);
00066     
00067     double charge = signal / GeVperfC;
00068     
00069     double noise = 0.;          // fC
00070     if (charge > 3. * pedWidth) {    // 3 sigma away from pedestal mean
00071         int npe = int (charge / pe2Charge);
00072         if(doThermal) {
00073           double electronEmission = 0.08;
00074           npe += theRandPoissonQ->fire(electronEmission);
00075         }
00076 
00077         noise = correctPE(detId, npe) - npe;
00078     }
00079     return (noise * GeVperfC);
00080 
00081 }
00082 
00083 
00084 double HPDIonFeedbackSim::correctPE(const DetId & detId, double npe) const
00085 {
00086     double rateInTail = 0.000211988;//read this from XML file
00087     double rateInSecondTail = 4.61579e-06;//read this from XML file
00088 
00089     // three gauss fit is applied to data to get ion feedback distribution
00090     // parameters (in fC)
00091     // first gaussian
00092     // double p0 = 9.53192e+05;
00093     // double p1 = -3.13653e-01;
00094     // double p2 = 2.78350e+00;
00095 
00096     // second gaussian
00097     // double p3 = 2.41611e+03;
00098     double p4 = 2.06117e+01;
00099     double p5 = 1.09239e+01;
00100 
00101     // third gaussian
00102     // double p6 = 3.42793e+01;
00103     double p7 = 5.45548e+01;
00104     double p8 = 1.59696e+01;
00105 
00106     double noise = 0.;          // fC
00107     int nFirst  = (int)(theRandBinomial->fire(npe, rateInTail));
00108     int nSecond = (int)(theRandBinomial->fire(npe, rateInSecondTail));
00109 
00110     for (int j = 0; j < nFirst; ++j) {
00111       noise += theRandGauss->fire(p4, p5);
00112     }
00113     for (int j = 0; j < nSecond; ++j) {
00114       noise += theRandGauss->fire(p7, p8);
00115     }
00116 
00117     return npe + std::max(noise/pe2Charge, 0.);
00118 }
00119 
00120 
00121 void HPDIonFeedbackSim::addThermalNoise(CaloSamples & samples)
00122 {
00123   // make some chance to add a PE (with a chance of feedback)
00124   // for each time sample
00125   double meanPE = 0.02;
00126   DetId detId(samples.id());
00127   int nSamples = samples.size();
00128   for(int i = 0; i < nSamples; ++i) 
00129   {
00130     double npe = theRandPoissonQ->fire(meanPE);
00131     // TODOprobably should time-smear these
00132     if(npe > 0.)
00133     {
00134       // chance of feedback
00135       npe = correctPE(detId, npe);
00136       for(int j = i; j < nSamples; ++j)
00137       {
00138         double timeFromPE = (j-i) * 25.;
00139         samples[j] += (*theShape)(timeFromPE) * npe;
00140       }
00141     }
00142   }
00143 }
00144 
00145 
00146 double HPDIonFeedbackSim::fCtoGeV(const DetId & detId) const
00147 {
00148   assert(theDbService != 0);
00149   HcalGenericDetId hcalGenDetId(detId);
00150   const HcalGain* gains = theDbService->getGain(hcalGenDetId);
00151   const HcalGainWidth* gwidths = theDbService->getGainWidth(hcalGenDetId);
00152   if (!gains || !gwidths )
00153   {
00154     edm::LogError("HcalAmplifier") << "Could not fetch HCAL conditions for channel " << hcalGenDetId;
00155   }
00156   // only one gain will be recorded per channel, so just use capID 0 for now
00157   double result = gains->getValue(0);
00158   return result;
00159 }