CMS 3D CMS Logo

CSCBaseElectronicsSim.cc
Go to the documentation of this file.
1 // This is CSCBaseElectronicsSim.cc
2 
3 #include "CLHEP/Units/GlobalPhysicalConstants.h"
9 
10 #include "CLHEP/Random/RandGaussQ.h"
11 
12 #include <algorithm>
13 #include <list>
14 
16  : theSpecs(nullptr),
17  theLayerGeometry(nullptr),
18  theLayer(nullptr),
19  theSignalMap(),
20  theAmpResponse(),
21  theBunchSpacing(25.),
22  theNoiseWasAdded(false),
23  nElements(0),
24  theShapingTime(p.getParameter<int>("shapingTime")),
25  thePeakTimeSigma(p.getParameter<double>("peakTimeSigma")),
26  theBunchTimingOffsets(p.getParameter<std::vector<double>>("bunchTimingOffsets")),
27  theSignalStartTime(p.getParameter<double>("signalStartTime")),
28  theSignalStopTime(p.getParameter<double>("signalStopTime")),
29  theSamplingTime(p.getParameter<double>("samplingTime")),
30  theNumberOfSamples(static_cast<int>((theSignalStopTime - theSignalStartTime) / theSamplingTime)),
31  theOffsetOfBxZero(p.getParameter<int>("timeBitForBxZero")),
32  theSignalPropagationSpeed(p.getParameter<std::vector<double>>("signalSpeed")),
33  theTimingCalibrationError(p.getParameter<std::vector<double>>("timingCalibrationError")),
34  doNoise_(p.getParameter<bool>("doNoise")) {
35  assert(theBunchTimingOffsets.size() == 11);
36 }
37 
39 
41  const std::vector<CSCDetectorHit> &detectorHits,
42  CLHEP::HepRandomEngine *engine) {
43  theNoiseWasAdded = false;
44 
45  {
46  theSignalMap.clear();
47  theDetectorHitMap.clear();
48  setLayer(layer);
49  // can we swap for efficiency?
51  }
52 
53  {
54  size_t nHits = detectorHits.size();
55  // turn each detector hit into an analog signal
56  for (size_t i = 0; i < nHits; ++i) {
57  int element = readoutElement(detectorHits[i].getElement());
58 
59  // skip if hit element is not part of a readout element
60  // e.g. wire in non-readout group
61  if (element != 0)
62  add(amplifySignal(detectorHits[i]), engine);
63  }
64  }
65 
66  {
67  if (doNoise_) {
68  addNoise(engine);
69  }
70  }
71 }
72 
74  // fill the specs member data
75  theSpecs = layer->chamber()->specs();
76  theLayerGeometry = layer->geometry();
77 
78  theLayer = layer;
81 }
82 
84  std::vector<float> ampBinValues(theNumberOfSamples);
85  int i = 0;
86  for (; i < theNumberOfSamples; ++i) {
87  ampBinValues[i] = calculateAmpResponse(i * theSamplingTime);
88  // truncate any entries that are trivially small
89  if (i > 5 && ampBinValues[i] < 0.000001)
90  break;
91  }
92  ampBinValues.resize(i);
93  theAmpResponse = CSCAnalogSignal(0, theSamplingTime, ampBinValues, 1., 0.);
94 
95  LogTrace("CSCBaseElectronicsSim") << "CSCBaseElectronicsSim: dump of theAmpResponse follows...\n" << theAmpResponse;
96 }
97 
99  int element = readoutElement(detectorHit.getElement());
100 
101  float readoutTime = detectorHit.getTime() + signalDelay(element, detectorHit.getPosition());
102 
103  // start from the amp response, and modify it.
104  CSCAnalogSignal thisSignal(theAmpResponse);
105  thisSignal *= detectorHit.getCharge();
106  thisSignal.setTimeOffset(readoutTime);
107  thisSignal.setElement(element);
108  // keep track of links between digis and hits
109  theDetectorHitMap.insert(DetectorHitMap::value_type(channelIndex(element), detectorHit));
110  return thisSignal;
111 }
112 
113 CSCAnalogSignal CSCBaseElectronicsSim::makeNoiseSignal(int element, CLHEP::HepRandomEngine *) {
114  std::vector<float> binValues(theNumberOfSamples);
115  // default is empty
116  return CSCAnalogSignal(element, theSamplingTime, binValues, 0., theSignalStartTime);
117 }
118 
119 void CSCBaseElectronicsSim::addNoise(CLHEP::HepRandomEngine *engine) {
120  for (CSCSignalMap::iterator mapI = theSignalMap.begin(); mapI != theSignalMap.end(); ++mapI) {
121  // superimpose electronics noise
122  (*mapI).second.superimpose(makeNoiseSignal((*mapI).first, engine));
123  // DON'T do amp gain variations. Handled in strips by calibration code
124  // and variations in the shaper peaking time.
125  double timeOffset = CLHEP::RandGaussQ::shoot(engine, (*mapI).second.getTimeOffset(), thePeakTimeSigma);
126  (*mapI).second.setTimeOffset(timeOffset);
127  }
128  theNoiseWasAdded = true;
129 }
130 
131 CSCAnalogSignal &CSCBaseElectronicsSim::find(int element, CLHEP::HepRandomEngine *engine) {
132  if (element <= 0 || element > nElements) {
133  LogTrace("CSCBaseElectronicsSim") << "CSCBaseElectronicsSim: bad element = " << element << ". There are "
134  << nElements << " elements.";
135  edm::LogError("Error in CSCBaseElectronicsSim: element out of bounds");
136  }
137  CSCSignalMap::iterator signalMapItr = theSignalMap.find(element);
138  if (signalMapItr == theSignalMap.end()) {
139  CSCAnalogSignal newSignal;
140  if (theNoiseWasAdded) {
141  newSignal = makeNoiseSignal(element, engine);
142  } else {
143  std::vector<float> emptyV(theNumberOfSamples);
144  newSignal = CSCAnalogSignal(element, theSamplingTime, emptyV, 0., theSignalStartTime);
145  }
146  signalMapItr = theSignalMap.insert(std::pair<int, CSCAnalogSignal>(element, newSignal)).first;
147  }
148  return (*signalMapItr).second;
149 }
150 
151 CSCAnalogSignal &CSCBaseElectronicsSim::add(const CSCAnalogSignal &signal, CLHEP::HepRandomEngine *engine) {
152  int element = signal.getElement();
153  CSCAnalogSignal &newSignal = find(element, engine);
154  newSignal.superimpose(signal);
155  return newSignal;
156 }
157 
158 float CSCBaseElectronicsSim::signalDelay(int element, float pos) const {
159  // readout is on top edge of chamber for strips, right edge
160  // for wires.
161  // zero calibrated to chamber center
162  float distance = -1. * pos;
164  return distance / speed;
165 }
166 
167 void CSCBaseElectronicsSim::addLinks(int channelIndex) {
168  std::pair<DetectorHitMap::iterator, DetectorHitMap::iterator> channelHitItr =
169  theDetectorHitMap.equal_range(channelIndex);
170 
171  // find the fraction contribution for each SimTrack
172  std::map<int, float> simTrackChargeMap;
173  std::map<int, EncodedEventId> eventIdMap;
174  float totalCharge = 0;
175  for (DetectorHitMap::iterator hitItr = channelHitItr.first; hitItr != channelHitItr.second; ++hitItr) {
176  const PSimHit *hit = hitItr->second.getSimHit();
177  // might be zero for unit tests and such
178  if (hit != nullptr) {
179  int simTrackId = hitItr->second.getSimHit()->trackId();
180  float charge = hitItr->second.getCharge();
181  std::map<int, float>::iterator chargeItr = simTrackChargeMap.find(simTrackId);
182  if (chargeItr == simTrackChargeMap.end()) {
183  simTrackChargeMap[simTrackId] = charge;
184  eventIdMap[simTrackId] = hit->eventId();
185  } else {
186  chargeItr->second += charge;
187  }
188  totalCharge += charge;
189  }
190  }
191 
192  for (std::map<int, float>::iterator chargeItr = simTrackChargeMap.begin(); chargeItr != simTrackChargeMap.end();
193  ++chargeItr) {
194  int simTrackId = chargeItr->first;
196  StripDigiSimLink(channelIndex, simTrackId, eventIdMap[simTrackId], chargeItr->second / totalCharge));
197  }
198 }
CSCBaseElectronicsSim::theSpecs
const CSCChamberSpecs * theSpecs
Definition: CSCBaseElectronicsSim.h:113
edm::DetSet::push_back
void push_back(const T &t)
Definition: DetSet.h:66
electrons_cff.bool
bool
Definition: electrons_cff.py:372
mps_fire.i
i
Definition: mps_fire.py:355
CSCBaseElectronicsSim::add
CSCAnalogSignal & add(const CSCAnalogSignal &, CLHEP::HepRandomEngine *)
Definition: CSCBaseElectronicsSim.cc:151
CSCBaseElectronicsSim::theAmpResponse
CSCAnalogSignal theAmpResponse
Definition: CSCBaseElectronicsSim.h:119
MessageLogger.h
funct::false
false
Definition: Factorize.h:34
CSCBaseElectronicsSim::initParameters
virtual void initParameters()=0
CSCLayer::chamber
const CSCChamber * chamber() const
Definition: CSCLayer.h:49
CSCBaseElectronicsSim::find
CSCAnalogSignal & find(int element, CLHEP::HepRandomEngine *)
Definition: CSCBaseElectronicsSim.cc:131
CSCBaseElectronicsSim::theNoiseWasAdded
bool theNoiseWasAdded
Definition: CSCBaseElectronicsSim.h:127
CSCChamberSpecs::chamberType
int chamberType() const
Definition: CSCChamberSpecs.cc:154
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CSCBaseElectronicsSim::theSignalMap
CSCSignalMap theSignalMap
Definition: CSCBaseElectronicsSim.h:118
pos
Definition: PixelAliasList.h:18
CSCBaseElectronicsSim::theDigiSimLinks
DigiSimLinks theDigiSimLinks
Definition: CSCBaseElectronicsSim.h:160
CSCBaseElectronicsSim::CSCBaseElectronicsSim
CSCBaseElectronicsSim(const edm::ParameterSet &p)
Definition: CSCBaseElectronicsSim.cc:15
cms::cuda::assert
assert(be >=bs)
HLT_2018_cff.distance
distance
Definition: HLT_2018_cff.py:6417
CSCLayer
Definition: CSCLayer.h:24
CSCBaseElectronicsSim::theSignalPropagationSpeed
std::vector< double > theSignalPropagationSpeed
Definition: CSCBaseElectronicsSim.h:153
CSCDetectorHit::getTime
float getTime() const
Definition: CSCDetectorHit.h:24
CSCBaseElectronicsSim::doNoise_
bool doNoise_
Definition: CSCBaseElectronicsSim.h:155
CSCDetectorHit
Definition: CSCDetectorHit.h:16
CSCAnalogSignal::superimpose
void superimpose(const CSCAnalogSignal &signal2)
Definition: CSCAnalogSignal.h:75
CSCBaseElectronicsSim.h
CSCBaseElectronicsSim::theNumberOfSamples
int theNumberOfSamples
Definition: CSCBaseElectronicsSim.h:147
CSCBaseElectronicsSim::~CSCBaseElectronicsSim
virtual ~CSCBaseElectronicsSim()
Definition: CSCBaseElectronicsSim.cc:38
PSimHit.h
CSCBaseElectronicsSim::DigiSimLinks
edm::DetSet< StripDigiSimLink > DigiSimLinks
Definition: CSCBaseElectronicsSim.h:40
CSCBaseElectronicsSim::makeNoiseSignal
virtual CSCAnalogSignal makeNoiseSignal(int element, CLHEP::HepRandomEngine *)
Definition: CSCBaseElectronicsSim.cc:113
CSCBaseElectronicsSim::thePeakTimeSigma
float thePeakTimeSigma
Definition: CSCBaseElectronicsSim.h:134
CSCBaseElectronicsSim::addLinks
virtual void addLinks(int channelIndex)
Definition: CSCBaseElectronicsSim.cc:167
CSCDetectorHit::getPosition
float getPosition() const
Definition: CSCDetectorHit.h:23
CSCBaseElectronicsSim::signalDelay
virtual float signalDelay(int element, float pos) const
Definition: CSCBaseElectronicsSim.cc:158
CSCLayer::geometry
const CSCLayerGeometry * geometry() const
Definition: CSCLayer.h:44
CSCBaseElectronicsSim::theSignalStartTime
float theSignalStartTime
Definition: CSCBaseElectronicsSim.h:140
CSCBaseElectronicsSim::theBunchTimingOffsets
std::vector< double > theBunchTimingOffsets
Definition: CSCBaseElectronicsSim.h:137
CSCBaseElectronicsSim::readoutElement
virtual int readoutElement(int element) const =0
CSCBaseElectronicsSim::theLayerId
CSCDetId theLayerId
Definition: CSCBaseElectronicsSim.h:116
CSCBaseElectronicsSim::theLayer
const CSCLayer * theLayer
Definition: CSCBaseElectronicsSim.h:115
ALCARECOTkAlJpsiMuMu_cff.charge
charge
Definition: ALCARECOTkAlJpsiMuMu_cff.py:47
CSCBaseElectronicsSim::nElements
int nElements
Definition: CSCBaseElectronicsSim.h:130
GeomDet::geographicalId
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
CSCBaseElectronicsSim::theLayerGeometry
const CSCLayerGeometry * theLayerGeometry
Definition: CSCBaseElectronicsSim.h:114
CSCBaseElectronicsSim::setLayer
void setLayer(const CSCLayer *layer)
Definition: CSCBaseElectronicsSim.cc:73
edm::ParameterSet
Definition: ParameterSet.h:36
edm::LogError
Definition: MessageLogger.h:183
CSCDetId
Definition: CSCDetId.h:26
CSCBaseElectronicsSim::theSamplingTime
float theSamplingTime
Definition: CSCBaseElectronicsSim.h:144
createfilelist.int
int
Definition: createfilelist.py:10
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
HLTSiStripMonitoring_cff.speed
speed
Definition: HLTSiStripMonitoring_cff.py:22
CSCChamber::specs
const CSCChamberSpecs * specs() const
Definition: CSCChamber.h:39
CSCAnalogSignal::getElement
int getElement() const
constructor from time and amp shape
Definition: CSCAnalogSignal.h:46
CSCBaseElectronicsSim::calculateAmpResponse
virtual float calculateAmpResponse(float t) const =0
CSCBaseElectronicsSim::amplifySignal
CSCAnalogSignal amplifySignal(const CSCDetectorHit &)
Definition: CSCBaseElectronicsSim.cc:98
CSCDetectorHit::getCharge
float getCharge() const
Definition: CSCDetectorHit.h:22
CSCBaseElectronicsSim::theDetectorHitMap
DetectorHitMap theDetectorHitMap
Definition: CSCBaseElectronicsSim.h:159
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
CSCLayer.h
CalibratedDigis_cfi.timeOffset
timeOffset
Definition: CalibratedDigis_cfi.py:22
CSCAnalogSignal
Definition: CSCAnalogSignal.h:32
CSCBaseElectronicsSim::addNoise
void addNoise(CLHEP::HepRandomEngine *)
Definition: CSCBaseElectronicsSim.cc:119
CSCBaseElectronicsSim::channelIndex
virtual int channelIndex(int channel) const
lets users map channels to different indices for links
Definition: CSCBaseElectronicsSim.h:101
CSCBaseElectronicsSim::layerId
CSCDetId layerId() const
the CSCDetId corresponding to the current layer
Definition: CSCBaseElectronicsSim.h:104
CSCDetectorHit.h
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:671
PSimHit
Definition: PSimHit.h:15
CSCDetectorHit::getElement
int getElement() const
Definition: CSCDetectorHit.h:21
CSCBaseElectronicsSim::fillAmpResponse
void fillAmpResponse()
Definition: CSCBaseElectronicsSim.cc:83
hit
Definition: SiStripHitEffFromCalibTree.cc:88
AlCaHLTBitMon_QueryRunRegistry.getElement
def getElement(obj, name)
Definition: AlCaHLTBitMon_QueryRunRegistry.py:178
CSCBaseElectronicsSim::simulate
void simulate(const CSCLayer *layer, const std::vector< CSCDetectorHit > &inputHits, CLHEP::HepRandomEngine *)
Definition: CSCBaseElectronicsSim.cc:40