From 5867588bf840ae37bc4e7a3c24ed2dfbc7b5843c Mon Sep 17 00:00:00 2001
From: Graeme Stewart <graemes.cern@gmail.com>
Date: Thu, 4 Aug 2016 10:57:28 +0200
Subject: [PATCH] Tagging Rivet_i-02-04-01 (Rivet_i-02-04-01)

	* Fix CMakeLists.txt for missing GSL dependency
	* https://gitlab.cern.ch/atlas/aogt6/commit/49c5bee8464d75cd74eddaf5149a1a5645902706
        * Tagging as Rivet_i-02-04-01

2015-10-26  Andy Buckley  <andy.buckley@cern.ch>

	* Tagging: Rivet_i-02-04-00

	* Add conversion-to-ROOT for Histo2D types (only guaranteed for Rivet >= 2.4 and corresponding YODA)

2015-07-29  James Monk  <jmonk@cern.ch>

	* Add WeightName property so that weight can be selected in the case of multiple input weights

	* Always make a copy of the input GenEvent


Former-commit-id: c885bd1820e730923f4f6d5abddc0b1551a8c4b9
---
 Generators/Rivet_i/CMakeLists.txt    |  9 +++---
 Generators/Rivet_i/Rivet_i/Rivet_i.h |  3 ++
 Generators/Rivet_i/cmt/requirements  |  1 +
 Generators/Rivet_i/src/Rivet_i.cxx   | 43 +++++++++++++++++++++++++---
 4 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/Generators/Rivet_i/CMakeLists.txt b/Generators/Rivet_i/CMakeLists.txt
index a7d029d17e5..c018ff34c60 100644
--- a/Generators/Rivet_i/CMakeLists.txt
+++ b/Generators/Rivet_i/CMakeLists.txt
@@ -21,6 +21,7 @@ find_package( HepMC )
 find_package( ROOT COMPONENTS Matrix Core Tree MathCore Hist RIO pthread )
 find_package( Rivet )
 find_package( YODA )
+find_package( GSL )
 
 # Remove the --as-needed linker flags:
 atlas_disable_as_needed()
