-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
FastSimProducer.cc
564 lines (480 loc) · 23.4 KB
/
FastSimProducer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// system include files
#include <memory>
#include <string>
// framework
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/ESWatcher.h"
// data formats
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
#include "SimDataFormats/Track/interface/SimTrackContainer.h"
#include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Math/interface/LorentzVector.h"
// fastsim
#include "FastSimulation/Utilities/interface/RandomEngineAndDistribution.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/Geometry.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/SimplifiedGeometry.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/Decayer.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/LayerNavigator.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/Particle.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/ParticleFilter.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/InteractionModel.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/InteractionModelFactory.h"
#include "FastSimulation/SimplifiedGeometryPropagator/interface/ParticleManager.h"
#include "FastSimulation/Particle/interface/ParticleTable.h" // TODO: move this
// Hack for calorimetry
#include "FastSimulation/Event/interface/FSimTrack.h"
#include "FastSimulation/Calorimetry/interface/CalorimetryManager.h"
#include "FastSimulation/CaloGeometryTools/interface/CaloGeometryHelper.h"
#include "Geometry/Records/interface/CaloGeometryRecord.h"
#include "Geometry/CaloGeometry/interface/CaloGeometry.h"
#include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
#include "FastSimulation/ShowerDevelopment/interface/FastHFShowerLibrary.h"
///////////////////////////////////////////////
// Author: L. Vanelderen, S. Kurz
// Date: 29 May 2017
//////////////////////////////////////////////////////////
//! The core class of the new SimplifiedGeometryPropagator.
/*!
Coordinates the propagation of all particles, this means it does the following loop:
1) Get particle from ParticleManager
2) Call LayerNavigator to move particle to next intersection with layer
3) Loop over all the interactions and add secondaries to the event
4) Repeat steps 2), 3) until particle left the tracker, lost all its energy or is about to decay
5) If particle is about to decay: do decay and add secondaries to the event
6) Restart from 1) with the next particle
7) If last particle was propagated add SimTracks, SimVertices, SimHits,... to the event
*/
class FastSimProducer : public edm::stream::EDProducer<> {
public:
explicit FastSimProducer(const edm::ParameterSet&);
~FastSimProducer() override{;}
private:
void beginStream(edm::StreamID id) override;
void produce(edm::Event&, const edm::EventSetup&) override;
void endStream() override;
virtual FSimTrack createFSimTrack(fastsim::Particle* particle, fastsim::ParticleManager* particleManager);
edm::EDGetTokenT<edm::HepMCProduct> genParticlesToken_; //!< Token to get the genParticles
fastsim::Geometry geometry_; //!< The definition of the tracker according to python config
fastsim::Geometry caloGeometry_; //!< Hack to interface "old" calo to "new" tracking
double beamPipeRadius_; //!< The radius of the beampipe
double deltaRchargedMother_; //!< Cut on deltaR for ClosestChargedDaughter algorithm (FastSim tracking)
fastsim::ParticleFilter particleFilter_; //!< Decides which particles have to be propagated
std::unique_ptr<RandomEngineAndDistribution> _randomEngine; //!< The random engine
bool simulateCalorimetry;
edm::ESWatcher<CaloGeometryRecord> watchCaloGeometry_;
edm::ESWatcher<CaloTopologyRecord> watchCaloTopology_;
std::unique_ptr<CalorimetryManager> myCalorimetry; // unfortunately, default constructor cannot be called
bool simulateMuons;
fastsim::Decayer decayer_; //!< Handles decays of non-stable particles using pythia
std::vector<std::unique_ptr<fastsim::InteractionModel> > interactionModels_; //!< All defined interaction models
std::map<std::string, fastsim::InteractionModel *> interactionModelMap_; //!< Each interaction model has a unique name
static const std::string MESSAGECATEGORY; //!< Category of debugging messages ("FastSimulation")
};
const std::string FastSimProducer::MESSAGECATEGORY = "FastSimulation";
FastSimProducer::FastSimProducer(const edm::ParameterSet& iConfig)
: genParticlesToken_(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("src")))
, geometry_(iConfig.getParameter<edm::ParameterSet>("trackerDefinition"))
, caloGeometry_(iConfig.getParameter<edm::ParameterSet>("caloDefinition"))
, beamPipeRadius_(iConfig.getParameter<double>("beamPipeRadius"))
, deltaRchargedMother_(iConfig.getParameter<double>("deltaRchargedMother"))
, particleFilter_(iConfig.getParameter<edm::ParameterSet>("particleFilter"))
, _randomEngine(nullptr)
, simulateCalorimetry(iConfig.getParameter<bool>("simulateCalorimetry"))
, simulateMuons(iConfig.getParameter<bool>("simulateMuons"))
{
//----------------
// define interaction models
//---------------
const edm::ParameterSet & modelCfgs = iConfig.getParameter<edm::ParameterSet>("interactionModels");
for(const std::string & modelName : modelCfgs.getParameterNames())
{
const edm::ParameterSet & modelCfg = modelCfgs.getParameter<edm::ParameterSet>(modelName);
std::string modelClassName(modelCfg.getParameter<std::string>("className"));
// Use plugin-factory to create model
std::unique_ptr<fastsim::InteractionModel> interactionModel(fastsim::InteractionModelFactory::get()->create(modelClassName, modelName, modelCfg));
if(!interactionModel.get()){
throw cms::Exception("FastSimProducer") << "InteractionModel " << modelName << " could not be created";
}
// Add model to list
interactionModels_.push_back(std::move(interactionModel));
// and create the map
interactionModelMap_[modelName] = interactionModels_.back().get();
}
//----------------
// calorimetry
//---------------
if(simulateCalorimetry){
myCalorimetry.reset(new CalorimetryManager(nullptr,
iConfig.getParameter<edm::ParameterSet>("Calorimetry"),
iConfig.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInECAL"),
iConfig.getParameter<edm::ParameterSet>("MaterialEffectsForMuonsInHCAL"),
iConfig.getParameter<edm::ParameterSet>("GFlash")));
}
//----------------
// register products
//----------------
// SimTracks and SimVertices
produces<edm::SimTrackContainer>();
produces<edm::SimVertexContainer>();
// products of interaction models, i.e. simHits
for(auto & interactionModel : interactionModels_)
{
interactionModel->registerProducts(*this);
}
produces<edm::PCaloHitContainer>("EcalHitsEB");
produces<edm::PCaloHitContainer>("EcalHitsEE");
produces<edm::PCaloHitContainer>("EcalHitsES");
produces<edm::PCaloHitContainer>("HcalHits");
produces<edm::SimTrackContainer>("MuonSimTracks");
}
void
FastSimProducer::beginStream(const edm::StreamID id)
{
_randomEngine = std::make_unique<RandomEngineAndDistribution>(id);
}
void
FastSimProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
{
LogDebug(MESSAGECATEGORY) << " produce";
geometry_.update(iSetup, interactionModelMap_);
caloGeometry_.update(iSetup, interactionModelMap_);
// Define containers for SimTracks, SimVertices
std::unique_ptr<edm::SimTrackContainer> simTracks_(new edm::SimTrackContainer);
std::unique_ptr<edm::SimVertexContainer> simVertices_(new edm::SimVertexContainer);
// Get the particle data table (in case lifetime or charge of GenParticles not set)
edm::ESHandle <HepPDT::ParticleDataTable> pdt;
iSetup.getData(pdt);
ParticleTable::Sentry ptable(&(*pdt));
// Get the GenParticle collection
edm::Handle<edm::HepMCProduct> genParticles;
iEvent.getByToken(genParticlesToken_, genParticles);
// Load the ParticleManager which returns the particles that have to be propagated
// Creates a fastsim::Particle out of a GenParticle/secondary
fastsim::ParticleManager particleManager(*genParticles->GetEvent()
,*pdt
,beamPipeRadius_
,deltaRchargedMother_
,particleFilter_
,*simTracks_
,*simVertices_);
// Initialize the calorimeter geometry
if(simulateCalorimetry)
{
if(watchCaloGeometry_.check(iSetup) || watchCaloTopology_.check(iSetup)){
edm::ESHandle<CaloGeometry> pG;
iSetup.get<CaloGeometryRecord>().get(pG);
myCalorimetry->getCalorimeter()->setupGeometry(*pG);
edm::ESHandle<CaloTopology> theCaloTopology;
iSetup.get<CaloTopologyRecord>().get(theCaloTopology);
myCalorimetry->getCalorimeter()->setupTopology(*theCaloTopology);
myCalorimetry->getCalorimeter()->initialize(geometry_.getMagneticFieldZ(math::XYZTLorentzVector(0., 0., 0., 0.)));
myCalorimetry->getHFShowerLibrary()->initHFShowerLibrary(iSetup);
}
// Important: this also cleans the calorimetry information from the last event
myCalorimetry->initialize(_randomEngine.get());
}
// The vector of SimTracks needed for the CalorimetryManager
std::vector<FSimTrack> myFSimTracks;
LogDebug(MESSAGECATEGORY) << "################################"
<< "\n###############################";
// loop over particles
for(std::unique_ptr<fastsim::Particle> particle = particleManager.nextParticle(*_randomEngine); particle != nullptr; particle=particleManager.nextParticle(*_randomEngine))
{
LogDebug(MESSAGECATEGORY) << "\n moving NEXT particle: " << *particle;
// -----------------------------
// This condition is necessary because of hack for calorimetry
// -> The CalorimetryManager should also be implemented based on this new FastSim classes (Particle.h) in a future project.
// A second loop (below) loops over all parts of the calorimetry in order to create a track of the old FastSim class FSimTrack.
// The condition below (R<128, z<302) makes sure that the particle geometrically is outside the tracker boundaries
// -----------------------------
if(particle->position().Perp2() < 128.*128. && std::abs(particle->position().Z()) < 302.){
// move the particle through the layers
fastsim::LayerNavigator layerNavigator(geometry_);
const fastsim::SimplifiedGeometry * layer = nullptr;
// moveParticleToNextLayer(..) returns 0 in case that particle decays
// in this case particle is propagated up to its decay vertex
while(layerNavigator.moveParticleToNextLayer(*particle,layer))
{
LogDebug(MESSAGECATEGORY) << " moved to next layer: " << *layer;
LogDebug(MESSAGECATEGORY) << " new state: " << *particle;
// Hack to interface "old" calo to "new" tracking
// Particle reached calorimetry so stop further propagation
if(layer->getCaloType() == fastsim::SimplifiedGeometry::TRACKERBOUNDARY)
{
layer = nullptr;
// particle no longer is on a layer
particle->resetOnLayer();
break;
}
// break after 25 ns: only happens for particles stuck in loops
if(particle->position().T() > 25)
{
layer = nullptr;
// particle no longer is on a layer
particle->resetOnLayer();
break;
}
// perform interaction between layer and particle
// do only if there is actual material
if(layer->getThickness(particle->position(), particle->momentum()) > 1E-10){
int nSecondaries = 0;
// loop on interaction models
for(fastsim::InteractionModel * interactionModel : layer->getInteractionModels())
{
LogDebug(MESSAGECATEGORY) << " interact with " << *interactionModel;
std::vector<std::unique_ptr<fastsim::Particle> > secondaries;
interactionModel->interact(*particle,*layer,secondaries,*_randomEngine);
nSecondaries += secondaries.size();
particleManager.addSecondaries(particle->position(),particle->simTrackIndex(),secondaries,layer);
}
// kinematic cuts: particle might e.g. lost all its energy
if(!particleFilter_.acceptsEn(*particle))
{
// Add endvertex if particle did not create any secondaries
if(nSecondaries==0) particleManager.addEndVertex(particle.get());
layer = nullptr;
break;
}
}
LogDebug(MESSAGECATEGORY) << "--------------------------------"
<< "\n-------------------------------";
}
// do decays
if(!particle->isStable() && particle->remainingProperLifeTimeC() < 1E-10)
{
LogDebug(MESSAGECATEGORY) << "Decaying particle...";
std::vector<std::unique_ptr<fastsim::Particle> > secondaries;
decayer_.decay(*particle,secondaries, _randomEngine->theEngine());
LogDebug(MESSAGECATEGORY) << " decay has " << secondaries.size() << " products";
particleManager.addSecondaries(particle->position(), particle->simTrackIndex(),secondaries);
continue;
}
LogDebug(MESSAGECATEGORY) << "################################"
<< "\n###############################";
}
// -----------------------------
// Hack to interface "old" calorimetry with "new" propagation in tracker
// The CalorimetryManager has to know which particle could in principle hit which parts of the calorimeter
// I think it's a bit strange to propagate the particle even further (and even decay it) if it already hits
// some part of the calorimetry but this is how the code works...
// -----------------------------
if(particle->position().Perp2() >= 128.*128. || std::abs(particle->position().Z()) >= 302.){
LogDebug(MESSAGECATEGORY) << "\n moving particle to calorimetry: " << *particle;
// create FSimTrack (this is the object the old propagation uses)
myFSimTracks.push_back(createFSimTrack(particle.get(), &particleManager));
// particle was decayed
if(!particle->isStable() && particle->remainingProperLifeTimeC() < 1E-10)
{
continue;
}
LogDebug(MESSAGECATEGORY) << "################################"
<< "\n###############################";
}
// -----------------------------
// End Hack
// -----------------------------
LogDebug(MESSAGECATEGORY) << "################################"
<< "\n###############################";
}
// store simTracks and simVertices
iEvent.put(std::move(simTracks_));
iEvent.put(std::move(simVertices_));
// store products of interaction models, i.e. simHits
for(auto & interactionModel : interactionModels_)
{
interactionModel->storeProducts(iEvent);
}
// -----------------------------
// Calorimetry Manager
// -----------------------------
if(simulateCalorimetry)
{
for(auto myFSimTrack : myFSimTracks)
{
myCalorimetry->reconstructTrack(myFSimTrack, _randomEngine.get());
}
}
// -----------------------------
// Store Hits
// -----------------------------
std::unique_ptr<edm::PCaloHitContainer> p4(new edm::PCaloHitContainer);
std::unique_ptr<edm::PCaloHitContainer> p5(new edm::PCaloHitContainer);
std::unique_ptr<edm::PCaloHitContainer> p6(new edm::PCaloHitContainer);
std::unique_ptr<edm::PCaloHitContainer> p7(new edm::PCaloHitContainer);
std::unique_ptr<edm::SimTrackContainer> m1(new edm::SimTrackContainer);
if(simulateCalorimetry)
{
myCalorimetry->loadFromEcalBarrel(*p4);
myCalorimetry->loadFromEcalEndcap(*p5);
myCalorimetry->loadFromPreshower(*p6);
myCalorimetry->loadFromHcal(*p7);
if(simulateMuons){
myCalorimetry->harvestMuonSimTracks(*m1);
}
}
iEvent.put(std::move(p4),"EcalHitsEB");
iEvent.put(std::move(p5),"EcalHitsEE");
iEvent.put(std::move(p6),"EcalHitsES");
iEvent.put(std::move(p7),"HcalHits");
iEvent.put(std::move(m1),"MuonSimTracks");
}
void
FastSimProducer::endStream()
{
_randomEngine.reset();
}
FSimTrack
FastSimProducer::createFSimTrack(fastsim::Particle* particle, fastsim::ParticleManager* particleManager)
{
FSimTrack myFSimTrack(particle->pdgId(),
particleManager->getSimTrack(particle->simTrackIndex()).momentum(),
particle->simVertexIndex(),
particle->genParticleIndex(),
particle->simTrackIndex(),
particle->charge(),
particle->position(),
particle->momentum(),
particleManager->getSimVertex(particle->simVertexIndex()));
// move the particle through the caloLayers
fastsim::LayerNavigator caloLayerNavigator(caloGeometry_);
const fastsim::SimplifiedGeometry * caloLayer = nullptr;
// moveParticleToNextLayer(..) returns 0 in case that particle decays
// in this case particle is propagated up to its decay vertex
while(caloLayerNavigator.moveParticleToNextLayer(*particle,caloLayer))
{
LogDebug(MESSAGECATEGORY) << " moved to next caloLayer: " << *caloLayer;
LogDebug(MESSAGECATEGORY) << " new state: " << *particle;
// break after 25 ns: only happens for particles stuck in loops
if(particle->position().T() > 50)
{
caloLayer = nullptr;
break;
}
//////////
// Define ParticlePropagators (RawParticle) needed for CalorimetryManager and save them
//////////
RawParticle PP(particle->pdgId(), particle->momentum());
PP.setVertex(particle->position());
// no material
if(caloLayer->getThickness(particle->position(), particle->momentum()) < 1E-10)
{
// unfortunately needed for CalorimetryManager
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::ECAL){
if(!myFSimTrack.onEcal())
{
myFSimTrack.setEcal(PP, 0);
}
}
else if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::HCAL){
if(!myFSimTrack.onHcal())
{
myFSimTrack.setHcal(PP, 0);
}
}
else if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::VFCAL){
if(!myFSimTrack.onVFcal())
{
myFSimTrack.setVFcal(PP, 0);
}
}
// not necessary to continue propagation
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::VFCAL)
{
myFSimTrack.setGlobal();
caloLayer = nullptr;
break;
}
continue;
}
// Stupid variable used by the old propagator
// For details check BaseParticlePropagator.h
int success = 0;
if(caloLayer->isForward())
{
success = 2;
// particle moves inwards
if(particle->position().Z() * particle->momentum().Z() < 0)
{
success *= -1;
}
}
else
{
success = 1;
// particle moves inwards
if(particle->momentum().X() * particle->position().X() + particle->momentum().Y() * particle->position().Y() < 0)
{
success *= -1;
}
}
// Save the hit
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::PRESHOWER1)
{
if(!myFSimTrack.onLayer1())
{
myFSimTrack.setLayer1(PP, success);
}
}
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::PRESHOWER2)
{
if(!myFSimTrack.onLayer2())
{
myFSimTrack.setLayer2(PP, success);
}
}
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::ECAL)
{
if(!myFSimTrack.onEcal())
{
myFSimTrack.setEcal(PP, success);
}
}
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::HCAL)
{
if(!myFSimTrack.onHcal())
{
myFSimTrack.setHcal(PP, success);
}
}
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::VFCAL)
{
if(!myFSimTrack.onVFcal())
{
myFSimTrack.setVFcal(PP, success);
}
}
// Particle reached end of detector
if(caloLayer->getCaloType() == fastsim::SimplifiedGeometry::VFCAL)
{
myFSimTrack.setGlobal();
caloLayer = nullptr;
break;
}
LogDebug(MESSAGECATEGORY) << "--------------------------------"
<< "\n-------------------------------";
}
// do decays
// don't have to worry about daughters if particle already within the calorimetry
// since they will be rejected by the vertex cut of the ParticleFilter
if(!particle->isStable() && particle->remainingProperLifeTimeC() < 1E-10)
{
LogDebug(MESSAGECATEGORY) << "Decaying particle...";
std::vector<std::unique_ptr<fastsim::Particle> > secondaries;
decayer_.decay(*particle,secondaries, _randomEngine->theEngine());
LogDebug(MESSAGECATEGORY) << " decay has " << secondaries.size() << " products";
particleManager->addSecondaries(particle->position(), particle->simTrackIndex(),secondaries);
}
return myFSimTrack;
}
DEFINE_FWK_MODULE(FastSimProducer);