Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:50

0001 #include "Geometry/RPCGeometry/interface/RPCRoll.h"
0002 #include "Geometry/RPCGeometry/interface/RPCRollSpecs.h"
0003 #include "SimMuon/RPCDigitizer/src/RPCSimAsymmetricCls.h"
0004 #include "SimMuon/RPCDigitizer/src/RPCSimSetUp.h"
0005 
0006 #include "SimMuon/RPCDigitizer/src/RPCSynchronizer.h"
0007 #include "Geometry/CommonTopologies/interface/RectangularStripTopology.h"
0008 #include "Geometry/CommonTopologies/interface/TrapezoidalStripTopology.h"
0009 #include "Geometry/RPCGeometry/interface/RPCGeomServ.h"
0010 
0011 #include <cmath>
0012 
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 
0018 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0019 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0020 #include "Geometry/RPCGeometry/interface/RPCGeometry.h"
0021 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0022 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
0023 #include "SimMuon/RPCDigitizer/src/RPCSimSetUp.h"
0024 
0025 #include <cstring>
0026 #include <iostream>
0027 #include <fstream>
0028 #include <string>
0029 #include <vector>
0030 #include <cstdlib>
0031 #include <utility>
0032 #include <map>
0033 
0034 #include "CLHEP/Random/RandFlat.h"
0035 #include "CLHEP/Random/RandPoissonQ.h"
0036 
0037 using namespace std;
0038 
0039 RPCSimAsymmetricCls::RPCSimAsymmetricCls(const edm::ParameterSet& config) : RPCSim(config) {
0040   aveEff = config.getParameter<double>("averageEfficiency");
0041   aveCls = config.getParameter<double>("averageClusterSize");
0042   resRPC = config.getParameter<double>("timeResolution");
0043   timOff = config.getParameter<double>("timingRPCOffset");
0044   dtimCs = config.getParameter<double>("deltatimeAdjacentStrip");
0045   resEle = config.getParameter<double>("timeJitter");
0046   sspeed = config.getParameter<double>("signalPropagationSpeed");
0047   lbGate = config.getParameter<double>("linkGateWidth");
0048   rpcdigiprint = config.getParameter<bool>("printOutDigitizer");
0049   eledig = config.getParameter<bool>("digitizeElectrons");
0050 
0051   rate = config.getParameter<double>("Rate");
0052   nbxing = config.getParameter<int>("Nbxing");
0053   gate = config.getParameter<double>("Gate");
0054   frate = config.getParameter<double>("Frate");
0055 
0056   if (rpcdigiprint) {
0057     std::cout << "Average Efficiency        = " << aveEff << std::endl;
0058     std::cout << "Average Cluster Size      = " << aveCls << " strips" << std::endl;
0059     std::cout << "RPC Time Resolution       = " << resRPC << " ns" << std::endl;
0060     std::cout << "RPC Signal formation time = " << timOff << " ns" << std::endl;
0061     std::cout << "RPC adjacent strip delay  = " << dtimCs << " ns" << std::endl;
0062     std::cout << "Electronic Jitter         = " << resEle << " ns" << std::endl;
0063     std::cout << "Signal propagation time   = " << sspeed << " x c" << std::endl;
0064     std::cout << "Link Board Gate Width     = " << lbGate << " ns" << std::endl;
0065   }
0066 
0067   _rpcSync = new RPCSynchronizer(config);
0068 }
0069 
0070 RPCSimAsymmetricCls::~RPCSimAsymmetricCls() { delete _rpcSync; }
0071 
0072 int RPCSimAsymmetricCls::getClSize(uint32_t id, float posX, CLHEP::HepRandomEngine* engine) {
0073   std::vector<double> clsForDetId = getRPCSimSetUp()->getAsymmetricClsDistribution(id, slice(posX));
0074 
0075   int cnt = 1;
0076   int min = 1;
0077 
0078   double rr_cl = CLHEP::RandFlat::shoot(engine);
0079   LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::getClSize] Fired RandFlat :: " << rr_cl;
0080   for (unsigned int i = 0; i < clsForDetId.size(); i++) {
0081     cnt++;
0082     if (rr_cl > clsForDetId[i]) {
0083       min = cnt;
0084     } else if (rr_cl < clsForDetId[i]) {
0085       break;
0086     }
0087   }
0088   return min;
0089 }
0090 
0091 int RPCSimAsymmetricCls::getClSize(float posX, CLHEP::HepRandomEngine* engine) {
0092   std::map<int, std::vector<double> > clsMap = getRPCSimSetUp()->getClsMap();
0093 
0094   int cnt = 1;
0095   int min = 1;
0096   double func = 0.0;
0097   std::vector<double> sum_clsize;
0098 
0099   double rr_cl = CLHEP::RandFlat::shoot(engine);
0100   LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::getClSize] Fired RandFlat :: " << rr_cl;
0101 
0102   if (0.0 <= posX && posX < 0.2) {
0103     func = (clsMap[1])[(clsMap[1]).size() - 1] * (rr_cl);
0104     sum_clsize = clsMap[1];
0105   }
0106   if (0.2 <= posX && posX < 0.4) {
0107     func = (clsMap[2])[(clsMap[2]).size() - 1] * (rr_cl);
0108     sum_clsize = clsMap[2];
0109   }
0110   if (0.4 <= posX && posX < 0.6) {
0111     func = (clsMap[3])[(clsMap[3]).size() - 1] * (rr_cl);
0112     sum_clsize = clsMap[3];
0113   }
0114   if (0.6 <= posX && posX < 0.8) {
0115     func = (clsMap[4])[(clsMap[4]).size() - 1] * (rr_cl);
0116     sum_clsize = clsMap[4];
0117   }
0118   if (0.8 <= posX && posX < 1.0) {
0119     func = (clsMap[5])[(clsMap[5]).size() - 1] * (rr_cl);
0120     sum_clsize = clsMap[5];
0121   }
0122 
0123   for (vector<double>::iterator iter = sum_clsize.begin(); iter != sum_clsize.end(); ++iter) {
0124     cnt++;
0125     if (func > (*iter)) {
0126       min = cnt;
0127     } else if (func < (*iter)) {
0128       break;
0129     }
0130   }
0131   return min;
0132 }
0133 
0134 void RPCSimAsymmetricCls::simulate(const RPCRoll* roll,
0135                                    const edm::PSimHitContainer& rpcHits,
0136                                    CLHEP::HepRandomEngine* engine) {
0137   _rpcSync->setRPCSimSetUp(getRPCSimSetUp());
0138   theRpcDigiSimLinks.clear();
0139   theDetectorHitMap.clear();
0140   theRpcDigiSimLinks = RPCDigiSimLinks(roll->id().rawId());
0141 
0142   RPCDetId rpcId = roll->id();
0143   RPCGeomServ RPCname(rpcId);
0144   std::string nameRoll = RPCname.name();
0145 
0146   const Topology& topology = roll->specs()->topology();
0147   for (edm::PSimHitContainer::const_iterator _hit = rpcHits.begin(); _hit != rpcHits.end(); ++_hit) {
0148     if (!eledig && _hit->particleType() == 11)
0149       continue;
0150     // Here I hould check if the RPC are up side down;
0151     const LocalPoint& entr = _hit->entryPoint();
0152 
0153     int time_hit = _rpcSync->getSimHitBx(&(*_hit), engine);
0154     float posX = roll->strip(_hit->localPosition()) - static_cast<int>(roll->strip(_hit->localPosition()));
0155 
0156     std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
0157 
0158 #ifdef EDM_ML_DEBUG
0159     std::stringstream veffstream;
0160     veffstream << "[";
0161     for (std::vector<float>::iterator veffIt = veff.begin(); veffIt != veff.end(); ++veffIt) {
0162       veffstream << (*veffIt) << ",";
0163     }
0164     veffstream << "]";
0165     std::string veffstr = veffstream.str();
0166     LogDebug("RPCSimAsymmetricCls") << "Get Eff from RPCSimSetup for detId = " << rpcId.rawId() << " :: " << veffstr;
0167 #endif
0168 
0169     // Efficiency
0170     int centralStrip = topology.channel(entr) + 1;
0171     float fire = CLHEP::RandFlat::shoot(engine);
0172     LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire << " --> < "
0173                                     << veff[centralStrip - 1] << " ? --> " << ((fire < veff[centralStrip - 1]) ? 1 : 0);
0174 
0175     if (fire < veff[centralStrip - 1]) {
0176       LogDebug("RPCSimAsymmetricCls") << "Detector is Efficient for this simhit";
0177 
0178       int fstrip = centralStrip;
0179       int lstrip = centralStrip;
0180 
0181       // Compute the cluster size
0182 
0183       //double w = CLHEP::RandFlat::shoot(engine);
0184       //LogDebug ("RPCSimAsymmetricCls")<<"[RPCSimAsymmetricCls::simulate] Fired RandFlat :: "<<w<<" (w is not used)";
0185       //if (w < 1.e-10) w=1.e-10;
0186 
0187       int clsize = this->getClSize(rpcId.rawId(), posX, engine);  // This is for cluster size chamber by chamber
0188       LogDebug("RPCSimAsymmetricCls") << "Clustersize = " << clsize;
0189 
0190       std::vector<int> cls;
0191 
0192       cls.push_back(centralStrip);
0193       if (clsize > 1) {
0194         for (int cl = 0; cl < (clsize - 1) / 2; cl++) {
0195           if (centralStrip - cl - 1 >= 1) {
0196             fstrip = centralStrip - cl - 1;
0197             cls.push_back(fstrip);
0198           }
0199           if (centralStrip + cl + 1 <= roll->nstrips()) {
0200             lstrip = centralStrip + cl + 1;
0201             cls.push_back(lstrip);
0202           }
0203         }
0204         if (clsize % 2 == 0) {  //even cluster size is a special case
0205           if (clsize > 5) {
0206             // insert the last strip according to the
0207             // simhit position in the central strip
0208             // needed for cls > 5, because higher cluster size has no asymmetry
0209             // and thus is treated like in the old parametrization
0210             double deltaw = roll->centreOfStrip(centralStrip).x() - entr.x();
0211             if (deltaw < 0.) {
0212               if (lstrip < roll->nstrips()) {
0213                 lstrip++;
0214                 cls.push_back(lstrip);
0215               }
0216             } else {
0217               if (fstrip > 1) {
0218                 fstrip--;
0219                 cls.push_back(fstrip);
0220               }
0221             }
0222           } else {
0223             // needed for correct initial position for even cluster size
0224             // in case of asymmetric cluster size
0225             if (lstrip < roll->nstrips()) {
0226               lstrip++;
0227               cls.push_back(lstrip);
0228             }
0229           }
0230         }
0231       }
0232 
0233       //Now calculate the shift according to the distribution
0234       float fire1 = CLHEP::RandFlat::shoot(engine);
0235       LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire1
0236                                       << " (fire1 is used for a shift of the cluster)";
0237 
0238       int strip_shift = 0;
0239 
0240       int offset;
0241 
0242       if (clsize % 2 == 0) {
0243         offset = 2;
0244       } else {
0245         offset = 1;
0246       }
0247 
0248       //No shift (asymmetry) for higher cluster size.
0249       if (clsize > 5) {
0250         strip_shift = 0;
0251       } else {
0252         std::vector<double> TMPclsAsymmForDetId = getRPCSimSetUp()->getAsymmetryForCls(rpcId, slice(posX), clsize);
0253 
0254         for (unsigned int i = 0; i < TMPclsAsymmForDetId.size(); i++) {
0255           if (fire1 < TMPclsAsymmForDetId[i]) {
0256             strip_shift = i - offset;
0257             break;
0258           }
0259         }
0260       }
0261 
0262       vector<int> shifted_cls;  // vector to hold shifted strips
0263       shifted_cls.clear();
0264 
0265       int min_strip = 100;
0266       int max_strip = 0;
0267 
0268       //correction for the edges
0269       for (std::vector<int>::iterator i = cls.begin(); i != cls.end(); i++) {
0270         if (*i + strip_shift < min_strip) {
0271           min_strip = *i + strip_shift;
0272         }
0273         if (*i + strip_shift > max_strip) {
0274           max_strip = *i + strip_shift;
0275         }
0276       }
0277 
0278       if (min_strip < 1 || max_strip - roll->nstrips() > 0) {
0279         strip_shift = 0;
0280       }
0281 
0282       //Now shift the cluster
0283       for (std::vector<int>::iterator i = cls.begin(); i != cls.end(); i++) {
0284         shifted_cls.push_back(*i + strip_shift);
0285       }
0286       for (std::vector<int>::iterator i = shifted_cls.begin(); i != shifted_cls.end(); i++) {
0287         // Check the timing of the adjacent strip
0288         if (*i != centralStrip) {
0289           double fire2 = CLHEP::RandFlat::shoot(engine);
0290           LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulate] Fired RandFlat :: " << fire2
0291                                           << " (check whether adjacent strips are efficient)";
0292           if (fire2 < veff[*i - 1]) {
0293             std::pair<int, int> digi(*i, time_hit);
0294             strips.insert(digi);
0295             LogDebug("RPCSimAsymmetricCls")
0296                 << "RPC Digi inserted :: Signl :: DetId :: " << rpcId << " = " << rpcId.rawId() << " ==> digi <"
0297                 << digi.first << "," << digi.second << ">";
0298 
0299             theDetectorHitMap.insert(DetectorHitMap::value_type(digi, &(*_hit)));
0300           }
0301         } else {
0302           std::pair<int, int> digi(*i, time_hit);
0303           theDetectorHitMap.insert(DetectorHitMap::value_type(digi, &(*_hit)));
0304 
0305           strips.insert(digi);
0306           LogDebug("RPCSimAsymmetricCls") << "RPC Digi inserted :: Signl :: DetId :: " << rpcId << " = "
0307                                           << rpcId.rawId() << " ==> digi <" << digi.first << "," << digi.second << ">";
0308         }
0309       }
0310     }
0311   }
0312 }
0313 
0314 void RPCSimAsymmetricCls::simulateNoise(const RPCRoll* roll, CLHEP::HepRandomEngine* engine) {
0315   RPCDetId rpcId = roll->id();
0316 
0317   RPCGeomServ RPCname(rpcId);
0318   std::string nameRoll = RPCname.name();
0319 
0320   std::vector<float> vnoise = (getRPCSimSetUp())->getNoise(rpcId.rawId());
0321   std::vector<float> veff = (getRPCSimSetUp())->getEff(rpcId.rawId());
0322 
0323   LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Treating DetId :: " << rpcId << " = "
0324                                   << rpcId.rawId() << " which has " << roll->nstrips() << " strips";
0325 
0326 #ifdef EDM_ML_DEBUG
0327   std::stringstream vnoisestream;
0328   vnoisestream << "[";
0329   for (std::vector<float>::iterator vnoiseIt = vnoise.begin(); vnoiseIt != vnoise.end(); ++vnoiseIt) {
0330     vnoisestream << (*vnoiseIt) << ",";
0331   }
0332   vnoisestream << "]";
0333   std::string vnoisestr = vnoisestream.str();
0334   LogDebug("RPCSimAsymmetricCls") << "Get Noise from RPCSimSetup for detId = " << rpcId.rawId() << " :: vector with "
0335                                   << vnoise.size() << "entries :: " << vnoisestr;
0336 #endif
0337 
0338   unsigned int nstrips = roll->nstrips();
0339   double area = 0.0;
0340 
0341   if (rpcId.region() == 0) {
0342     const RectangularStripTopology* top_ = dynamic_cast<const RectangularStripTopology*>(&(roll->topology()));
0343     float xmin = (top_->localPosition(0.)).x();
0344     float xmax = (top_->localPosition((float)roll->nstrips())).x();
0345     float striplength = (top_->stripLength());
0346     area = striplength * (xmax - xmin);
0347   } else {
0348     const TrapezoidalStripTopology* top_ = dynamic_cast<const TrapezoidalStripTopology*>(&(roll->topology()));
0349     float xmin = (top_->localPosition(0.)).x();
0350     float xmax = (top_->localPosition((float)roll->nstrips())).x();
0351     float striplength = (top_->stripLength());
0352     area = striplength * (xmax - xmin);
0353   }
0354 
0355   LogDebug("RPCSimAsymmetricCls") << "Noise :: vnoise.size() = " << vnoise.size();
0356 
0357   for (unsigned int j = 0; j < vnoise.size(); ++j) {
0358     if (j >= nstrips)
0359       break;
0360 
0361     // The efficiency of 0% does not imply on the noise rate.
0362     // If the strip is masked the noise rate should be 0 Hz/cm^2
0363     // if(veff[j] == 0) continue;
0364 
0365     // double ave = vnoise[j]*nbxing*gate*area*1.0e-9*frate;
0366     // The vnoise is the noise rate per strip, so we shout multiply not
0367     // by the chamber area,
0368     // but the strip area which is area/((float)roll->nstrips()));
0369     double ave = vnoise[j] * nbxing * gate * area * 1.0e-9 * frate / ((float)roll->nstrips());
0370     LogDebug("RPCSimAsymmetricCls") << "Noise :: strip " << j << " Average = " << ave
0371                                     << " = vnoise[j]*nbxing*gate*area*1.0e-9*frate/((float)roll->nstrips()) = "
0372                                     << vnoise[j] << "*" << nbxing << "*" << gate << "*" << area << "*" << 1.0e-9 << "*"
0373                                     << frate << "/" << ((float)roll->nstrips());
0374 
0375     CLHEP::RandPoissonQ randPoissonQ(*engine, ave);
0376     N_hits = randPoissonQ.fire();
0377     LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Fired RandPoissonQ :: " << N_hits;
0378     LogDebug("RPCSimAsymmetricCls") << "Noise :: Amount of Noise Hits for DetId :: " << rpcId << " = " << rpcId.rawId()
0379                                     << " = N_hits = randPoissonQ.fire() = " << N_hits;
0380 
0381     for (int i = 0; i < N_hits; i++) {
0382       double time2 = CLHEP::RandFlat::shoot((nbxing * gate) / gate);
0383       LogDebug("RPCSimAsymmetricCls") << "[RPCSimAsymmetricCls::simulateNoise] Fired RandFlat :: " << time2;
0384       int time_hit = (static_cast<int>(time2) - nbxing / 2);
0385       std::pair<int, int> digi(j + 1, time_hit);
0386       strips.insert(digi);
0387       LogDebug("RPCSimAsymmetricCls") << "RPC Digi inserted :: Noise :: DetId :: " << rpcId << " = " << rpcId.rawId()
0388                                       << " ==> digi <" << digi.first << "," << digi.second << ">";
0389     }
0390   }
0391 }
0392 
0393 unsigned int RPCSimAsymmetricCls::slice(float posX) {
0394   if (0.0 <= posX && posX < 0.2) {
0395     return 0;
0396   } else if (0.2 <= posX && posX < 0.4) {
0397     return 1;
0398   } else if (0.4 <= posX && posX < 0.6) {
0399     return 2;
0400   } else if (0.6 <= posX && posX < 0.8) {
0401     return 3;
0402   } else if (0.8 <= posX && posX < 1.0) {
0403     return 4;
0404   } else
0405     return 2;
0406 }