Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-07-01 07:05:42

0001 /**
0002  \file
0003  Declarations of file information and log-reading classes.
0004 
0005  \author    Thomas Burton
0006  \date      2011-07-29
0007  \copyright 2011 Brookhaven National Lab
0008  */
0009 
0010 #ifndef INCLUDE_EICSMEAR_ERHIC_FILE_H_
0011 #define INCLUDE_EICSMEAR_ERHIC_FILE_H_
0012 
0013 #include <iostream>
0014 #include <map>
0015 #include <string>
0016 
0017 #include <TObject.h>
0018 #include <TObjString.h>
0019 #include <TString.h>
0020 
0021 #include "eicsmear/erhic/EventBase.h"
0022 #include "eicsmear/erhic/EventHepMC.h"
0023 #include "eicsmear/erhic/EventMilou.h"
0024 #include "eicsmear/erhic/EventPythia.h"
0025 #include "eicsmear/erhic/EventFactory.h"
0026 
0027 namespace erhic {
0028 
0029 /**
0030  Base class for log file processors.
0031 
0032  Reads a log file from a Monte Carlo generator and extracts information.
0033  Inherited classes for each generator type implement the Extract()
0034  method to gather the required information for that generator and the
0035  Save() method to store it to file.
0036  */
0037 class LogReader : public TObject {
0038  public:
0039   /**
0040    Constructor.
0041    */
0042   LogReader() { }
0043 
0044   /**
0045    Destructor.
0046    */
0047   virtual ~LogReader() { }
0048 
0049   /**
0050    Return a new LogReader instance.
0051    */
0052   virtual LogReader* Create() const = 0;
0053 
0054   /**
0055    Extract data from the named log file.
0056    */
0057   virtual bool Extract(const std::string& file) = 0;
0058 
0059   /**
0060    Saves the extracted data to the current file, if one is open and is
0061    writeable.
0062    Returns -1 if the data cannot be saved.
0063    To write the LogReader itself, use LogReader::Write().
0064    */
0065   virtual Int_t Save() const = 0;
0066 
0067   ClassDef(erhic::LogReader, 1)
0068 };
0069 
0070 /**
0071  Processes PYTHIA log files.
0072 
0073  Reads a log file and finds the total cross section and the number
0074  of generated events when Extract() is called.
0075  Writes those values as TObjStrings to the current directory when
0076  Save() is called, assuming that directory is writeable.
0077  */
0078 class LogReaderPythia : public LogReader {
0079  public:
0080   /**
0081    Constructor.
0082    */
0083   LogReaderPythia();
0084 
0085   /**
0086    Destructor.
0087    */
0088   virtual ~LogReaderPythia();
0089 
0090   /**
0091    Return a new LogReaderPythia instance.
0092    */
0093   LogReaderPythia* Create() const;
0094 
0095   /**
0096    Extract data from the named log file.
0097    */
0098   bool Extract(const std::string& file);
0099 
0100   /**
0101    Write the extracted information to the current file, if it is
0102    writeable. If you want to write the LogReaderPythia itself, use
0103    LogReaderPythia::Write().
0104    */
0105   Int_t Save() const;
0106 
0107  protected:
0108   TObjString crossSection_;  ///> Total cross section in microbarns
0109   TObjString nEvents_;  ///> Total number of events generated and kept
0110   TObjString nTrials_;  ///> Total number of events generated
0111 
0112   ClassDef(erhic::LogReaderPythia, 2)
0113 };
0114 
0115 inline LogReaderPythia* LogReaderPythia::Create() const {
0116   return new LogReaderPythia;
0117 }
0118 
0119 /**
0120  Processes PEPSI log files.
0121 
0122  Reads a log file and finds the total cross section and the number
0123  of generated events when Extract() is called.
0124  Writes those values as TObjStrings to the current directory when
0125  Save() is called, assuming that directory is writeable.
0126  */
0127 class LogReaderPepsi : public LogReader {
0128  public:
0129   /**
0130    Constructor.
0131    */
0132   LogReaderPepsi();
0133 
0134   /**
0135    Destructor.
0136    */
0137   virtual ~LogReaderPepsi();
0138 
0139   /**
0140    Return a new LogReaderPepsi instance.
0141    */
0142   LogReaderPepsi* Create() const;
0143 
0144   /**
0145    Extract data from the named log file.
0146    */
0147   bool Extract(const std::string& file);
0148 
0149   /**
0150    Write the extracted information to the current file, if it is
0151    writeable. If you want to write the LogReaderPepsi itself, use
0152    LogReaderPepsi::Write().
0153    */
0154   Int_t Save() const;
0155 
0156  protected:
0157   TObjString crossSection_;  ///> Total cross section in microbarns
0158   TObjString nEvents_;  ///> Total number of events generated and kept
0159   TObjString nTrials_;  ///> Total number of events generated
0160 
0161   ClassDef(erhic::LogReaderPepsi, 2)
0162 };
0163 
0164 inline LogReaderPepsi* LogReaderPepsi::Create() const {
0165   return new LogReaderPepsi;
0166 }
0167 
0168 /**
0169  Processes DJANGOH log files.
0170 
0171  Reads a log file and finds the total cross section and the number
0172  of generated events when Extract() is called.
0173  Writes those values as TObjStrings to the current directory when
0174  Save() is called, assuming that directory is writeable.
0175  */
0176 class LogReaderDjangoh : public LogReader {
0177  public:
0178   /**
0179    Constructor.
0180    */
0181   LogReaderDjangoh();
0182 
0183   /**
0184    Destructor.
0185    */
0186   virtual ~LogReaderDjangoh();
0187 
0188   /**
0189    Return a new LogReaderDjangoh instance.
0190    */
0191   LogReaderDjangoh* Create() const;
0192 
0193   /**
0194    Extract data from the named log file.
0195    */
0196   bool Extract(const std::string& file);
0197 
0198   /**
0199    Write the extracted information to the current file, if it is
0200    writeable. If you want to write the LogReaderDjangoh itself, use
0201    LogReaderDjangoh::Write().
0202    */
0203   Int_t Save() const;
0204 
0205  protected:
0206   TObjString crossSection_;  ///> Total cross section in microbarns
0207   TObjString nEvents_;  ///> Total number of events generated and kept
0208   TObjString nTrials_;  ///> Total number of events generated
0209 
0210   ClassDef(erhic::LogReaderDjangoh, 2)
0211 };
0212 
0213 inline LogReaderDjangoh* LogReaderDjangoh::Create() const {
0214   return new LogReaderDjangoh;
0215 }
0216 
0217 /**
0218  Processes PYTHIA log files.
0219 
0220  Reads a log file and finds the total cross section and the number
0221  of generated events when Extract() is called.
0222  Writes those values as TObjStrings to the current directory when
0223  Save() is called, assuming that directory is writeable.
0224  */
0225 class LogReaderMilou : public LogReader {
0226  public:
0227   /**
0228    Constructor.
0229    */
0230   LogReaderMilou() { }
0231 
0232   /**
0233    Destructor.
0234    */
0235   virtual ~LogReaderMilou() { }
0236 
0237   /**
0238    Return a new LogReaderMilou instance.
0239    */
0240   LogReaderMilou* Create() const;
0241 
0242   /**
0243    Extract data from the named log file.
0244    */
0245   bool Extract(const std::string& file);
0246 
0247   /**
0248    Write the extracted information to the current file, if it is
0249    writeable. If you want to write the LogReaderMilou itself, use
0250    LogReaderMilou::Write().
0251    */
0252   Int_t Save() const;
0253 
0254   /**
0255    Returns the number of events reported by the log file.
0256    Extract() should be called first.
0257    */
0258   Int_t GetNEvents() const;
0259 
0260   /**
0261    Returns the total cross section reported by the log file.
0262    Extract() should be called first.
0263    */
0264   Double_t GetCrossSection() const;
0265 
0266   /**
0267    Returns the error on total cross section reported by the log file.
0268    Extract() should be called first.
0269    */
0270   Double_t GetCrossSectionError() const;
0271 
0272  protected:
0273   TObjString crossSection_;  ///> Total cross section in nb
0274   TObjString crossSectionError_;  ///> Cross section error in nb
0275   TObjString nEvents_;  ///> Total number of events generated
0276 
0277   ClassDef(erhic::LogReaderMilou, 1)
0278 };
0279 
0280 inline LogReaderMilou* LogReaderMilou::Create() const {
0281   return new LogReaderMilou;
0282 }
0283 
0284 inline Int_t LogReaderMilou::GetNEvents() const {
0285   return nEvents_.GetString().Atoi();
0286 }
0287 
0288 inline Double_t LogReaderMilou::GetCrossSection() const {
0289   return crossSection_.GetString().Atof();
0290 }
0291 
0292 /**
0293  Returns the error on total cross section reported by the log file.
0294  Extract() should be called first.
0295  */
0296 inline Double_t LogReaderMilou::GetCrossSectionError() const {
0297   return crossSectionError_.GetString().Atof();
0298 }
0299 
0300 /**
0301  Processes gmc_trans log files.
0302 
0303  Reads a log file and finds the total cross section and the number
0304  of generated events.
0305  */
0306 class LogReaderGmcTrans : public LogReader {
0307  public:
0308   /**
0309    Constructor.
0310    */
0311   LogReaderGmcTrans();
0312 
0313   /**
0314    Destructor.
0315    */
0316   virtual ~LogReaderGmcTrans();
0317 
0318   /**
0319    Return a new LogReaderGmcTrans instance.
0320    */
0321   LogReaderGmcTrans* Create() const;
0322 
0323   /**
0324    Search the named file.
0325    Store the total cross section and number of generated events
0326    if they can be found.
0327    */
0328   bool Extract(const std::string& filename);
0329 
0330   /**
0331    Write the stored cross section and number of events to
0332    the active ROOT directory.
0333    Returns the total number of bytes written, or a value <= 0
0334    in the case of an error.
0335    */
0336   Int_t Save() const;
0337 
0338   /**
0339    Returns the number of events reported by the log file.
0340    Extract() should be called first.
0341    */
0342   Int_t GetNEvents() const;
0343 
0344   /**
0345    Returns the total cross section reported by the log file.
0346    Extract() should be called first.
0347    */
0348   Double_t GetCrossSection() const;
0349 
0350  protected:
0351   TObjString mNEvents;  ///< Number of generated events.
0352   TObjString mCrossSection;  ///< Total cross section in microbarns.
0353 
0354   ClassDef(erhic::LogReaderGmcTrans, 1)
0355 };
0356 
0357 /**
0358  Factory class for LogReaders.
0359 
0360  Singleton class.
0361  Creates a LogReader instance corresponding to a Monte Carlo
0362  generator type.
0363  */
0364 class LogReaderFactory {
0365  public:
0366   /**
0367    Returns the single instance of LogReaderFactory.
0368    */
0369   static LogReaderFactory& GetInstance();
0370 
0371   /**
0372    Returns a LogReader instance of the type for reading log files
0373    from the Monte Carlo generator event type 'event'.
0374    Returns NULL in the case of an unsupported generator.
0375    The LogReader must be deleted by the user.
0376    */
0377   LogReader* CreateReader(const EventBase& event) const;
0378 
0379   /**
0380    Returns a LogReader instance of the type for reading log files
0381    from the Monte Carlo generator named 'name'.
0382    Returns NULL in the case of an unsupported generator.
0383    The LogReader must be deleted by the user.
0384    */
0385   LogReader* CreateReader(const std::string& name) const;
0386 
0387   /**
0388    Returns a LogReader instance of the type for reading log files
0389    from the Monte Carlo generator which produced the content in an
0390    istream, by reading the first line of that stream.
0391    Returns NULL in the case of an unsupported generator.
0392    The LogReader must be deleted by the user.
0393    */
0394   LogReader* CreateReader(std::istream&) const;
0395 
0396   /**
0397    Attempts to locate a log file corresponding to the named Monte
0398    Carlo file.
0399    Searches for a file with the same base name and extension '.log'.
0400    Looks in
0401    the current directory and, if mcFile gives a path containing
0402    'TXTFILES', in the corresonding directory substituting 'LOGFILES'.
0403    */
0404   std::string Locate(const std::string& mcFile) const;
0405 
0406  protected:
0407   /**
0408    Constructor.
0409    */
0410   LogReaderFactory();
0411 
0412   /**
0413    Destructor.
0414    */
0415   ~LogReaderFactory();
0416 
0417   typedef std::map<std::string, LogReader*> Map;
0418   Map prototypes_;
0419 
0420   ClassDef(erhic::LogReaderFactory, 1)
0421 };
0422 
0423 /**
0424  Abstract base class for Monte Carlo file types.
0425  Describes a Monte Carlo file type and returns objects required for
0426  processing or analysis of that file type.
0427  */
0428 class FileType : public TObject {
0429  public:
0430   /**
0431    Destructor.
0432    */
0433   virtual ~FileType() { }
0434 
0435   /**
0436    Returns a new FileType instance.
0437    */
0438   virtual FileType* Create() const = 0;
0439 
0440   /**
0441    Returns a new event object for the generator making this type of file.
0442    */
0443   virtual EventBase* AllocateEvent() const = 0;
0444 
0445   /**
0446    Returns the name of the generator.
0447       Should be entirely in lower case.
0448    */
0449   virtual std::string GetGeneratorName() const;
0450 
0451   /**
0452    Sets the name of the generator.
0453    Should be entirely in lower case.
0454    */
0455   virtual void SetGeneratorName(const std::string newname="");
0456   
0457   /**
0458    Returns a reader to process the log file corresponding to this type of file.
0459    */
0460   virtual LogReader* CreateLogReader() const = 0;
0461 
0462   /**
0463    Returns a new event object for the generator making this type of file.
0464    */
0465   virtual VirtualEventFactory* CreateEventFactory(std::istream&) const = 0;
0466 
0467  protected:
0468     std::string generatorname;
0469   
0470   ClassDef(erhic::FileType, 2)
0471 };
0472 
0473 /*
0474  Templated file descriptor class, valid for Monte Carlo event classes.
0475  e.g. File<EventPythia> describes a Pythia event file.
0476  */
0477 template<typename T>
0478 class File : public FileType {
0479  public:
0480   /**
0481    Constructor.
0482 
0483    If the string argument is not empty, the File attempts to open
0484    a file with that name. If the file is opened it tries to extract
0485    */
0486   File();
0487 
0488   /**
0489    Destructor.
0490    */
0491   virtual ~File();
0492 
0493   /**
0494    Returns a new File object.
0495    */
0496   virtual File<T>* Create() const;
0497 
0498   /**
0499    Allocates an event of the type for this file.
0500    */
0501   virtual T* AllocateEvent() const;
0502   
0503   /**
0504    Create a LogReader for this type of Monte Carlo file.
0505    Returns NULL if the file type is unsupported or has no LogReader
0506    class implemented.
0507    The LogReader must be deleted by the user.
0508    */
0509   virtual LogReader* CreateLogReader() const;
0510 
0511   /**
0512    Returns a new event factory instance.
0513    */
0514   virtual EventFromAsciiFactory<T>*
0515     CreateEventFactory(std::istream& is) const {
0516      return new EventFromAsciiFactory<T>(is);
0517   }
0518 
0519  protected:
0520   T* t_;
0521 
0522   // Warning: explicitly putting the erhic:: namespace before the class
0523   // name doesn't seen to work for template classes.
0524   ClassDef(File, 2)
0525 };
0526 
0527 template<>
0528 class File<erhic::EventHepMC> : public FileType {
0529  public:
0530 
0531   /**
0532    Constructor.
0533 
0534    If the string argument is not empty, the File attempts to open
0535    a file with that name. If the file is opened it tries to extract
0536    */
0537   File(){}
0538 
0539   /**
0540    Destructor.
0541    */
0542   virtual ~File(){}
0543 
0544   /**
0545    Returns a new File object.
0546    */
0547   virtual File<erhic::EventHepMC>* Create() const {
0548     return new File<erhic::EventHepMC>();
0549   }
0550   
0551   /**
0552      Allocates an event of the type for this file.
0553    */
0554   virtual erhic::EventHepMC* AllocateEvent() const {
0555     return new erhic::EventHepMC();
0556   }
0557 
0558   /**
0559    Create a LogReader for this type of Monte Carlo file.
0560    Returns NULL if the file type is unsupported or has no LogReader
0561    class implemented.
0562    The LogReader must be deleted by the user.
0563    */
0564   virtual LogReader* CreateLogReader() const {return nullptr;}
0565 
0566   EventFromAsciiFactory<erhic::EventHepMC>* CreateEventFactory(std::istream& is) const {
0567     return new EventFromAsciiFactory<erhic::EventHepMC>(is);}
0568  protected:
0569   erhic::EventHepMC* t_;
0570 
0571   ClassDef(File<erhic::EventHepMC>, 1)
0572 };
0573 
0574 template<typename T>
0575 inline T* File<T>::AllocateEvent() const {
0576   return new T;
0577 }
0578 
0579 template<typename T>
0580 inline File<T>* File<T>::Create() const {
0581   return new File<T>();
0582 }
0583 
0584 
0585 /**
0586  Factory class for Files.
0587  Singleton class.
0588  */
0589 class FileFactory {
0590  public:
0591   /**
0592    Returns the single instance of FileFactory.
0593    */
0594   static FileFactory& GetInstance();
0595 
0596   /**
0597    Returns a FileType object for the named generator.
0598    */
0599   const FileType* GetFile(const std::string& generatorName) const;
0600 
0601   /**
0602    Returns a FileType object, determining the generator type from a stream.
0603    The isp may need to be updated for hepmc, that's why it's passed by reference.
0604    */
0605   const FileType* GetFile(std::shared_ptr<std::istream>& isp, const std::string fileName="") const;
0606 
0607  protected:
0608   /**
0609    Constructor.
0610    */
0611   FileFactory();
0612 
0613   /**
0614    Destructor.
0615    */
0616   virtual ~FileFactory();
0617 
0618   typedef std::map<std::string, FileType*> Map;
0619   Map prototypes_;
0620 };
0621 
0622 }  // namespace erhic
0623 
0624 #endif  // INCLUDE_EICSMEAR_ERHIC_FILE_H_