CMS 3D CMS Logo

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