CMS 3D CMS Logo

LHESource.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <functional>
3 #include <iostream>
4 #include <string>
5 #include <memory>
6 
7 #include <boost/bind.hpp>
8 #include <boost/ptr_container/ptr_deque.hpp>
9 
18 
23 
27 
31 
32 #include "LHESource.h"
33 
34 using namespace lhef;
35 
37  const edm::InputSourceDescription &desc) :
38  ProducerSourceFromFiles(params, desc, false),
39  reader(new LHEReader(fileNames(), params.getUntrackedParameter<unsigned int>("skipEvents", 0))),
40  wasMerged(false),
41  lheProvenanceHelper_(edm::TypeID(typeid(LHEEventProduct)), edm::TypeID(typeid(LHERunInfoProduct)), productRegistryUpdate()),
42  phid_(),
43  runPrincipal_()
44 {
45  nextEvent();
47  // Initialize metadata, and save the process history ID for use every event.
49 
50  // These calls are not wanted, because the principals are used for putting the products.
51  //produces<LHEEventProduct>();
52  //produces<LHERunInfoProduct, edm::InRun>();
53 }
54 
56 {
57 }
58 
60 {
61  reader.reset();
62 }
63 
65 {
66  if (partonLevel) {
67  return;
68  }
69 
70  bool newFileOpened;
71  do {
72  newFileOpened = false;
73  partonLevel = reader->next(&newFileOpened);
74  } while (newFileOpened && !partonLevel);
75 
76  if (!partonLevel) {
77  return;
78  }
79 
80  boost::shared_ptr<LHERunInfo> runInfoThis = partonLevel->getRunInfo();
81  if (runInfoThis != runInfoLast) {
82  runInfo = runInfoThis;
83  runInfoLast = runInfoThis;
84  }
85  if (runInfo) {
86  std::auto_ptr<LHERunInfoProduct> product(
87  new LHERunInfoProduct(*runInfo->getHEPRUP()));
88  std::for_each(runInfo->getHeaders().begin(),
89  runInfo->getHeaders().end(),
90  boost::bind(
92  product.get(), _1));
93  std::for_each(runInfo->getComments().begin(),
94  runInfo->getComments().end(),
95  boost::bind(&LHERunInfoProduct::addComment,
96  product.get(), _1));
97 
98  if (!runInfoProducts.empty()) {
99  if (runInfoProducts.front().mergeProduct(*product)) {
100  if (!wasMerged) {
101  runInfoProducts.pop_front();
102  runInfoProducts.push_front(product);
103  wasMerged = true;
104  }
105  } else {
107  // Initialize metadata, and save the process history ID for use every event.
110  }
111  }
112 
113  runInfo.reset();
114  }
115 }
116 
117 // This is the only way we can now access the run principal.
118 void
120  runAuxiliary()->setProcessHistoryID(phid_);
122  runPrincipal_ = &runPrincipal;
123 }
124 
125 void
127  luminosityBlockAuxiliary()->setProcessHistoryID(phid_);
129 }
130 
132 {
133  if (runInfoLast) {
135 
136  std::unique_ptr<LHERunInfoProduct> product(
137  new LHERunInfoProduct(*runInfo->getHEPRUP()));
138  std::for_each(runInfo->getHeaders().begin(),
139  runInfo->getHeaders().end(),
140  boost::bind(
142  product.get(), _1));
143  std::for_each(runInfo->getComments().begin(),
144  runInfo->getComments().end(),
145  boost::bind(&LHERunInfoProduct::addComment,
146  product.get(), _1));
147 
148  // keep a copy around in case of merging
149  runInfoProducts.push_back(new LHERunInfoProduct(*product));
150  wasMerged = false;
151 
152  std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHERunInfoProduct>(std::move(product)));
154 
155  runInfo.reset();
156  }
157 }
158 
160 {
161  if (!runInfoProducts.empty()) {
162  std::unique_ptr<LHERunInfoProduct> product(
163  runInfoProducts.pop_front().release());
164  std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHERunInfoProduct>(std::move(product)));
166  }
167  runPrincipal_ = nullptr;
168 }
169 
171 {
172  nextEvent();
173  if (!partonLevel) {
174  // We just finished an input file. See if there is another.
175  nextEvent();
176  if (!partonLevel) {
177  // No more input files.
178  return false;
179  }
180  }
181  return true;
182 }
183 
184 void
190 
191  std::unique_ptr<LHEEventProduct> product(
192  new LHEEventProduct(*partonLevel->getHEPEUP(),
193  partonLevel->originalXWGTUP())
194  );
195  if (partonLevel->getPDF()) {
196  product->setPDF(*partonLevel->getPDF());
197  }
198  std::for_each(partonLevel->weights().begin(),
199  partonLevel->weights().end(),
200  boost::bind(&LHEEventProduct::addWeight,
201  product.get(), _1));
202  product->setScales(partonLevel->scales());
203  product->setNpLO(partonLevel->npLO());
204  product->setNpNLO(partonLevel->npNLO());
205  std::for_each(partonLevel->getComments().begin(),
206  partonLevel->getComments().end(),
207  boost::bind(&LHEEventProduct::addComment,
208  product.get(), _1));
209 
210  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<LHEEventProduct>(std::move(product)));
212 
213  partonLevel.reset();
214 
216 }
217 
218 std::shared_ptr<edm::RunAuxiliary>
221  resetNewRun();
222  auto aux = std::make_shared<edm::RunAuxiliary>(eventID().run(), ts, edm::Timestamp::invalidTimestamp());
223  aux->setProcessHistoryID(phid_);
224  return aux;
225 }
226 
227 std::shared_ptr<edm::LuminosityBlockAuxiliary>
229  if (processingMode() == Runs) return std::shared_ptr<edm::LuminosityBlockAuxiliary>();
231  resetNewLumi();
232  auto aux = std::make_shared<edm::LuminosityBlockAuxiliary>(eventID().run(), eventID().luminosityBlock(),
234  aux->setProcessHistoryID(phid_);
235  return aux;
236 }
237 
RunNumber_t run() const
Definition: EventID.h:39
boost::shared_ptr< lhef::LHERunInfo > runInfo
Definition: LHESource.h:56
std::shared_ptr< edm::LuminosityBlockAuxiliary > readLuminosityBlockAuxiliary_() override
Definition: LHESource.cc:228
static Timestamp invalidTimestamp()
Definition: Timestamp.h:101
void addHeader(const Header &header)
void addComment(const std::string &line)
void endJob() override
Definition: LHESource.cc:59
edm::ProcessHistoryID phid_
Definition: LHESource.h:62
std::shared_ptr< edm::RunAuxiliary > readRunAuxiliary_() override
Definition: LHESource.cc:219
edm::LHEProvenanceHelper lheProvenanceHelper_
Definition: LHESource.h:61
void readLuminosityBlock_(edm::LuminosityBlockPrincipal &lumiPrincipal) override
Definition: LHESource.cc:126
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp) const
Definition: RunPrincipal.cc:32
void endRun(edm::Run &run) override
Definition: LHESource.cc:159
void addWeight(const WGT &wgt)
void resetRunAuxiliary(bool isNewRun=true) const
Definition: InputSource.h:355
ProcessingMode processingMode() const
RunsLumisAndEvents (default), RunsAndLumis, or Runs.
Definition: InputSource.h:246
boost::shared_ptr< lhef::LHEEvent > partonLevel
Definition: LHESource.h:57
void fillRunPrincipal(ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
Definition: RunPrincipal.cc:20
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
std::string const & processGUID() const
Accessor for global process identifier.
Definition: InputSource.h:211
bool setRunAndEventInfo(edm::EventID &, edm::TimeValue_t &, edm::EventAuxiliary::ExperimentType &) override
Definition: LHESource.cc:170
boost::shared_ptr< lhef::LHERunInfo > runInfoLast
Definition: LHESource.h:55
BranchDescription const runProductBranchDescription_
void fillLuminosityBlockPrincipal(ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
#define DEFINE_FWK_INPUT_SOURCE(type)
void beginRun(edm::Run &run) override
Definition: LHESource.cc:131
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
unsigned long long TimeValue_t
Definition: Timestamp.h:28
void nextEvent()
Definition: LHESource.cc:64
void resetEventCached()
Definition: InputSource.h:377
LHESource(const edm::ParameterSet &params, const edm::InputSourceDescription &desc)
Definition: LHESource.cc:36
~LHESource() override
Definition: LHESource.cc:55
void readEvent_(edm::EventPrincipal &eventPrincipal) override
Definition: LHESource.cc:185
ProcessHistoryID lheInit(ProcessHistoryRegistry &processHistoryRegistry)
void addComment(const std::string &line)
bool wasMerged
Definition: LHESource.h:60
TimeValue_t presentTime() const
edm::RunPrincipal * runPrincipal_
Definition: LHESource.h:63
void setProcessHistoryID(ProcessHistoryID const &phid)
std::shared_ptr< RunAuxiliary > runAuxiliary() const
Called by the framework to merge or insert run in principal cache.
Definition: InputSource.h:252
void readRun_(edm::RunPrincipal &runPrincipal) override
Definition: LHESource.cc:119
ProcessHistoryRegistry & processHistoryRegistryForUpdate()
Definition: InputSource.h:345
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
HLT enums.
EventID const & eventID() const
std::auto_ptr< lhef::LHEReader > reader
Definition: LHESource.h:53
ProductProvenance eventProductProvenance_
std::shared_ptr< LuminosityBlockAuxiliary > luminosityBlockAuxiliary() const
Called by the framework to merge or insert lumi in principal cache.
Definition: InputSource.h:255
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
Definition: LHESource.h:59
def move(src, dest)
Definition: eostools.py:510
BranchDescription const eventProductBranchDescription_
void lheAugment(lhef::LHERunInfo const *runInfo)
Definition: Run.h:42
bool eventCached() const
Definition: InputSource.h:374