CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/SimMuon/RPCDigitizer/src/RPCSimAverageNoiseEffCls.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/RPCSimAverageNoiseEffCls.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 RPCSimAverageNoiseEffCls::RPCSimAverageNoiseEffCls(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 RPCSimAverageNoiseEffCls::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 RPCSimAverageNoiseEffCls::~RPCSimAverageNoiseEffCls()
00092 {
00093   //Deleting the distribution defined in the constructor
00094   delete flatDistribution;
00095   delete flatDistribution2;
00096   delete poissonDistribution_;
00097   delete _rpcSync;
00098 }
00099 
00100 
00101 int RPCSimAverageNoiseEffCls::getClSize(uint32_t id,float posX)
00102 {
00103   std::vector<double> clsForDetId = getRPCSimSetUp()->getCls(id);
00104 
00105   int cnt = 1;
00106   int min = 1;
00107   double func=0.0;
00108   std::vector<double> sum_clsize;
00109 
00110   sum_clsize.clear();
00111   sum_clsize = clsForDetId;
00112   int vectOffset(0);
00113 
00114   double rr_cl = flatDistribution->fire(1);
00115 
00116   if(0.0 <= posX && posX < 0.2)  {
00117     func = clsForDetId[19]*(rr_cl);
00118     vectOffset = 0;
00119   }
00120   if(0.2 <= posX && posX < 0.4) {
00121     func = clsForDetId[39]*(rr_cl);
00122     vectOffset = 20;
00123   }
00124   if(0.4 <= posX && posX < 0.6) {
00125     func = clsForDetId[59]*(rr_cl);
00126     vectOffset = 40;
00127   }
00128   if(0.6 <= posX && posX < 0.8) {
00129     func = clsForDetId[79]*(rr_cl);
00130     vectOffset = 60;
00131   }  
00132   if(0.8 <= posX && posX < 1.0)  {
00133     func = clsForDetId[89]*(rr_cl);
00134     vectOffset = 80;
00135   }
00136   
00137 
00138   for(int i = vectOffset; i<(vectOffset+20); i++){
00139     cnt++;
00140     if(func > clsForDetId[i]){
00141       min = cnt;
00142     }
00143     else if(func < clsForDetId[i]){
00144       break;
00145     }
00146   }
00147   return min;
00148 }
00149 
00150 int RPCSimAverageNoiseEffCls::getClSize(float posX)
00151 {
00152 
00153   std::map< int, std::vector<double> > clsMap = getRPCSimSetUp()->getClsMap();
00154 
00155   int cnt = 1;
00156   int min = 1;
00157   double func=0.0;
00158   std::vector<double> sum_clsize;
00159 
00160   double rr_cl = flatDistribution->fire(1);
00161   if(0.0 <= posX && posX < 0.2)  {
00162     func = (clsMap[1])[(clsMap[1]).size()-1]*(rr_cl);
00163     sum_clsize = clsMap[1];
00164   }
00165   if(0.2 <= posX && posX < 0.4) {
00166     func = (clsMap[2])[(clsMap[2]).size()-1]*(rr_cl);
00167     sum_clsize = clsMap[2];
00168   }
00169   if(0.4 <= posX && posX < 0.6) {
00170     func = (clsMap[3])[(clsMap[3]).size()-1]*(rr_cl);
00171     sum_clsize = clsMap[3];
00172   }
00173   if(0.6 <= posX && posX < 0.8) {
00174     func = (clsMap[4])[(clsMap[4]).size()-1]*(rr_cl);
00175     sum_clsize = clsMap[4];
00176   }
00177   if(0.8 <= posX && posX < 1.0)  {
00178     func = (clsMap[5])[(clsMap[5]).size()-1]*(rr_cl);
00179     sum_clsize = clsMap[5];
00180   }
00181 
00182   for(vector<double>::iterator iter = sum_clsize.begin();
00183       iter != sum_clsize.end(); ++iter){
00184     cnt++;
00185     if(func > (*iter)){
00186       min = cnt;
00187     }
00188     else if(func < (*iter)){
00189       break;
00190     }
00191   }
00192   return min;
00193 }
00194 
00195 void
00196 RPCSimAverageNoiseEffCls::simulate(const RPCRoll* roll,
00197                         const edm::PSimHitContainer& rpcHits)
00198 {
00199 
00200   _rpcSync->setRPCSimSetUp(getRPCSimSetUp());
00201   theRpcDigiSimLinks.clear();
00202   theDetectorHitMap.clear();
00203   theRpcDigiSimLinks = RPCDigiSimLinks(roll->id().rawId());
00204 
00205   RPCDetId rpcId = roll->id();
00206   RPCGeomServ RPCname(rpcId);
00207   //std::string nameRoll = RPCname.name();
00208 
00209   const Topology& topology=roll->specs()->topology();
00210 
00211   for (edm::PSimHitContainer::const_iterator _hit = rpcHits.begin();
00212        _hit != rpcHits.end(); ++_hit){
00213 
00214     if(_hit-> particleType() == 11) continue;
00215 
00216     // Here I hould check if the RPC are up side down;
00217     const LocalPoint& entr=_hit->entryPoint();
00218 
00219     int time_hit = _rpcSync->getSimHitBx(&(*_hit));
00220     float posX = roll->strip(_hit->localPosition()) - static_cast<int>(roll->strip(_hit->localPosition()));
00221 
00222     std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00223 
00224     // Effinciecy
00225     int centralStrip = topology.channel(entr)+1;;
00226     float fire = flatDistribution->fire(1);
00227 
00228     if (fire < veff[centralStrip-1]) {
00229 
00230       int fstrip=centralStrip;
00231       int lstrip=centralStrip;
00232 
00233       // Compute the cluster size
00234       double w = flatDistribution->fire(1);
00235       if (w < 1.e-10) w=1.e-10;
00236 //       int clsize = this->getClSize(posX); // This is for one and the same cls for all the chambers
00237       int clsize = this->getClSize(rpcId.rawId(),posX); // This is for cluster size chamber by chamber
00238       std::vector<int> cls;
00239       cls.push_back(centralStrip);
00240       if (clsize > 1){
00241         for (int cl = 0; cl < (clsize-1)/2; cl++){
00242           if (centralStrip - cl -1 >= 1  ){
00243             fstrip = centralStrip-cl-1;
00244             cls.push_back(fstrip);
00245           }
00246           if (centralStrip + cl + 1 <= roll->nstrips() ){
00247             lstrip = centralStrip+cl+1;
00248             cls.push_back(lstrip);
00249           }
00250         }
00251         if (clsize%2 == 0 ){
00252           // insert the last strip according to the 
00253           // simhit position in the central strip 
00254           double deltaw=roll->centreOfStrip(centralStrip).x()-entr.x();
00255           if (deltaw<0.) {
00256             if (lstrip < roll->nstrips() ){
00257               lstrip++;
00258               cls.push_back(lstrip);
00259             }
00260           }else{
00261             if (fstrip > 1 ){
00262               fstrip--;
00263               cls.push_back(fstrip);
00264             }
00265           }
00266         }
00267       }
00268 
00269       for (std::vector<int>::iterator i=cls.begin(); i!=cls.end();i++){
00270         // Check the timing of the adjacent strip
00271         if(*i != centralStrip){
00272           if(flatDistribution->fire(1) < veff[*i-1]){
00273             std::pair<int, int> digi(*i,time_hit);
00274             strips.insert(digi);
00275 
00276             theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00277           }
00278         } 
00279         else {
00280           std::pair<int, int> digi(*i,time_hit);
00281           theDetectorHitMap.insert(DetectorHitMap::value_type(digi,&(*_hit)));
00282 
00283           strips.insert(digi);
00284         }
00285       }
00286     }
00287   }
00288 }
00289 
00290 void RPCSimAverageNoiseEffCls::simulateNoise(const RPCRoll* roll)
00291 {
00292 
00293   RPCDetId rpcId = roll->id();
00294 
00295   RPCGeomServ RPCname(rpcId);
00296   //std::string nameRoll = RPCname.name();
00297 
00298   std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
00299   std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
00300 
00301   unsigned int nstrips = roll->nstrips();
00302   double area = 0.0;
00303   
00304   if ( rpcId.region() == 0 )
00305     {
00306       const RectangularStripTopology* top_ = dynamic_cast<const
00307         RectangularStripTopology*>(&(roll->topology()));
00308       float xmin = (top_->localPosition(0.)).x();
00309       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00310       float striplength = (top_->stripLength());
00311       area = striplength*(xmax-xmin);
00312     }
00313   else
00314     {
00315       const TrapezoidalStripTopology* top_=dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
00316       float xmin = (top_->localPosition(0.)).x();
00317       float xmax = (top_->localPosition((float)roll->nstrips())).x();
00318       float striplength = (top_->stripLength());
00319       area = striplength*(xmax-xmin);
00320     }
00321 
00322   for(unsigned int j = 0; j < vnoise.size(); ++j){
00323     
00324     if(j >= nstrips) break; 
00325 
00326     // The efficiency of 0% does not imply on the noise rate.
00327     // If the strip is masked the noise rate should be 0 Hz/cm^2
00328     //    if(veff[j] == 0) continue;
00329     
00330     //    double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
00331     // The vnoise is the noise rate per strip, so we shout multiply not
00332     // by the chamber area,
00333     // but the strip area which is area/((float)roll->nstrips()));
00334     double ave =
00335       vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips());
00336 
00337     N_hits = poissonDistribution_->fire(ave);
00338 
00339     for (int i = 0; i < N_hits; i++ ){
00340       
00341       int time_hit = (static_cast<int>(flatDistribution2->fire((nbxing*gate)/gate))) - nbxing/2;
00342       std::pair<int, int> digi(j+1,time_hit);
00343       strips.insert(digi);
00344     }
00345   }
00346 }
00347