17 #include "GaudiKernel/IAppMgrUI.h"
18 #include "GaudiKernel/Bootstrap.h"
19 #include "GaudiKernel/ITHistSvc.h"
21 #include "Rivet/Rivet.hh"
22 #include "Rivet/Analysis.hh"
23 #include "Rivet/Config/RivetConfig.hh"
24 #include "Rivet/Tools/RivetYODA.hh"
37 m_needsConversion(false),
63 return val == NULL ? std::string() : std::string(
val);
80 std::string env_rap(
getenv_str(
"RIVET_ANALYSIS_PATH"));
81 if (!m_anapath.empty()) {
82 ATH_MSG_INFO(
"Setting Rivet plugin analyses loader env path: " << m_anapath);
83 if (!env_rap.empty())
ATH_MSG_INFO(
"Overwriting environment's RIVET_ANALYSIS_PATH = " << env_rap <<
"!");
84 setenv(
"RIVET_ANALYSIS_PATH", m_anapath.c_str(), 1);
89 if (!env_rap.empty())
ATH_MSG_DEBUG(
"Loading Rivet plugin analyses from env path: " << env_rap);
92 #if RIVET_VERSION_CODE >= 40100
93 m_analysisHandler =
new Rivet::AnalysisHandler();
95 m_analysisHandler =
new Rivet::AnalysisHandler(m_runname);
97 assert(m_analysisHandler);
99 #if RIVET_VERSION_CODE >= 40000
100 m_analysisHandler->setCheckBeams(!m_ignorebeams);
101 m_analysisHandler->matchWeightNames(m_matchWeights);
102 m_analysisHandler->unmatchWeightNames(m_unmatchWeights);
104 m_analysisHandler->setIgnoreBeams(m_ignorebeams);
105 m_analysisHandler->selectMultiWeights(m_matchWeights);
106 m_analysisHandler->deselectMultiWeights(m_unmatchWeights);
108 m_analysisHandler->skipMultiWeights(m_skipweights);
109 m_analysisHandler->setNominalWeightName(m_nominalWeightName);
110 if (m_weightcap>0) m_analysisHandler->setWeightCap(m_weightcap);
117 std::vector<std::string> analysisNames = Rivet::AnalysisLoader::analysisNames();
123 for (
const std::string&
a : m_analysisNames) {
125 m_analysisHandler->addAnalysis(
a);
133 if (m_preload!=
"") {
134 m_analysisHandler->readData(m_preload);
137 return StatusCode::SUCCESS;
144 const EventContext& ctx = getContext();
149 std::unique_ptr<HepMC::GenEvent> checkedEvent;
153 ATH_MSG_ERROR(
"Could not retrieve TruthEvents collection, aborting.");
154 return StatusCode::FAILURE;
158 ATH_MSG_DEBUG(
"Attempt xAOD::Truth to HepMC::GenEvent conversion.");
159 std::vector<HepMC::GenEvent>&& hepmc_evts =
m_xAODtoHepMCTool->getHepMCEvents(truthCollection, eventInfo.cptr());
161 if (hepmc_evts.empty()) {
162 ATH_MSG_ERROR(
"Conversion didn't yield HepMC::GenEvent, aborting.");
163 return StatusCode::FAILURE;
165 checkedEvent =
checkEvent(std::move(hepmc_evts[0]), ctx);
172 if (
sc.isFailure() || eventCollection == 0) {
174 return StatusCode::FAILURE;
182 const HepMC::GenEvent*
event = eventCollection->
front();
183 if (
event ==
nullptr) {
185 return StatusCode::FAILURE;
194 return StatusCode::FAILURE;
206 return StatusCode::SUCCESS;
224 if (
m_file.find(
".yoda") == std::string::npos)
m_file +=
".yoda.gz";
226 if (
pos == std::string::npos) {
233 if (std::rename((
m_file.substr(0,
pos)+
".gz").c_str(),
m_file.c_str()) !=0) {
234 std::string error_msg =
"Impossible to rename ";
235 error_msg +=
m_file.substr(0,
pos)+
".gz";
239 return StatusCode::FAILURE;
243 return StatusCode::SUCCESS;
250 return a->momentum().e() >
b->momentum().e();
253 inline std::vector<std::string>
split(
const std::string&
input,
const std::string&
regex) {
256 std::sregex_token_iterator
259 return {
first, last};
263 auto modEvent = std::make_unique<HepMC::GenEvent>(
event);
268 modEvent->set_event_number(
static_cast<int>(evtInfo->
eventNumber()));
273 std::shared_ptr<HepMC3::GenRunInfo> modRunInfo;
274 if (
event.run_info()) {
275 modRunInfo = std::make_shared<HepMC3::GenRunInfo>(*(
event.run_info().get()));
276 if (
event.run_info()->weight_names().empty() &&
event.weights().size() == 1) {
277 modRunInfo->set_weight_names({
"Default"});
282 modRunInfo = std::make_shared<HepMC3::GenRunInfo>();
283 std::vector<std::string> w_names;
284 for (
size_t i = 0;
i <
event.weights().
size();
i++) { w_names.push_back(std::string(
"badweight") +
std::to_string(
i)); }
285 modRunInfo->set_weight_names(w_names);
287 modEvent->set_run_info(modRunInfo);
288 std::vector<std::string> w_names = modEvent->weight_names();
289 if (w_names.size()) {
290 std::vector<std::pair<std::string,std::string> > w_subs = {
303 for (std::string& wname : w_names) {
304 for (
const auto& sub : w_subs) {
305 size_t start_pos = wname.find(sub.first);
306 while (start_pos != std::string::npos) {
307 wname.replace(start_pos, sub.first.length(), sub.second);
308 start_pos = wname.find(sub.first);
312 modEvent->run_info()->set_weight_names(w_names);
315 const HepMC::WeightContainer& old_wc =
event.weights();
316 std::vector<std::string> old_wnames = old_wc.weight_names();
317 if (old_wnames.size()) {
318 HepMC::WeightContainer& new_wc = modEvent->weights();
320 std::vector<std::pair<std::string,std::string> > w_subs = {
333 std::map<std::string, double> new_name_to_value;
334 std::map<std::string, std::string> old_name_to_new_name;
335 for (
const std::string& old_name : old_wnames) {
336 std::string wname = std::string(old_name);
337 double value = old_wc[old_name];
338 for (
const auto& sub : w_subs) {
339 size_t start_pos = wname.find(sub.first);
340 while (start_pos != std::string::npos) {
341 wname.replace(start_pos, sub.first.length(), sub.second);
342 start_pos = wname.find(sub.first);
345 new_name_to_value[wname] =
value;
346 old_name_to_new_name[old_name] = wname;
348 auto itEnd = old_name_to_new_name.end();
349 for (
const std::string& old_name : old_wnames) {
350 if (old_name_to_new_name.find(old_name) == itEnd)
continue;
351 const std::string& new_name = old_name_to_new_name[old_name];
352 new_wc[ new_name ] = new_name_to_value[new_name];
360 if (modEvent->particles().size() == 1) modEvent->set_beam_particles(modEvent->particles().front(), modEvent->particles().front());
365 for (
const auto&
p : modEvent->particles()) {
367 etotal +=
p->momentum().e();
369 const double ebeam = 0.5*etotal;
371 HepMC::GenParticlePtr b1 = std::make_shared<HepMC::GenParticle>(HepMC::FourVector(0,0,ebeam,ebeam),2212,4);
372 HepMC::GenParticlePtr b2 = std::make_shared<HepMC::GenParticle>(HepMC::FourVector(0,0,-1*ebeam,ebeam),2212,4);
374 std::vector<HepMC::GenParticlePtr> notBeams;
379 for (
const auto& bp : notBeams) bp->production_vertex()->remove_particle_out(bp);
381 modEvent->set_beam_particles(b1, b2);
383 if (modEvent->beams().front()->momentum().e() > 50000.0) {
388 if (modEvent->particles_size() == 1) modEvent->set_beam_particles(*modEvent->particles_begin(), *modEvent->particles_begin());
389 if (modEvent->beam_particles().first->momentum().e() > 50000.0) {
399 for (
auto&
p:
evt.particles()) {
400 p->set_momentum(
p->momentum()*0.001);
403 const HepMC::FourVector&
mom =
p->momentum();
404 p->set_momentum(HepMC::FourVector (
mom.px()*0.001,
409 p->set_generated_mass(
p->generated_mass()*0.001);