-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathPileUp.cc
429 lines (375 loc) · 18.6 KB
/
PileUp.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
#include "Mixing/Base/interface/PileUp.h"
#include "DataFormats/Provenance/interface/BranchIDListHelper.h"
#include "DataFormats/Provenance/interface/ModuleDescription.h"
#include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
#include "FWCore/Framework/interface/EventPrincipal.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/src/SignallingProductRegistry.h"
#include "FWCore/Framework/interface/ESRecordsToProxyIndices.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
#include "FWCore/ServiceRegistry/interface/ProcessContext.h"
#include "FWCore/Sources/interface/VectorInputSourceDescription.h"
#include "FWCore/Sources/interface/VectorInputSourceFactory.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/GetPassID.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "FWCore/Version/interface/GetReleaseVersion.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "Mixing/Base/src/SecondaryEventProvider.h"
#include "CondFormats/DataRecord/interface/MixingRcd.h"
#include "CondFormats/RunInfo/interface/MixingModuleConfig.h"
#include "CLHEP/Random/RandPoissonQ.h"
#include "CLHEP/Random/RandPoisson.h"
#include <algorithm>
#include <memory>
#include "TMath.h"
////////////////////////////////////////////////////////////////////////////////
/// return a random number distributed according the histogram bin contents.
///
/// This routine is derived from root/hist/hist/src/TH1.cxx
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
static Double_t GetRandom(TH1* th1, CLHEP::HepRandomEngine* rng) {
Int_t nbinsx = th1->GetNbinsX();
Double_t* fIntegral = th1->GetIntegral();
Double_t integral = fIntegral[nbinsx];
if (integral == 0)
return 0;
Double_t r1 = rng->flat();
Int_t ibin = TMath::BinarySearch(nbinsx, fIntegral, r1);
Double_t x = th1->GetBinLowEdge(ibin + 1);
if (r1 > fIntegral[ibin])
x += th1->GetBinWidth(ibin + 1) * (r1 - fIntegral[ibin]) / (fIntegral[ibin + 1] - fIntegral[ibin]);
return x;
}
////////////////////////////////////////////////////////////////////////////////
namespace edm {
PileUp::PileUp(ParameterSet const& pset, const std::shared_ptr<PileUpConfig>& config)
: type_(pset.getParameter<std::string>("type")),
Source_type_(config->sourcename_),
averageNumber_(config->averageNumber_),
intAverage_(static_cast<int>(averageNumber_)),
histo_(std::make_shared<TH1F>(*config->histo_)),
histoDistribution_(type_ == "histo"),
probFunctionDistribution_(type_ == "probFunction"),
poisson_(type_ == "poisson"),
fixed_(type_ == "fixed"),
none_(type_ == "none"),
fileNameHash_(0U),
productRegistry_(new SignallingProductRegistry),
input_(VectorInputSourceFactory::get()
->makeVectorInputSource(
pset, VectorInputSourceDescription(productRegistry_, edm::PreallocationConfiguration()))
.release()),
processConfiguration_(new ProcessConfiguration(std::string("@MIXING"), getReleaseVersion(), getPassID())),
processContext_(new ProcessContext()),
eventPrincipal_(),
lumiPrincipal_(),
runPrincipal_(),
provider_(),
PoissonDistribution_(),
PoissonDistr_OOT_(),
randomEngine_(),
playback_(config->playback_),
sequential_(pset.getUntrackedParameter<bool>("sequential", false)) {
// Use the empty parameter set for the parameter set ID of our "@MIXING" process.
processConfiguration_->setParameterSetID(ParameterSet::emptyParameterSetID());
processContext_->setProcessConfiguration(processConfiguration_.get());
if (pset.existsAs<std::vector<ParameterSet> >("producers", true)) {
std::vector<ParameterSet> producers = pset.getParameter<std::vector<ParameterSet> >("producers");
provider_ = std::make_unique<SecondaryEventProvider>(producers, *productRegistry_, processConfiguration_);
}
productRegistry_->setFrozen();
// A modified HistoryAppender must be used for unscheduled processing.
eventPrincipal_ = std::make_unique<EventPrincipal>(input_->productRegistry(),
std::make_shared<BranchIDListHelper>(),
std::make_shared<ThinnedAssociationsHelper>(),
*processConfiguration_,
nullptr);
bool DB = type_ == "readDB";
if (pset.exists("nbPileupEvents")) {
if (0 != pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed", 0)) {
edm::LogWarning("MixingModule") << "Parameter nbPileupEvents.seed is not supported";
}
}
edm::Service<edm::RandomNumberGenerator> rng;
if (!rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "PileUp requires the RandomNumberGeneratorService\n"
"which is not present in the configuration file. You must add the service\n"
"in the configuration file or remove the modules that require it.";
}
if (!(histoDistribution_ || probFunctionDistribution_ || poisson_ || fixed_ || none_) && !DB) {
throw cms::Exception("Illegal parameter value", "PileUp::PileUp(ParameterSet const& pset)")
<< "'type' parameter (a string) has a value of '" << type_ << "'.\n"
<< "Legal values are 'poisson', 'fixed', or 'none'\n";
}
if (!DB) {
manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
// Check for string describing special processing. Add these here for individual cases
PU_Study_ = false;
Study_type_ = pset.getUntrackedParameter<std::string>("Special_Pileup_Studies", "");
if (Study_type_ == "Fixed_ITPU_Vary_OOTPU") {
PU_Study_ = true;
intFixed_ITPU_ = pset.getUntrackedParameter<int>("intFixed_ITPU", 0);
}
if (manage_OOT_) { // figure out what the parameters are
// if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
// << " manage_OOT option not allowed with playback ";
std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");
if (OOT_type == "Poisson" || OOT_type == "poisson") {
poisson_OOT_ = true;
} else if (OOT_type == "Fixed" || OOT_type == "fixed") {
fixed_OOT_ = true;
// read back the fixed number requested out-of-time
intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
if (intFixed_OOT_ < 0) {
throw cms::Exception("Illegal parameter value", "PileUp::PileUp(ParameterSet const& pset)")
<< " Fixed out-of-time pileup requested, but no fixed value given ";
}
} else {
throw cms::Exception("Illegal parameter value", "PileUp::PileUp(ParameterSet const& pset)")
<< "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
<< "Legal values are 'poisson' or 'fixed'\n";
}
edm::LogInfo("MixingModule") << " Out-of-time pileup will be generated with a " << OOT_type
<< " distribution. ";
}
}
if (Source_type_ == "cosmics") { // allow for some extra flexibility for mixing
minBunch_cosmics_ = pset.getUntrackedParameter<int>("minBunch_cosmics", -1000);
maxBunch_cosmics_ = pset.getUntrackedParameter<int>("maxBunch_cosmics", 1000);
}
} // end of constructor
void PileUp::beginStream(edm::StreamID) {
auto iID = eventPrincipal_->streamID(); // each producer has its own workermanager, so use default streamid
streamContext_.reset(new StreamContext(iID, processContext_.get()));
input_->doBeginJob();
if (provider_.get() != nullptr) {
//TODO for now, we do not support consumes from EventSetup
provider_->beginJob(*productRegistry_, eventsetup::ESRecordsToProxyIndices{{}});
provider_->beginStream(iID, *streamContext_);
}
}
void PileUp::endStream() {
if (provider_.get() != nullptr) {
provider_->endStream(streamContext_->streamID(), *streamContext_);
provider_->endJob();
}
input_->doEndJob();
}
void PileUp::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
if (provider_.get() != nullptr) {
auto aux = std::make_shared<RunAuxiliary>(run.runAuxiliary());
runPrincipal_.reset(new RunPrincipal(aux, productRegistry_, *processConfiguration_, nullptr, 0));
provider_->beginRun(*runPrincipal_, setup.impl(), run.moduleCallingContext(), *streamContext_);
}
}
void PileUp::beginLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& setup) {
if (provider_.get() != nullptr) {
lumiPrincipal_.reset(new LuminosityBlockPrincipal(productRegistry_, *processConfiguration_, nullptr, 0));
lumiPrincipal_->setAux(lumi.luminosityBlockAuxiliary());
lumiPrincipal_->setRunPrincipal(runPrincipal_);
provider_->beginLuminosityBlock(*lumiPrincipal_, setup.impl(), lumi.moduleCallingContext(), *streamContext_);
}
}
void PileUp::endRun(const edm::Run& run, const edm::EventSetup& setup) {
if (provider_.get() != nullptr) {
provider_->endRun(*runPrincipal_, setup.impl(), run.moduleCallingContext(), *streamContext_);
}
}
void PileUp::endLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& setup) {
if (provider_.get() != nullptr) {
provider_->endLuminosityBlock(*lumiPrincipal_, setup.impl(), lumi.moduleCallingContext(), *streamContext_);
}
}
void PileUp::setupPileUpEvent(const edm::EventSetup& setup) {
if (provider_.get() != nullptr) {
// note: run and lumi numbers must be modified to match lumiPrincipal_
eventPrincipal_->setLuminosityBlockPrincipal(lumiPrincipal_.get());
eventPrincipal_->setRunAndLumiNumber(lumiPrincipal_->run(), lumiPrincipal_->luminosityBlock());
provider_->setupPileUpEvent(*eventPrincipal_, setup.impl(), *streamContext_);
}
}
void PileUp::reload(const edm::EventSetup& setup) {
//get the required parameters from DB.
edm::ESHandle<MixingModuleConfig> configM;
setup.get<MixingRcd>().get(configM);
const MixingInputConfig& config = configM->config(inputType_);
//get the type
type_ = config.type();
//set booleans
histoDistribution_ = type_ == "histo";
probFunctionDistribution_ = type_ == "probFunction";
poisson_ = type_ == "poisson";
fixed_ = type_ == "fixed";
none_ = type_ == "none";
if (histoDistribution_)
edm::LogError("MisConfiguration") << "type histo cannot be reloaded from DB, yet";
if (fixed_) {
averageNumber_ = averageNumber();
} else if (poisson_) {
averageNumber_ = config.averageNumber();
if (PoissonDistribution_) {
PoissonDistribution_ = std::make_unique<CLHEP::RandPoissonQ>(PoissonDistribution_->engine(), averageNumber_);
}
} else if (probFunctionDistribution_) {
//need to reload the histogram from DB
const std::vector<int>& dataProbFunctionVar = config.probFunctionVariable();
std::vector<double> dataProb = config.probValue();
int varSize = (int)dataProbFunctionVar.size();
int probSize = (int)dataProb.size();
if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
throw cms::Exception("BadProbFunction")
<< "Please, check the variables of the probability function! The first variable should be 0 and the "
"difference between two variables should be 1."
<< std::endl;
// Complete the vector containing the probability function data
// with the values "0"
if (probSize < varSize) {
edm::LogWarning("MixingModule") << " The probability function data will be completed with "
<< (varSize - probSize) << " values 0.";
for (int i = 0; i < (varSize - probSize); i++)
dataProb.push_back(0);
probSize = dataProb.size();
edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
}
// Create an histogram with the data from the probability function provided by the user
int xmin = (int)dataProbFunctionVar[0];
int xmax = (int)dataProbFunctionVar[varSize - 1] + 1; // need upper edge to be one beyond last value
int numBins = varSize;
edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range (" << xmin
<< "," << xmax << ")." << std::endl;
histo_.reset(new TH1F("h", "Histo from the user's probability function", numBins, xmin, xmax));
LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
for (int j = 0; j < numBins; j++) {
LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j] << " P(x) = " << dataProb[j];
histo_->Fill(dataProbFunctionVar[j] + 0.5,
dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
}
// Check if the histogram is normalized
if (std::abs(histo_->Integral() - 1) > 1.0e-02) {
throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << std::endl;
}
averageNumber_ = histo_->GetMean();
}
int oot = config.outOfTime();
manage_OOT_ = false;
if (oot == 1) {
manage_OOT_ = true;
poisson_OOT_ = false;
fixed_OOT_ = true;
intFixed_OOT_ = config.fixedOutOfTime();
} else if (oot == 2) {
manage_OOT_ = true;
poisson_OOT_ = true;
fixed_OOT_ = false;
}
}
PileUp::~PileUp() {}
std::unique_ptr<CLHEP::RandPoissonQ> const& PileUp::poissonDistribution(StreamID const& streamID) {
if (!PoissonDistribution_) {
CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
PoissonDistribution_ = std::make_unique<CLHEP::RandPoissonQ>(engine, averageNumber_);
}
return PoissonDistribution_;
}
std::unique_ptr<CLHEP::RandPoisson> const& PileUp::poissonDistr_OOT(StreamID const& streamID) {
if (!PoissonDistr_OOT_) {
CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
PoissonDistr_OOT_ = std::make_unique<CLHEP::RandPoisson>(engine);
}
return PoissonDistr_OOT_;
}
CLHEP::HepRandomEngine* PileUp::randomEngine(StreamID const& streamID) {
if (!randomEngine_) {
Service<RandomNumberGenerator> rng;
randomEngine_ = &rng->getEngine(streamID);
}
return randomEngine_;
}
void PileUp::CalculatePileup(int MinBunch,
int MaxBunch,
std::vector<int>& PileupSelection,
std::vector<float>& TrueNumInteractions,
StreamID const& streamID) {
// if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
// crossing zero first, save it for later.
int nzero_crossing = -1;
double Fnzero_crossing = -1;
if (manage_OOT_) {
if (none_) {
nzero_crossing = 0;
} else if (poisson_) {
nzero_crossing = poissonDistribution(streamID)->fire();
} else if (fixed_) {
nzero_crossing = intAverage_;
} else if (histoDistribution_ || probFunctionDistribution_) {
// RANDOM_NUMBER_ERROR
// Random number should be generated by the engines from the
// RandomNumberGeneratorService. This appears to use the global
// engine in ROOT. This is not thread safe unless the module using
// it is a one module and declares a shared resource and all
// other modules using it also declare the same shared resource.
// This also breaks replay.
double d = GetRandom(histo_.get(), randomEngine(streamID));
//n = (int) floor(d + 0.5); // incorrect for bins with integer edges
Fnzero_crossing = d;
nzero_crossing = int(d);
}
}
for (int bx = MinBunch; bx < MaxBunch + 1; ++bx) {
if (manage_OOT_) {
if (bx == 0 && !poisson_OOT_) {
PileupSelection.push_back(nzero_crossing);
TrueNumInteractions.push_back(nzero_crossing);
} else {
if (poisson_OOT_) {
if (PU_Study_ && (Study_type_ == "Fixed_ITPU_Vary_OOTPU") && bx == 0) {
PileupSelection.push_back(intFixed_ITPU_);
} else {
PileupSelection.push_back(poissonDistr_OOT(streamID)->fire(Fnzero_crossing));
}
TrueNumInteractions.push_back(Fnzero_crossing);
} else {
PileupSelection.push_back(intFixed_OOT_);
TrueNumInteractions.push_back(intFixed_OOT_);
}
}
} else {
if (none_) {
PileupSelection.push_back(0);
TrueNumInteractions.push_back(0.);
} else if (poisson_) {
PileupSelection.push_back(poissonDistribution(streamID)->fire());
TrueNumInteractions.push_back(averageNumber_);
} else if (fixed_) {
PileupSelection.push_back(intAverage_);
TrueNumInteractions.push_back(intAverage_);
} else if (histoDistribution_ || probFunctionDistribution_) {
// RANDOM_NUMBER_ERROR
// Random number should be generated by the engines from the
// RandomNumberGeneratorService. This appears to use the global
// engine in ROOT. This is not thread safe unless the module using
// it is a one module and declares a shared resource and all
// other modules using it also declare the same shared resource.
// This also breaks replay.
double d = GetRandom(histo_.get(), randomEngine(streamID));
PileupSelection.push_back(int(d));
TrueNumInteractions.push_back(d);
}
}
}
}
} //namespace edm