CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripDigitizer.cc
Go to the documentation of this file.
1 // File: SiStripDigitizerAlgorithm.cc
2 // Description: Class for digitization.
3 
4 // Modified 15/May/2013 mark.grimes@bristol.ac.uk - Modified so that the digi-sim link has the correct
5 // index for the sim hits stored. It was previously always set to zero (I won't mention that it was
6 // me who originally wrote that).
7 
8 // system include files
9 #include <memory>
10 
12 
13 #include "SiStripDigitizer.h"
16 
21 
22 // user include files
25 
28 
32 
33 //needed for the geometry:
43 //needed for the magnetic field:
46 
48 
49 //Data Base infromations
53 
54 //Random Number
58 #include "CLHEP/Random/RandomEngine.h"
59 
61  gainLabel(conf.getParameter<std::string>("Gain")),
62  hitsProducer(conf.getParameter<std::string>("hitsProducer")),
63  trackerContainers(conf.getParameter<std::vector<std::string> >("ROUList")),
64  ZSDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("ZSDigi")),
65  SCDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("SCDigi")),
66  VRDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("VRDigi")),
67  PRDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("PRDigi")),
68  geometryType(conf.getParameter<std::string>("GeometryType")),
69  useConfFromDB(conf.getParameter<bool>("TrackerConfigurationFromDB")),
70  zeroSuppression(conf.getParameter<bool>("ZeroSuppression")),
71  makeDigiSimLinks_(conf.getUntrackedParameter<bool>("makeDigiSimLinks", false))
72 {
73  const std::string alias("simSiStripDigis");
74 
75  mixMod.produces<edm::DetSetVector<SiStripDigi> >(ZSDigi).setBranchAlias(ZSDigi);
76  mixMod.produces<edm::DetSetVector<SiStripRawDigi> >(SCDigi).setBranchAlias(alias + SCDigi);
77  mixMod.produces<edm::DetSetVector<SiStripRawDigi> >(VRDigi).setBranchAlias(alias + VRDigi);
78  mixMod.produces<edm::DetSetVector<SiStripRawDigi> >(PRDigi).setBranchAlias(alias + PRDigi);
79  mixMod.produces<edm::DetSetVector<StripDigiSimLink> >().setBranchAlias(alias + "siStripDigiSimLink");
81  if ( ! rng.isAvailable()) {
82  throw cms::Exception("Configuration")
83  << "SiStripDigitizer requires the RandomNumberGeneratorService\n"
84  "which is not present in the configuration file. You must add the service\n"
85  "in the configuration file or remove the modules that require it.";
86  }
87 
88  rndEngine = &(rng->getEngine());
90 
91 }
92 
93 // Virtual destructor needed.
95 }
96 
97 void SiStripDigitizer::accumulateStripHits(edm::Handle<std::vector<PSimHit> > hSimHits,
98  const TrackerTopology *tTopo, size_t globalSimHitIndex ) {
99  // globalSimHitIndex is the index the sim hit will have when it is put in a collection
100  // of sim hits for all crossings. This is only used when creating digi-sim links if
101  // configured to do so.
102 
103  if(hSimHits.isValid()) {
104  std::set<unsigned int> detIds;
105  std::vector<PSimHit> const& simHits = *hSimHits.product();
106  for(std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd; ++it, ++globalSimHitIndex ) {
107  unsigned int detId = (*it).detUnitId();
108  if(detIds.insert(detId).second) {
109  // The insert succeeded, so this detector element has not yet been processed.
110  unsigned int isub = DetId(detId).subdetId();
111  if((isub == StripSubdetector::TIB) || (isub == StripSubdetector::TID) || (isub == StripSubdetector::TOB) || (isub == StripSubdetector::TEC)) {
112  StripGeomDetUnit* stripdet = detectorUnits[detId];
113  //access to magnetic field in global coordinates
114  GlobalVector bfield = pSetup->inTesla(stripdet->surface().position());
115  LogDebug ("Digitizer ") << "B-field(T) at " << stripdet->surface().position() << "(cm): "
116  << pSetup->inTesla(stripdet->surface().position());
117  theDigiAlgo->accumulateSimHits(it, itEnd, globalSimHitIndex, stripdet, bfield, tTopo);
118  }
119  }
120  } // end of loop over sim hits
121  }
122 }
123 
124 // Functions that gets called by framework every event
125  void
127  //Retrieve tracker topology from geometry
129  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
130  const TrackerTopology *tTopo=tTopoHand.product();
131 
132  // Step A: Get Inputs
133  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
136 
137  iEvent.getByLabel(tag, simHits);
139  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
140  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
141  // as though they were on the end of this collection.
142  // Note that this is only used for creating digi-sim links (if configured to do so).
143  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
144  }
145  }
146 
147  void
149 
151  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
152  const TrackerTopology *tTopo=tTopoHand.product();
153 
154  // Step A: Get Inputs
155  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
158 
159  iEvent.getByLabel(tag, simHits);
161  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
162  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
163  // as though they were on the end of this collection.
164  // Note that this is only used for creating digi-sim links (if configured to do so).
165  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
166  }
167  }
168 
169 
171  // Make sure that the first crossing processed starts indexing the sim hits from zero.
172  // This variable is used so that the sim hits from all crossing frames have sequential
173  // indices used to create the digi-sim link (if configured to do so) rather than starting
174  // from zero for each crossing.
176 
177  // Step A: Get Inputs
178 
179  if(useConfFromDB){
181  iSetup.get<SiStripDetCablingRcd>().get(detCabling);
182  detCabling->addConnected(theDetIdList);
183  }
184 
185  theDigiAlgo->initializeEvent(iSetup);
186 
188  iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
189 
190  // FIX THIS! We only need to clear and (re)fill detectorUnits when the geometry type IOV changes. Use ESWatcher to determine this.
191  bool changes = true;
192  if(changes) { // Replace with ESWatcher
193  detectorUnits.clear();
194  }
195  for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); ++iu) {
196  unsigned int detId = (*iu)->geographicalId().rawId();
197  DetId idet=DetId(detId);
198  unsigned int isub=idet.subdetId();
199  if((isub == StripSubdetector::TIB) ||
200  (isub == StripSubdetector::TID) ||
201  (isub == StripSubdetector::TOB) ||
202  (isub == StripSubdetector::TEC)) {
203  StripGeomDetUnit* stripdet = dynamic_cast<StripGeomDetUnit*>((*iu));
204  assert(stripdet != 0);
205  if(changes) { // Replace with ESWatcher
206  detectorUnits.insert(std::make_pair(detId, stripdet));
207  }
208  theDigiAlgo->initializeDetUnit(stripdet, iSetup);
209  }
210  }
211 }
212 
214  edm::ESHandle<SiStripGain> gainHandle;
215  edm::ESHandle<SiStripNoises> noiseHandle;
216  edm::ESHandle<SiStripThreshold> thresholdHandle;
217  edm::ESHandle<SiStripPedestals> pedestalHandle;
218  iSetup.get<SiStripGainSimRcd>().get(gainLabel,gainHandle);
219  iSetup.get<SiStripNoisesRcd>().get(noiseHandle);
220  iSetup.get<SiStripThresholdRcd>().get(thresholdHandle);
221  iSetup.get<SiStripPedestalsRcd>().get(pedestalHandle);
222 
223  std::vector<edm::DetSet<SiStripDigi> > theDigiVector;
224  std::vector<edm::DetSet<SiStripRawDigi> > theRawDigiVector;
225  std::auto_ptr< edm::DetSetVector<StripDigiSimLink> > pOutputDigiSimLink( new edm::DetSetVector<StripDigiSimLink> );
226 
227  // Step B: LOOP on StripGeomDetUnit
228  theDigiVector.reserve(10000);
229  theDigiVector.clear();
230 
231  for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); iu ++){
232  if(useConfFromDB){
233  //apply the cable map _before_ digitization: consider only the detis that are connected
234  if(theDetIdList.find((*iu)->geographicalId().rawId())==theDetIdList.end())
235  continue;
236  }
237  StripGeomDetUnit* sgd = dynamic_cast<StripGeomDetUnit*>((*iu));
238  if (sgd != 0){
239  edm::DetSet<SiStripDigi> collectorZS((*iu)->geographicalId().rawId());
240  edm::DetSet<SiStripRawDigi> collectorRaw((*iu)->geographicalId().rawId());
241  edm::DetSet<StripDigiSimLink> collectorLink((*iu)->geographicalId().rawId());
242  theDigiAlgo->digitize(collectorZS,collectorRaw,collectorLink,sgd,
243  gainHandle,thresholdHandle,noiseHandle,pedestalHandle);
244  if(zeroSuppression){
245  if(collectorZS.data.size()>0){
246  theDigiVector.push_back(collectorZS);
247  if( !collectorLink.data.empty() ) pOutputDigiSimLink->insert(collectorLink);
248  }
249  }else{
250  if(collectorRaw.data.size()>0){
251  theRawDigiVector.push_back(collectorRaw);
252  if( !collectorLink.data.empty() ) pOutputDigiSimLink->insert(collectorLink);
253  }
254  }
255  }
256  }
257 
258  if(zeroSuppression){
259  // Step C: create output collection
260  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_virginraw(new edm::DetSetVector<SiStripRawDigi>());
261  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_scopemode(new edm::DetSetVector<SiStripRawDigi>());
262  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_processedraw(new edm::DetSetVector<SiStripRawDigi>());
263  std::auto_ptr<edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(theDigiVector) );
264  // Step D: write output to file
265  iEvent.put(output, ZSDigi);
266  iEvent.put(output_scopemode, SCDigi);
267  iEvent.put(output_virginraw, VRDigi);
268  iEvent.put(output_processedraw, PRDigi);
269  if( makeDigiSimLinks_ ) iEvent.put( pOutputDigiSimLink ); // The previous EDProducer didn't name this collection so I won't either
270  }else{
271  // Step C: create output collection
272  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_virginraw(new edm::DetSetVector<SiStripRawDigi>(theRawDigiVector));
273  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_scopemode(new edm::DetSetVector<SiStripRawDigi>());
274  std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > output_processedraw(new edm::DetSetVector<SiStripRawDigi>());
275  std::auto_ptr<edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>() );
276  // Step D: write output to file
277  iEvent.put(output, ZSDigi);
278  iEvent.put(output_scopemode, SCDigi);
279  iEvent.put(output_virginraw, VRDigi);
280  iEvent.put(output_processedraw, PRDigi);
281  if( makeDigiSimLinks_ ) iEvent.put( pOutputDigiSimLink ); // The previous EDProducer didn't name this collection so I won't either
282  }
283 }
#define LogDebug(id)
const vstring trackerContainers
const std::string ZSDigi
int i
Definition: DBlmapReader.cc:9
edm::ESHandle< TrackerGeometry > pDD
const bool zeroSuppression
virtual void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
const std::string hitsProducer
virtual void finalizeEvent(edm::Event &e, edm::EventSetup const &c) override
virtual void accumulate(edm::Event const &e, edm::EventSetup const &c) override
const bool useConfFromDB
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
std::string encode() const
Definition: InputTag.cc:164
std::map< uint32_t, std::vector< int > > theDetIdList
virtual ~SiStripDigitizer()
int iEvent
Definition: GenABIO.cc:243
std::map< std::string, size_t > crossingSimHitIndexOffset_
Offset to add to the index of each sim hit to account for which crossing it&#39;s in. ...
std::map< unsigned int, StripGeomDetUnit * > detectorUnits
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
bool isAvailable() const
Definition: Service.h:47
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool isValid() const
Definition: HandleBase.h:76
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
tuple conf
Definition: dbtoconf.py:185
Definition: DetId.h:20
std::unique_ptr< SiStripDigitizerAlgorithm > theDigiAlgo
void accumulateStripHits(edm::Handle< std::vector< PSimHit > >, const TrackerTopology *tTopo, size_t globalSimHitIndex)
edm::ESHandle< MagneticField > pSetup
const std::string VRDigi
const std::string PRDigi
const T & get() const
Definition: EventSetup.h:55
const std::string geometryType
T const * product() const
Definition: ESHandle.h:62
tuple simHits
Definition: trackerHits.py:16
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
const bool makeDigiSimLinks_
Whether or not to create the association to sim truth collection. Set in configuration.
const PositionType & position() const
SiStripDigitizer(const edm::ParameterSet &conf, edm::EDProducer &mixMod)
const std::string gainLabel
CLHEP::HepRandomEngine * rndEngine
const std::string SCDigi