@@ -30,12 +31,12 @@ atlas_add_library( Rivet_iLib
                    src/*.cxx
                    PUBLIC_HEADERS Rivet_i
                    INCLUDE_DIRS ${RIVET_INCLUDE_DIRS}
-                   PRIVATE_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS} ${HEPMC_INCLUDE_DIRS} ${YODA_INCLUDE_DIRS} ${FASTJET_INCLUDE_DIRS}
+                   PRIVATE_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS} ${HEPMC_INCLUDE_DIRS} ${YODA_INCLUDE_DIRS} ${FASTJET_INCLUDE_DIRS} ${GSL_INCLUDE_DIRS}
                    LINK_LIBRARIES ${RIVET_LIBRARIES} AthenaBaseComps GaudiKernel
-                   PRIVATE_LINK_LIBRARIES ${Boost_LIBRARIES} ${ROOT_LIBRARIES} ${HEPMC_LIBRARIES} ${YODA_LIBRARIES} ${FASTJET_LIBRARIES} AthenaKernel GeneratorObjects PathResolver )
+                   PRIVATE_LINK_LIBRARIES ${Boost_LIBRARIES} ${GSL_LIBRARIES} ${ROOT_LIBRARIES} ${HEPMC_LIBRARIES} ${YODA_LIBRARIES} ${FASTJET_LIBRARIES} AthenaKernel GeneratorObjects PathResolver )
 
 atlas_add_component( Rivet_i
                      src/components/*.cxx
-                     INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS} ${HEPMC_INCLUDE_DIRS} ${YODA_INCLUDE_DIRS} ${FASTJET_INCLUDE_DIRS} ${RIVET_INCLUDE_DIRS}
-                     LINK_LIBRARIES ${Boost_LIBRARIES} ${ROOT_LIBRARIES} ${HEPMC_LIBRARIES} ${YODA_LIBRARIES} ${FASTJET_LIBRARIES} ${RIVET_LIBRARIES} AthenaBaseComps GaudiKernel AthenaKernel GeneratorObjects PathResolver Rivet_iLib )
+                     INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS} ${HEPMC_INCLUDE_DIRS} ${YODA_INCLUDE_DIRS} ${FASTJET_INCLUDE_DIRS} ${RIVET_INCLUDE_DIRS} ${GSL_INCLUDE_DIRS}
+                     LINK_LIBRARIES ${Boost_LIBRARIES} ${GSL_LIBRARIES} ${ROOT_LIBRARIES} ${HEPMC_LIBRARIES} ${YODA_LIBRARIES} ${FASTJET_LIBRARIES} ${RIVET_LIBRARIES} AthenaBaseComps GaudiKernel AthenaKernel GeneratorObjects PathResolver Rivet_iLib )
 
diff --git a/Generators/Rivet_i/Rivet_i/Rivet_i.h b/Generators/Rivet_i/Rivet_i/Rivet_i.h
index 8e44605838e..c3ce4f744f9 100644
--- a/Generators/Rivet_i/Rivet_i/Rivet_i.h
+++ b/Generators/Rivet_i/Rivet_i/Rivet_i.h
@@ -105,6 +105,9 @@ private:
   /// The cross section for this run of events, set from the job properties.
   double m_crossSection;
 
+  /// The name of the weight to use in case the input HepMC contains multiple weights
+  std::string m_weightName;
+  
   /// Flag to determine whether Rivet init has already happened (in execute())
   bool m_init;
 
diff --git a/Generators/Rivet_i/cmt/requirements b/Generators/Rivet_i/cmt/requirements
index 62492c9fe35..1fe3ec4c96b 100644
--- a/Generators/Rivet_i/cmt/requirements
+++ b/Generators/Rivet_i/cmt/requirements
@@ -33,3 +33,4 @@ end_private
 ## Athena stuff
 apply_pattern named_dual_use_library library="Rivet_i" files="*.cxx"
 apply_pattern declare_joboptions files="*.py"
+
diff --git a/Generators/Rivet_i/src/Rivet_i.cxx b/Generators/Rivet_i/src/Rivet_i.cxx
index af8c93f1c71..494de74c4d6 100644
--- a/Generators/Rivet_i/src/Rivet_i.cxx
+++ b/Generators/Rivet_i/src/Rivet_i.cxx
@@ -48,6 +48,7 @@ Rivet_i::Rivet_i(const std::string& name, ISvcLocator* pSvcLocator) :
   declareProperty("McEventKey", m_genEventKey="GEN_EVENT");
   declareProperty("Analyses", m_analysisNames);
   declareProperty("CrossSection", m_crossSection=-1.0);
+  declareProperty("WeightName", m_weightName="");
   declareProperty("Stream", m_stream="/Rivet");
   declareProperty("RunName", m_runname="");
   declareProperty("HistoFile", m_file="Rivet.yoda");
@@ -187,6 +188,11 @@ StatusCode Rivet_i::execute() {
   // ATH_MSG_ALWAYS("CHK1 BEAM ENERGY = " << checkedEvent->beam_particles().first->momentum().e());
   // ATH_MSG_ALWAYS("CHK1 UNITS == MEV = " << std::boolalpha << (checkedEvent->momentum_unit() == HepMC::Units::MEV));
 
+  if(!checkedEvent) {
+    ATH_MSG_ERROR("Check on HepMC event failed!");
+    return StatusCode::FAILURE;
+  }
+
   // Initialize Rivet (on the first event only)
   if (!m_init) {
     m_analysisHandler->init(*checkedEvent);
@@ -196,8 +202,7 @@ StatusCode Rivet_i::execute() {
   // Analyse the event
   m_analysisHandler->analyze(*checkedEvent);
 
-  // Delete the modified event copy
-  if (checkedEvent != event) delete checkedEvent;
+  delete checkedEvent;
 
   return StatusCode::SUCCESS;
 }
@@ -220,18 +225,35 @@ StatusCode Rivet_i::finalize() {
       const string basename = ao->path().substr(ao->path().rfind("/")+1); // There should always be >= 1 slash
 
       // Convert YODA histos to heap-allocated ROOT objects and register
+      /// @todo Counter
       if (ao->type() == "Histo1D") {
         TH1* h = (TH1*) YODA::toTH1D(*boost::dynamic_pointer_cast<YODA::Histo1D>(ao)).Clone(basename.c_str());
         CHECK(m_histSvc->regHist(m_stream + path, h));
         ATH_MSG_INFO("TH1D " + path + " created from YODA::Histo1D");
+      } else if (ao->type() == "Histo2D") {
+        TH2* h = (TH2*) YODA::toTH2D(*boost::dynamic_pointer_cast<YODA::Histo2D>(ao)).Clone(basename.c_str());
+        CHECK(m_histSvc->regHist(m_stream + path, h));
+        ATH_MSG_INFO("TH2 " + path + " created from YODA::Histo2D");
+      // } else if (ao->type() == "Profile2D") {
+      //   TProfile2D* h = (TProfile2D*) YODA::toTProfile2D(*boost::dynamic_pointer_cast<YODA::Profile2D>(ao)).Clone(basename.c_str());
+      //   CHECK(m_histSvc->regHist(m_stream + path, h));
+      //   ATH_MSG_INFO("TProfile2D " + path + " created from YODA::Profile2D");
       } else if (ao->type() == "Profile1D") {
         TH1* h = (TH1*) YODA::toTProfile(*boost::dynamic_pointer_cast<YODA::Profile1D>(ao)).Clone(basename.c_str());
         CHECK(m_histSvc->regHist(m_stream + path, h));
         ATH_MSG_INFO("TProfile " + path + " created from YODA::Profile1D");
+      // } else if (ao->type() == "Scatter1D") {
+      //   TGraph* g = (TGraph*) YODA::toTGraph(*boost::dynamic_pointer_cast<YODA::Scatter1D>(ao)).Clone(basename.c_str());
+      //   CHECK(m_histSvc->regGraph(m_stream + path, g));
+      //   ATH_MSG_INFO("TGraph " + path + " created from YODA::Scatter2D");
       } else if (ao->type() == "Scatter2D") {
         TGraph* g = (TGraph*) YODA::toTGraph(*boost::dynamic_pointer_cast<YODA::Scatter2D>(ao)).Clone(basename.c_str());
         CHECK(m_histSvc->regGraph(m_stream + path, g));
         ATH_MSG_INFO("TGraph " + path + " created from YODA::Scatter2D");
+      // } else if (ao->type() == "Scatter3D") {
+      //   TGraph* g = (TGraph*) YODA::toTGraph(*boost::dynamic_pointer_cast<YODA::Scatter3D>(ao)).Clone(basename.c_str());
+      //   CHECK(m_histSvc->regGraph(m_stream + path, g));
+      //   ATH_MSG_INFO("TGraph " + path + " created from YODA::Scatter2D");
       } else {
         ATH_MSG_WARNING("Couldn't convert YODA histo " + path + " to ROOT: unsupported data type " + ao->type());
       }
@@ -255,6 +277,20 @@ bool cmpGenParticleByEDesc(const HepMC::GenParticle* a, const HepMC::GenParticle
 
 const HepMC::GenEvent* Rivet_i::checkEvent(const HepMC::GenEvent* event) {
   std::vector<HepMC::GenParticle*> beams;
+  HepMC::GenEvent* modEvent = new HepMC::GenEvent(*event);
+
+  if(m_weightName != ""){
+    if(event->weights().has_key(m_weightName)){
+      double weight = event->weights()[m_weightName];
+      modEvent->weights().clear();
+      modEvent->weights()[m_weightName] = weight;
+    }else{
+      ATH_MSG_ERROR("Weight named " + m_weightName + " could not be found in the HepMC event!");
+      delete modEvent;
+      return (HepMC::GenEvent*)0;
+    }
+  }
+
   if (!event->valid_beam_particles()) {
     for (HepMC::GenEvent::particle_const_iterator p = event->particles_begin(); p != event->particles_end(); ++p) {
       if (!(*p)->production_vertex() && (*p)->pdg_id() != 0) {
@@ -283,9 +319,8 @@ const HepMC::GenEvent* Rivet_i::checkEvent(const HepMC::GenEvent* event) {
   #endif
 
   if (scalefactor == 1.0 && event->valid_beam_particles()) {
-    return event;
+    return modEvent;
   } else {
-    HepMC::GenEvent* modEvent = new HepMC::GenEvent(*event);
     if (scalefactor != 1.0) {
       // ATH_MSG_ALWAYS("RESCALING * " << scalefactor);
       for (HepMC::GenEvent::particle_iterator p = modEvent->particles_begin(); p != modEvent->particles_end(); ++p) {
-- 
GitLab