Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:19:18

0001 /*******************************************************************************
0002  * Copyright (c) The JETSCAPE Collaboration, 2018
0003  *
0004  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
0005  * 
0006  * For the list of contributors see AUTHORS.
0007  *
0008  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
0009  *
0010  * or via email to bugs.jetscape@gmail.com
0011  *
0012  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
0013  * See COPYING for details.
0014  ******************************************************************************/
0015 
0016 #ifndef JETSCAPEMODULEBASE_H
0017 #define JETSCAPEMODULEBASE_H
0018 
0019 #include <string>
0020 #include <memory>
0021 #include <random>
0022 #include <map>
0023 
0024 #include "JetScapeTask.h"
0025 #include "JetScapeXML.h"
0026 #include "sigslot.h"
0027 
0028 namespace Jetscape {
0029 
0030 class JetScapeWriter;
0031 
0032 class JetScapeModuleBase
0033     : public JetScapeTask,
0034       public sigslot::has_slots<sigslot::multi_threaded_local> {
0035 
0036 public:
0037   /** Default constructor to create a JetScapeModuleBase. It sets the XML file name to a default string value.
0038   */
0039   JetScapeModuleBase();
0040 
0041   /** This is a constructor to create a JetScapeModuleBase. It sets the XML file name to "m_name" to be used to read input parameters.
0042   */
0043   JetScapeModuleBase(string m_name);
0044 
0045   /** This is a destructor for the JetScapeModuleBase.
0046    */
0047   virtual ~JetScapeModuleBase();
0048 
0049   //virtual shared_ptr<JetScapeModuleBase> Clone() const {return nullptr;}
0050   /** A virtual function for a default initialization of a JetScapeModuleBase. It also checks whether a XML file is loaded or not.
0051   */
0052   virtual void Init();
0053 
0054   /** A virtual function to define a default Exec() function for a JetScapeModuleBase. It can be overridden by different modules/tasks.
0055    */
0056   virtual void Exec(){};
0057 
0058   /**  A virtual function to define a default Clear() function for a JetScapeModuleBase. It can be overridden by different modules/tasks.
0059    */
0060   virtual void Clear(){};
0061 
0062   /** This function sets the name of the XML file to be used to store output information for the modules/tasks of a JetScapeTask.
0063    */
0064   void SetXMLMainFileName(string m_name) { xml_main_file_name = m_name; }
0065 
0066   /** This function returns the XML file name. This file contains the output data for the modules/tasks of a JetScapeTask.
0067    */
0068   string GetXMLMainFileName() { return xml_main_file_name; }
0069 
0070   /** This function sets the name of the XML file to be used to store output information for the modules/tasks of a JetScapeTask.
0071    */
0072   void SetXMLUserFileName(string m_name) { xml_user_file_name = m_name; }
0073 
0074   /** This function returns the XML file name. This file contains the output data for the modules/tasks of a JetScapeTask.
0075    */
0076   string GetXMLUserFileName() { return xml_user_file_name; }
0077 
0078   /** This function returns the current event number.
0079    */
0080   static int GetCurrentEvent() { return current_event; }
0081 
0082   /** This function increases the current event number by one.
0083    */
0084   static void IncrementCurrentEvent() { current_event++; }
0085 
0086   /** This function returns a random number based on Mersenne-Twister algorithm.
0087    */
0088   shared_ptr<std::mt19937> GetMt19937Generator();
0089 
0090   /** Helper functions for XML parsing, wrapping functionality in JetScapeXML:
0091    */
0092   tinyxml2::XMLElement *GetXMLElement(std::initializer_list<const char *> path,
0093                                       bool isRequired = true) {
0094     return JetScapeXML::Instance()->GetElement(path, isRequired);
0095   }
0096   std::string GetXMLElementText(std::initializer_list<const char *> path,
0097                                 bool isRequired = true) {
0098     return JetScapeXML::Instance()->GetElementText(path, isRequired);
0099   }
0100   int GetXMLElementInt(std::initializer_list<const char *> path,
0101                        bool isRequired = true) {
0102     return JetScapeXML::Instance()->GetElementInt(path, isRequired);
0103   }
0104   double GetXMLElementDouble(std::initializer_list<const char *> path,
0105                              bool isRequired = true) {
0106     return JetScapeXML::Instance()->GetElementDouble(path, isRequired);
0107   }
0108 
0109 private:
0110   std::string xml_main_file_name;
0111   std::string xml_user_file_name;
0112   static int current_event;
0113   shared_ptr<std::mt19937> mt19937_generator_;
0114 };
0115 
0116 /**
0117  * @class JetScapeModuleComponentFactory
0118  * @brief Factory for modules in the Jetscape framework.
0119  *
0120  * This class implements a static map (i.e. shared between all instances of JetScapeModuleBase)
0121  * consisting of a std::string of module names (i.e. name in XML config) and a function that creates
0122  * an instance of the module.
0123  * This will allow us to automatically add new modules to Jetscape without modifying the framework classes.
0124  *
0125  * Based on: https://stackoverflow.com/a/582456 and
0126  * https://github.com/alisw/AliPhysics/blob/master/PWG/EMCAL/EMCALtasks/AliEmcalCorrectionComponent.h
0127  */
0128 
0129 /// Template function for creating a new module. Used to register the module.
0130 template <typename T> shared_ptr<JetScapeModuleBase> createT() {
0131   return std::make_shared<T>();
0132 }
0133 
0134 // Factory to create and keep track of new modules
0135 class JetScapeModuleFactory {
0136 public:
0137   virtual ~JetScapeModuleFactory() {}
0138 
0139   typedef std::map<std::string, shared_ptr<JetScapeModuleBase> (*)()> map_type;
0140 
0141   /// Creates an instance of an object based on the name if the name is registered in the map.
0142   static shared_ptr<JetScapeModuleBase> createInstance(std::string const &s) {
0143     map_type::iterator it = getMap()->find(s);
0144     if (it == getMap()->end()) {
0145       return 0;
0146     }
0147     return it->second();
0148   }
0149 
0150 protected:
0151   /// Creates and access the module map
0152   static map_type *getMap() {
0153     // We never delete the map (until program termination) because we cannot guarantee correct destruction order
0154     if (!moduleMap) {
0155       moduleMap = new map_type;
0156     }
0157     return moduleMap;
0158   }
0159 
0160 private:
0161   /// Contains the map to all of the modules
0162   static map_type *moduleMap;
0163 };
0164 
0165 /**
0166  * @class RegisterJetScapeModule
0167  * @brief Registers Jetscape modules in the factory map
0168  */
0169 template <typename T>
0170 class RegisterJetScapeModule : public JetScapeModuleFactory {
0171 public:
0172   /// Registers the name of the module to map to a function that can create the module
0173   RegisterJetScapeModule(std::string const &s) {
0174     //std::cout << "Registering " << s << " to the map" << std::endl;
0175     getMap()->insert(std::make_pair(s, &createT<T>));
0176   }
0177 };
0178 
0179 } // end namespace Jetscape
0180 
0181 #endif