CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/SimMuon/RPCDigitizer/src/RPCSimAverageNoiseEff.cc

Go to the documentation of this file.
00001 #include "Geometry/RPCGeometry/interface/RPCRoll.h"
00002 #include "Geometry/RPCGeometry/interface/RPCRollSpecs.h"
00003 #include "SimMuon/RPCDigitizer/src/RPCSimAverageNoiseEff.h"
00004 #include "SimMuon/RPCDigitizer/src/RPCSimSetUp.h"
00005 
00006 #include "SimMuon/RPCDigitizer/src/RPCSynchronizer.h"
00007 #include "Geometry/CommonTopologies/interface/RectangularStripTopology.h"
00008 #include "Geometry/CommonTopologies/interface/TrapezoidalStripTopology.h"
00009 #include "Geometry/RPCGeometry/interface/RPCGeomServ.h"
00010 
00011 #include <cmath>
00012 #include "FWCore/ServiceRegistry/interface/Service.h"
00013 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00014 #include "FWCore/Utilities/interface/Exception.h"
00015 #include "CLHEP/Random/RandomEngine.h"
00016 #include "CLHEP/Random/RandFlat.h"
00017 #include <CLHEP/Random/RandGaussQ.h>
00018 #include <CLHEP/Random/RandFlat.h>
00019 
00020 #include <FWCore/Framework/interface/Frameworkfwd.h>
00021 #include <FWCore/Framework/interface/EventSetup.h>
00022 #include <FWCore/Framework/interface/EDAnalyzer.h>
00023 #include <FWCore/Framework/interface/Event.h>
00024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00025 #include <FWCore/Framework/interface/ESHandle.h>
00026 
00027 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00028 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00029 #include "Geometry/RPCGeometry/interface/RPCGeometry.h"
00030 #include <Geometry/Records/interface/MuonGeometryRecord.h>
00031 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00032 #include "SimMuon/RPCDigitizer/src/RPCSimSetUp.h"
00033 
00034 #include<cstring>
00035 #include<iostream>
00036 #include<fstream>
00037 #include<string>
00038 #include<vector>
00039 #include<stdlib.h>
00040 #include <utility>
00041 #include <map>
00042 
00043 //#include "CLHEP/config/CLHEP.h"
00044 #include "CLHEP/Random/Random.h"
00045 #include "CLHEP/Random/RandFlat.h"
00046 #include "CLHEP/Random/RandPoissonQ.h"
00047 
00048 using namespace std;
00049 
00050 RPCSimAverageNoiseEff::RPCSimAverageNoiseEff(const edm::ParameterSet& config) : 
00051   RPCSim(config)
00052 {
00053 
00054   aveEff = config.getParameter<double>("averageEfficiency");
00055   aveCls = config.getParameter<double>("averageClusterSize");
00056   resRPC = config.getParameter<double>("timeResolution");
00057   timOff = config.getParameter<double>("timingRPCOffset");
00058   dtimCs = config.getParameter<double>("deltatimeAdjacentStrip");
00059   resEle = config.getParameter<double>("timeJitter");
00060   sspeed = config.getParameter<double>("signalPropagationSpeed");
00061   lbGate = config.getParameter<double>("linkGateWidth");
00062   rpcdigiprint = config.getParameter<bool>("printOutDigitizer");
00063 
00064   rate=config.getParameter<double>("Rate");
00065   nbxing=config.getParameter<int>("Nbxing");
00066   gate=config.getParameter<double>("Gate");
00067   frate=config.getParameter<double>("Frate");
00068 
00069   if (rpcdigiprint) {
00070     std::cout <<"Average Efficiency        = "<<aveEff<<std::endl;
00071     std::cout <<"Average Cluster Size      = "<<aveCls<<" strips"<<std::endl;
00072     std::cout <<"RPC Time Resolution       = "<<resRPC<<" ns"<<std::endl;
00073     std::cout <<"RPC Signal formation time = "<<timOff<<" ns"<<std::endl;
00074     std::cout <<"RPC adjacent strip delay  = "<<dtimCs<<" ns"<<std::endl;
00075     std::cout <<"Electronic Jitter         = "<<resEle<<" ns"<<std::endl;
00076     std::cout <<"Signal propagation time   = "<<sspeed<<" x c"<<std::endl;
00077     std::cout <<"Link Board Gate Width     = "<<lbGate<<" ns"<<std::endl;
00078   }
00079 
00080   _rpcSync = new RPCSynchronizer(config);
00081 
00082 }
00083 
00084 void RPCSimAverageNoiseEff::setRandomEngine(CLHEP::HepRandomEngine& eng){
00085   flatDistribution = new CLHEP::RandFlat(eng);
00086   flatDistribution2 = new CLHEP::RandFlat(eng);
00087   poissonDistribution_ = new CLHEP::RandPoissonQ(eng);
00088   _rpcSync->setRandomEngine(eng);
00089 }
00090 
00091 RPCSimAverageNoiseEff::~RPCSimAverageNoiseEff()
00092 {
00093   //Deleting the distribution defined in the constructor
00094   delete flatDistribution;
00095   delete flatDistribution2;
00096   delete poissonDistribution_;
00097   delete _rpcSync;
00098 }
00099 
00100 int RPCSimAverageNoiseEff::getClSize(float posX)
00101 {
00102 
00103   std::map< int, std::vector<double> > clsMap = getRPCSimSetUp()->getClsMap();
00104 
00105   int cnt = 1;
00106   int min = 1;
00107   double func=0.0;
00108   std::vector<double> sum_clsize;
00109 
00110   double rr_cl = flatDistribution->fire(1);
00111   if(0.0 <= posX && posX < 0.2)  {
00112     func = (clsMap[1])[(clsMap[1]).size()-1]*(rr_cl);
00113     sum_clsize = clsMap[1];
00114   }
00115   if(0.2 <= posX && posX < 0.4) {
00116     func = (clsMap[2])[(clsMap[2]).size()-1]*(rr_cl);
00117     sum_clsize = clsMap[2];
00118   }
00119   if(0.4 <= posX && posX < 0.6) {
00120     func = (clsMap[3])[(clsMap[3]).size()-1]*(rr_cl);
00121     sum_clsize = clsMap[3];
00122   }
00123   if(0.6 <= posX && posX < 0.8) {
00124     func = (clsMap[4])[(clsMap[4]).size()-1]*(rr_cl);
00125     sum_clsize = clsMap[4];
00126   }
00127   if(0.8 <= posX && posX < 1.0)  {
00128     func = (clsMap[5])[(clsMap[5]).size()-1]*(rr_cl);
00129     sum_clsize = clsMap[5];
00130   }
00131 
00132   for(vector<double>::iterator iter = sum_clsize.begin();
00133       iter != sum_clsize.end(); ++iter){
00134     cnt++;
00135     if(func > (*iter)){
00136       min = cnt;
00137     }
00138     else if(func < (*iter)){
00139       break;
00140     }
00141   }
00142   return min;
00143 }
00144 
00145 void
00146 RPCSimAverageNoiseEff::simulate(const RPCRoll* roll,
00147                         const edm::PSimHitContainer& rpcHits)
00148 {
00149 
00150   _rpcSync->setRPCSimSetUp(getRPCSimSetUp());
00151   theRpcDigiSimLinks.clear();
00152   theDetectorHitMap.clear();
00153   theRpcDigiSimLinks = RPCDigiSimLinks(roll->id().rawId());
00154 
00155   RPCDetId rpcId = roll->id();
00156   RPCGeomServ RPCname(rpcId);
00157   std::string nameRoll = RPCname.name();
00158 
00159   const Topology& topology=roll->specs()->topology();
00160 
00161   for (edm::PSimHitContainer::const_iterator _hit = rpcHits.begin();
00162        _hit != rpcHits.end(); ++_hit){
00163 
00164     if(_hit-> particleType() == 11) continue;
00165 
00166     // Here I hould check if the RPC are up side down;
00167     const LocalPoint& entr=_hit->entryPoint();
00168 
00169     int time_hit = _rpcSync->getSimHitBx(&(*_hit));
00170     float posX = roll->strip(_hit->localPosition()) - static_cast<int>(roll->strip(_hit->localPosition()));
00171 
00172     std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00173 
00174     // Effinciecy
00175     int centralStrip = topology.channel(entr)+1;;
00176     float fire = flatDistribution->fire(1);
00177 
00178     if (fire < veff[centralStrip-1]) {
00179 
00180       int fstrip=centralStrip;
00181       int lstrip=centralStrip;
00182 
00183       // Compute the cluster size
00184       double w = flatDistribution->fire(1);
00185       if (w < 1.e-10) w=1.e-10;
00186       int clsize = this->getClSize(posX);
00187 
00188       std::vector<int> cls;
00189       cls.push_back(centralStrip);
00190       if (clsize > 1){
00191         for (int cl = 0; cl < (clsize-1)/2; cl++){
00192           if (centralStrip - cl -1 >= 1  ){
00193             fstrip = centralStrip-cl-1;
00194             cls.push_back(fstrip);
00195           }
00196           if (centralStrip + cl + 1 <= roll->nstrips() ){
00197             lstrip = centralStrip+cl+1;
00198             cls.push_back(lstrip);
00199           }
00200         }
00201         if (clsize%2 == 0 ){
00202           // insert the last strip according to the 
00203           // simhit position in the central strip 
00204           double deltaw=roll->centreOfStrip(centralStrip).x()-entr.x();
00205           if (deltaw<0.) {
00206             if (lstrip < roll->nstrips() ){
00207               lstrip++;
00208               cls.push_back(lstrip);
00209             }
00210           }else{
00211             if (fstrip > 1 ){
00212               fstrip--;
00213               cls.push_back(fstrip);
00214             }
00215           }
00216         }
00217       }
00218 
00219       for (std::vector<int>::iterator i=cls.begin(); i!=cls.end();i++){
00220         // Check the timing of the adjacent strip
00221         if(*i != centralStrip){
00222           if(flatDistribution->fire(1) < veff[*i-1]){
00223             std::pair<int, int> digi(*i,time_hit);
00224             strips.insert(digi);
00225 
00226             theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00227           }
00228         } 
00229         else {
00230           std::pair<int, int> digi(*i,time_hit);
00231           theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00232 
00233           strips.insert(digi);
00234         }
00235       }
00236     }
00237   }
00238 }
00239 
00240 void RPCSimAverageNoiseEff::simulateNoise(const RPCRoll* roll)
00241 {
00242 
00243   RPCDetId rpcId = roll->id();
00244 
00245   RPCGeomServ RPCname(rpcId);
00246   std::string nameRoll = RPCname.name();
00247 
00248   std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
00249   std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00250 
00251   unsigned int nstrips = roll->nstrips();
00252   double area = 0.0;
00253   
00254   if ( rpcId.region() == 0 )
00255     {
00256       const RectangularStripTopology* top_ = dynamic_cast<const
00257         RectangularStripTopology*>(&(roll->topology()));
00258       float xmin = (top_->localPosition(0.)).x();
00259       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00260       float striplength = (top_->stripLength());
00261       area = striplength*(xmax-xmin);
00262     }
00263   else
00264     {
00265       const TrapezoidalStripTopology* top_=dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
00266       float xmin = (top_->localPosition(0.)).x();
00267       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00268       float striplength = (top_->stripLength());
00269       area = striplength*(xmax-xmin);
00270     }
00271 
00272   for(unsigned int j = 0; j < vnoise.size(); ++j){
00273     
00274     if(j >= nstrips) break; 
00275     
00276     // The efficiency of 0% does not imply on the noise rate. 
00277     // If the strip is masked the noise rate should be 0 Hz/cm^2 
00278     //    if(veff[j] == 0) continue;
00279 
00280 
00281     //    double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
00282     // The vnoise is the noise rate per strip, so we shout multiply not 
00283     // by the chamber area, 
00284     // but the strip area which is area/((float)roll->nstrips()));
00285     double ave = 
00286       vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips());
00287 
00288     N_hits = poissonDistribution_->fire(ave);
00289 
00290     for (int i = 0; i < N_hits; i++ ){
00291       
00292       int time_hit = (static_cast<int>(flatDistribution2->fire((nbxing*gate)/gate))) - nbxing/2;
00293       std::pair<int, int> digi(j+1,time_hit);
00294       strips.insert(digi);
00295     }
00296   }
00297 }
00298