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"
62 return val == NULL ? std::string() : std::string(
val);
79 std::string env_rap(
getenv_str(
"RIVET_ANALYSIS_PATH"));
80 if (m_anapath.size() > 0) {
81 ATH_MSG_INFO(
"Setting Rivet plugin analyses loader env path: " << m_anapath);
82 if (!env_rap.empty())
ATH_MSG_INFO(
"Overwriting environment's RIVET_ANALYSIS_PATH = " << env_rap <<
"!");
83 setenv(
"RIVET_ANALYSIS_PATH", m_anapath.c_str(), 1);
87 std::vector<std::string> anapaths;
88 const std::string cmtpath =
getenv_str(
"CMTPATH");
89 if (cmtpath.empty()) {
90 ATH_MSG_WARNING(
"$CMTPATH variable not set: finding the main analysis plugin directory will be difficult...");
92 std::vector<std::string> cmtpaths;
93 std::stringstream
ss(cmtpath);
95 while (std::getline(
ss,
item,
':')) {
96 cmtpaths.push_back(std::move(
item));
98 const std::string cmtconfig =
getenv_str(
"CMTCONFIG");
99 if (cmtconfig.empty()) {
100 ATH_MSG_WARNING(
"$CMTCONFIG variable not set: finding the main analysis plugin directory will be difficult...");
103 const std::string libpath =
"/InstallArea/" + cmtconfig +
"/lib";
104 for (
const std::string&
p : cmtpaths) {
105 const std::string cmtlibpath =
p + libpath;
107 ATH_MSG_INFO(
"Appending " + cmtlibpath +
" to default Rivet analysis search path");
108 anapaths.push_back(cmtlibpath);
115 std::string anapathstr =
getenv_str(
"RIVET_ANALYSIS_PATH");
116 for (
const std::string&
ap : anapaths) {
117 if (anapathstr.size() > 0) anapathstr +=
":";
120 setenv(
"RIVET_ANALYSIS_PATH", anapathstr.c_str(), 1);
124 if (!env_rap.empty())
ATH_MSG_DEBUG(
"Loading Rivet plugin analyses from env path: " << env_rap);
127 m_analysisHandler =
new Rivet::AnalysisHandler(m_runname);
128 assert(m_analysisHandler);
130 #if RIVET_VERSION_CODE >= 40000
131 m_analysisHandler->setCheckBeams(!m_ignorebeams);
132 m_analysisHandler->matchWeightNames(m_matchWeights);
133 m_analysisHandler->unmatchWeightNames(m_unmatchWeights);
135 m_analysisHandler->setIgnoreBeams(m_ignorebeams);
136 m_analysisHandler->selectMultiWeights(m_matchWeights);
137 m_analysisHandler->deselectMultiWeights(m_unmatchWeights);
139 m_analysisHandler->skipMultiWeights(m_skipweights);
140 m_analysisHandler->setNominalWeightName(m_nominalWeightName);
141 if (m_weightcap>0) m_analysisHandler->setWeightCap(m_weightcap);
148 std::vector<std::string> analysisNames = Rivet::AnalysisLoader::analysisNames();
154 for (
const std::string&
a : m_analysisNames) {
156 m_analysisHandler->addAnalysis(
a);
164 if (m_preload!=
"") {
165 m_analysisHandler->readData(m_preload);
168 return StatusCode::SUCCESS;
175 const EventContext& ctx = getContext();
180 std::unique_ptr<HepMC::GenEvent> checkedEvent;
184 ATH_MSG_ERROR(
"Could not retrieve TruthEvents collection, aborting.");
185 return StatusCode::FAILURE;
189 ATH_MSG_DEBUG(
"Attempt xAOD::Truth to HepMC::GenEvent conversion.");
190 std::vector<HepMC::GenEvent>&& hepmc_evts =
m_xAODtoHepMCTool->getHepMCEvents(truthCollection, eventInfo.
cptr());
192 if (hepmc_evts.empty()) {
193 ATH_MSG_ERROR(
"Conversion didn't yield HepMC::GenEvent, aborting.");
194 return StatusCode::FAILURE;
196 checkedEvent =
checkEvent(std::move(hepmc_evts[0]), ctx);
203 if (
sc.isFailure() || eventCollection == 0) {
205 return StatusCode::FAILURE;
213 const HepMC::GenEvent*
event = eventCollection->
front();
214 if (
event ==
nullptr) {
216 return StatusCode::FAILURE;
225 return StatusCode::FAILURE;
237 return StatusCode::SUCCESS;
255 if (
m_file.find(
".yoda") == std::string::npos)
m_file +=
".yoda.gz";
257 if (
pos == std::string::npos) {
264 if (std::rename((
m_file.substr(0,
pos)+
".gz").c_str(),
m_file.c_str()) !=0) {
265 std::string error_msg =
"Impossible to rename ";
266 error_msg +=
m_file.substr(0,
pos)+
".gz";
270 return StatusCode::FAILURE;
274 return StatusCode::SUCCESS;
281 return a->momentum().e() >
b->momentum().e();
284 inline std::vector<std::string>
split(
const std::string&
input,
const std::string&
regex) {
287 std::sregex_token_iterator
290 return {
first, last};
294 auto modEvent = std::make_unique<HepMC::GenEvent>(
event);
299 modEvent->set_event_number(
static_cast<int>(evtInfo->
eventNumber()));
304 std::shared_ptr<HepMC3::GenRunInfo> modRunInfo;
305 if (
event.run_info()) {
306 modRunInfo = std::make_shared<HepMC3::GenRunInfo>(*(
event.run_info().get()));
307 if (
event.run_info()->weight_names().empty() &&
event.weights().size() == 1) {
308 modRunInfo->set_weight_names({
"Default"});
313 modRunInfo = std::make_shared<HepMC3::GenRunInfo>();
314 std::vector<std::string> w_names;
315 for (
size_t i = 0;
i <
event.weights().
size();
i++) { w_names.push_back(std::string(
"badweight") +
std::to_string(
i)); }
316 modRunInfo->set_weight_names(w_names);
318 modEvent->set_run_info(modRunInfo);
319 std::vector<std::string> w_names = modEvent->weight_names();
320 if (w_names.size()) {
321 std::vector<std::pair<std::string,std::string> > w_subs = {
334 for (std::string& wname : w_names) {
335 for (
const auto& sub : w_subs) {
336 size_t start_pos = wname.find(sub.first);
337 while (start_pos != std::string::npos) {
338 wname.replace(start_pos, sub.first.length(), sub.second);
339 start_pos = wname.find(sub.first);
343 modEvent->run_info()->set_weight_names(w_names);
346 const HepMC::WeightContainer& old_wc =
event.weights();
347 std::vector<std::string> old_wnames = old_wc.weight_names();
348 if (old_wnames.size()) {
349 HepMC::WeightContainer& new_wc = modEvent->weights();
351 std::vector<std::pair<std::string,std::string> > w_subs = {
364 std::map<std::string, double> new_name_to_value;
365 std::map<std::string, std::string> old_name_to_new_name;
366 for (
const std::string& old_name : old_wnames) {
367 std::string wname = std::string(old_name);
368 double value = old_wc[old_name];
369 for (
const auto& sub : w_subs) {
370 size_t start_pos = wname.find(sub.first);
371 while (start_pos != std::string::npos) {
372 wname.replace(start_pos, sub.first.length(), sub.second);
373 start_pos = wname.find(sub.first);
376 new_name_to_value[wname] =
value;
377 old_name_to_new_name[old_name] = wname;
379 auto itEnd = old_name_to_new_name.end();
380 for (
const std::string& old_name : old_wnames) {
381 if (old_name_to_new_name.find(old_name) == itEnd)
continue;
382 const std::string& new_name = old_name_to_new_name[old_name];
383 new_wc[ new_name ] = new_name_to_value[new_name];
391 if (modEvent->particles().size() == 1) modEvent->set_beam_particles(modEvent->particles().front(), modEvent->particles().front());
396 for (
const auto&
p : modEvent->particles()) {
398 etotal +=
p->momentum().e();
400 const double ebeam = 0.5*etotal;
402 HepMC::GenParticlePtr b1 = std::make_shared<HepMC::GenParticle>(HepMC::FourVector(0,0,ebeam,ebeam),2212,4);
403 HepMC::GenParticlePtr b2 = std::make_shared<HepMC::GenParticle>(HepMC::FourVector(0,0,-1*ebeam,ebeam),2212,4);
405 std::vector<HepMC::GenParticlePtr> notBeams;
410 for (
const auto& bp : notBeams) bp->production_vertex()->remove_particle_out(bp);
412 modEvent->set_beam_particles(b1, b2);
414 if (modEvent->beams().front()->momentum().e() > 50000.0) {
419 if (modEvent->particles_size() == 1) modEvent->set_beam_particles(*modEvent->particles_begin(), *modEvent->particles_begin());
420 if (modEvent->beam_particles().first->momentum().e() > 50000.0) {
430 for (
auto&
p:
evt.particles()) {
431 p->set_momentum(
p->momentum()*0.001);
434 const HepMC::FourVector&
mom =
p->momentum();
435 p->set_momentum(HepMC::FourVector (
mom.px()*0.001,
440 p->set_generated_mass(
p->generated_mass()*0.001);