/
LHESource.cc
212 lines (180 loc) · 6.92 KB
/
LHESource.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
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <memory>
#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_deque.hpp>
#include "FWCore/Framework/interface/InputSourceMacros.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventPrincipal.h"
#include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
#include "FWCore/Framework/interface/RunPrincipal.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/TypeID.h"
#include "DataFormats/Common/interface/OrphanHandle.h"
#include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
#include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
#include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
#include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
#include "GeneratorInterface/LHEInterface/interface/LHEReader.h"
#include "LHESource.h"
using namespace lhef;
LHESource::LHESource(const edm::ParameterSet ¶ms,
const edm::InputSourceDescription &desc) :
ProducerSourceFromFiles(params, desc, false),
reader(new LHEReader(fileNames(), params.getUntrackedParameter<unsigned int>("skipEvents", 0))),
wasMerged(false),
lheProvenanceHelper_(edm::TypeID(typeid(LHEEventProduct)), edm::TypeID(typeid(LHERunInfoProduct)), productRegistryUpdate()),
phid_(),
runPrincipal_()
{
nextEvent();
lheProvenanceHelper_.lheAugment(runInfo.get());
// Initialize metadata, and save the process history ID for use every event.
phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
// These calls are not wanted, because the principals are used for putting the products.
//produces<LHEEventProduct>();
//produces<LHERunInfoProduct, edm::InRun>();
}
LHESource::~LHESource()
{
}
void LHESource::endJob()
{
reader.reset();
}
void LHESource::nextEvent()
{
if (partonLevel) {
return;
}
bool newFileOpened = false;
partonLevel = reader->next(&newFileOpened);
if (!partonLevel) {
return;
}
boost::shared_ptr<LHERunInfo> runInfoThis = partonLevel->getRunInfo();
if (runInfoThis != runInfoLast) {
runInfo = runInfoThis;
runInfoLast = runInfoThis;
}
if (runInfo) {
std::auto_ptr<LHERunInfoProduct> product(
new LHERunInfoProduct(*runInfo->getHEPRUP()));
std::for_each(runInfo->getHeaders().begin(),
runInfo->getHeaders().end(),
boost::bind(
&LHERunInfoProduct::addHeader,
product.get(), _1));
std::for_each(runInfo->getComments().begin(),
runInfo->getComments().end(),
boost::bind(&LHERunInfoProduct::addComment,
product.get(), _1));
if (!runInfoProducts.empty()) {
if (runInfoProducts.front().mergeProduct(*product)) {
if (!wasMerged) {
runInfoProducts.pop_front();
runInfoProducts.push_front(product);
wasMerged = true;
}
} else {
lheProvenanceHelper_.lheAugment(runInfo.get());
// Initialize metadata, and save the process history ID for use every event.
phid_ = lheProvenanceHelper_.lheInit(processHistoryRegistryForUpdate());
resetRunAuxiliary();
}
}
runInfo.reset();
}
}
// This is the only way we can now access the run principal.
void
LHESource::readRun_(edm::RunPrincipal& runPrincipal) {
runAuxiliary()->setProcessHistoryID(phid_);
runPrincipal.fillRunPrincipal(processHistoryRegistryForUpdate());
runPrincipal_ = &runPrincipal;
}
void
LHESource::readLuminosityBlock_(edm::LuminosityBlockPrincipal& lumiPrincipal) {
luminosityBlockAuxiliary()->setProcessHistoryID(phid_);
lumiPrincipal.fillLuminosityBlockPrincipal(processHistoryRegistryForUpdate());
}
void LHESource::beginRun(edm::Run&)
{
if (runInfoLast) {
runInfo = runInfoLast;
std::unique_ptr<LHERunInfoProduct> product(
new LHERunInfoProduct(*runInfo->getHEPRUP()));
std::for_each(runInfo->getHeaders().begin(),
runInfo->getHeaders().end(),
boost::bind(
&LHERunInfoProduct::addHeader,
product.get(), _1));
std::for_each(runInfo->getComments().begin(),
runInfo->getComments().end(),
boost::bind(&LHERunInfoProduct::addComment,
product.get(), _1));
// keep a copy around in case of merging
runInfoProducts.push_back(new LHERunInfoProduct(*product));
wasMerged = false;
std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHERunInfoProduct>(std::move(product)));
runPrincipal_->put(lheProvenanceHelper_.runProductBranchDescription_, std::move(rdp));
runInfo.reset();
}
}
void LHESource::endRun(edm::Run&)
{
if (!runInfoProducts.empty()) {
std::unique_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHERunInfoProduct>(std::move(product)));
runPrincipal_->put(lheProvenanceHelper_.runProductBranchDescription_, std::move(rdp));
}
runPrincipal_ = nullptr;
}
bool LHESource::setRunAndEventInfo(edm::EventID&, edm::TimeValue_t&, edm::EventAuxiliary::ExperimentType&)
{
nextEvent();
if (!partonLevel) {
// We just finished an input file. See if there is another.
nextEvent();
if (!partonLevel) {
// No more input files.
return false;
}
}
return true;
}
void
LHESource::readEvent_(edm::EventPrincipal& eventPrincipal) {
assert(eventCached() || processingMode() != RunsLumisAndEvents);
edm::EventAuxiliary aux(eventID(), processGUID(), edm::Timestamp(presentTime()), false);
aux.setProcessHistoryID(phid_);
eventPrincipal.fillEventPrincipal(aux, processHistoryRegistryForUpdate());
std::unique_ptr<LHEEventProduct> product(
new LHEEventProduct(*partonLevel->getHEPEUP(),
partonLevel->originalXWGTUP())
);
if (partonLevel->getPDF()) {
product->setPDF(*partonLevel->getPDF());
}
std::for_each(partonLevel->weights().begin(),
partonLevel->weights().end(),
boost::bind(&LHEEventProduct::addWeight,
product.get(), _1));
product->setScales(partonLevel->scales());
product->setNpLO(partonLevel->npLO());
product->setNpNLO(partonLevel->npNLO());
std::for_each(partonLevel->getComments().begin(),
partonLevel->getComments().end(),
boost::bind(&LHEEventProduct::addComment,
product.get(), _1));
std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<LHEEventProduct>(std::move(product)));
eventPrincipal.put(lheProvenanceHelper_.eventProductBranchDescription_, std::move(edp), lheProvenanceHelper_.eventProductProvenance_);
partonLevel.reset();
resetEventCached();
}
DEFINE_FWK_INPUT_SOURCE(LHESource);