CMS 3D CMS Logo

CMSSW_4_4_3_patch1/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   int max = 1;
00108   double func=0.0;
00109   std::vector<double> sum_clsize;
00110 
00111   double rr_cl = flatDistribution->fire(1);
00112   if(0.0 <= posX && posX < 0.2)  {
00113     func = (clsMap[1])[(clsMap[1]).size()-1]*(rr_cl);
00114     sum_clsize = clsMap[1];
00115   }
00116   if(0.2 <= posX && posX < 0.4) {
00117     func = (clsMap[2])[(clsMap[2]).size()-1]*(rr_cl);
00118     sum_clsize = clsMap[2];
00119   }
00120   if(0.4 <= posX && posX < 0.6) {
00121     func = (clsMap[3])[(clsMap[3]).size()-1]*(rr_cl);
00122     sum_clsize = clsMap[3];
00123   }
00124   if(0.6 <= posX && posX < 0.8) {
00125     func = (clsMap[4])[(clsMap[4]).size()-1]*(rr_cl);
00126     sum_clsize = clsMap[4];
00127   }
00128   if(0.8 <= posX && posX < 1.0)  {
00129     func = (clsMap[5])[(clsMap[5]).size()-1]*(rr_cl);
00130     sum_clsize = clsMap[5];
00131   }
00132 
00133   for(vector<double>::iterator iter = sum_clsize.begin();
00134       iter != sum_clsize.end(); ++iter){
00135     cnt++;
00136     if(func > (*iter)){
00137       min = cnt;
00138     }
00139     else if(func < (*iter)){
00140       max = cnt;
00141       break;
00142     }
00143   }
00144   return min;
00145 }
00146 
00147 void
00148 RPCSimAverageNoiseEff::simulate(const RPCRoll* roll,
00149                         const edm::PSimHitContainer& rpcHits)
00150 {
00151 
00152   _rpcSync->setRPCSimSetUp(getRPCSimSetUp());
00153   theRpcDigiSimLinks.clear();
00154   theDetectorHitMap.clear();
00155   theRpcDigiSimLinks = RPCDigiSimLinks(roll->id().rawId());
00156 
00157   RPCDetId rpcId = roll->id();
00158   RPCGeomServ RPCname(rpcId);
00159   std::string nameRoll = RPCname.name();
00160 
00161   const Topology& topology=roll->specs()->topology();
00162 
00163   for (edm::PSimHitContainer::const_iterator _hit = rpcHits.begin();
00164        _hit != rpcHits.end(); ++_hit){
00165 
00166     if(_hit-> particleType() == 11) continue;
00167 
00168     // Here I hould check if the RPC are up side down;
00169     const LocalPoint& entr=_hit->entryPoint();
00170 
00171     int time_hit = _rpcSync->getSimHitBx(&(*_hit));
00172     float posX = roll->strip(_hit->localPosition()) - static_cast<int>(roll->strip(_hit->localPosition()));
00173 
00174     std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00175 
00176     // Effinciecy
00177     int centralStrip = topology.channel(entr)+1;;
00178     float fire = flatDistribution->fire(1);
00179 
00180     if (fire < veff[centralStrip-1]) {
00181 
00182       int fstrip=centralStrip;
00183       int lstrip=centralStrip;
00184 
00185       // Compute the cluster size
00186       double w = flatDistribution->fire(1);
00187       if (w < 1.e-10) w=1.e-10;
00188       int clsize = this->getClSize(posX);
00189 
00190       std::vector<int> cls;
00191       cls.push_back(centralStrip);
00192       if (clsize > 1){
00193         for (int cl = 0; cl < (clsize-1)/2; cl++){
00194           if (centralStrip - cl -1 >= 1  ){
00195             fstrip = centralStrip-cl-1;
00196             cls.push_back(fstrip);
00197           }
00198           if (centralStrip + cl + 1 <= roll->nstrips() ){
00199             lstrip = centralStrip+cl+1;
00200             cls.push_back(lstrip);
00201           }
00202         }
00203         if (clsize%2 == 0 ){
00204           // insert the last strip according to the 
00205           // simhit position in the central strip 
00206           double deltaw=roll->centreOfStrip(centralStrip).x()-entr.x();
00207           if (deltaw<0.) {
00208             if (lstrip < roll->nstrips() ){
00209               lstrip++;
00210               cls.push_back(lstrip);
00211             }
00212           }else{
00213             if (fstrip > 1 ){
00214               fstrip--;
00215               cls.push_back(fstrip);
00216             }
00217           }
00218         }
00219       }
00220 
00221       for (std::vector<int>::iterator i=cls.begin(); i!=cls.end();i++){
00222         // Check the timing of the adjacent strip
00223         if(*i != centralStrip){
00224           if(flatDistribution->fire(1) < veff[*i-1]){
00225             std::pair<int, int> digi(*i,time_hit);
00226             strips.insert(digi);
00227 
00228             theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00229           }
00230         } 
00231         else {
00232           std::pair<int, int> digi(*i,time_hit);
00233           theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00234 
00235           strips.insert(digi);
00236         }
00237       }
00238     }
00239   }
00240 }
00241 
00242 void RPCSimAverageNoiseEff::simulateNoise(const RPCRoll* roll)
00243 {
00244 
00245   RPCDetId rpcId = roll->id();
00246 
00247   RPCGeomServ RPCname(rpcId);
00248   std::string nameRoll = RPCname.name();
00249 
00250   std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
00251   std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00252 
00253   unsigned int nstrips = roll->nstrips();
00254   double area = 0.0;
00255   
00256   if ( rpcId.region() == 0 )
00257     {
00258       const RectangularStripTopology* top_ = dynamic_cast<const
00259         RectangularStripTopology*>(&(roll->topology()));
00260       float xmin = (top_->localPosition(0.)).x();
00261       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00262       float striplength = (top_->stripLength());
00263       area = striplength*(xmax-xmin);
00264     }
00265   else
00266     {
00267       const TrapezoidalStripTopology* top_=dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
00268       float xmin = (top_->localPosition(0.)).x();
00269       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00270       float striplength = (top_->stripLength());
00271       area = striplength*(xmax-xmin);
00272     }
00273 
00274   for(unsigned int j = 0; j < vnoise.size(); ++j){
00275     
00276     if(j >= nstrips) break; 
00277     
00278     // The efficiency of 0% does not imply on the noise rate. 
00279     // If the strip is masked the noise rate should be 0 Hz/cm^2 
00280     //    if(veff[j] == 0) continue;
00281 
00282 
00283     //    double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
00284     // The vnoise is the noise rate per strip, so we shout multiply not 
00285     // by the chamber area, 
00286     // but the strip area which is area/((float)roll->nstrips()));
00287     double ave = 
00288       vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips());
00289 
00290     N_hits = poissonDistribution_->fire(ave);
00291 
00292     for (int i = 0; i < N_hits; i++ ){
00293       
00294       int time_hit = (static_cast<int>(flatDistribution2->fire((nbxing*gate)/gate))) - nbxing/2;
00295       std::pair<int, int> digi(j+1,time_hit);
00296       strips.insert(digi);
00297     }
00298   }
00299 }
00300