Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /**
0002  \file
0003  Declaration of class erhic::Forester.
0004 
0005  \author    Thomas Burton
0006  \date      2011-06-23
0007  \copyright 2011 Brookhaven National Lab
0008 */
0009 
0010 #ifndef INCLUDE_EICSMEAR_ERHIC_FORESTER_H_
0011 #define INCLUDE_EICSMEAR_ERHIC_FORESTER_H_
0012 
0013 // C(++) headers
0014 #include <cmath>
0015 #include <ctime>
0016 #include <fstream>
0017 #include <iomanip>
0018 #include <iostream>
0019 #include <string>
0020 
0021 // ROOT headers
0022 #include <Rtypes.h>
0023 #include <TFile.h>
0024 #include <TStopwatch.h>
0025 #include <TTree.h>
0026 
0027 // Other headers
0028 #include "eicsmear/erhic/EventMC.h"
0029 
0030 namespace erhic {
0031 
0032 class FileType;
0033 class VirtualEventFactory;
0034 
0035 
0036 /**
0037  Manages the creation of trees from plain-text Monte Carlo files.
0038  Bad pun, I know, but the ROOT guys started it.
0039  */
0040 class Forester : public TObject {
0041  public:
0042   /**
0043    Default constructor.
0044    */
0045   Forester();
0046 
0047   /**
0048    Destructor.
0049    */
0050   virtual ~Forester();
0051 
0052   /**
0053    Processes a text file into a ROOT file.
0054    Returns the number of events processed.
0055    */
0056   Long64_t Plant();
0057 
0058   /**
0059    Sets the name of the input text file containing Monte Carlo data.
0060    */
0061   void SetInputFileName(const std::string&);
0062 
0063   /**
0064    Sets the name of the ROOT tree file to create.
0065    */
0066   void SetOutputFileName(const std::string&);
0067 
0068   /**
0069    Sets the name of the TTree to write to the file named by
0070    SetOutputFileName().
0071    */
0072   void SetTreeName(const std::string& = "EICTree");
0073 
0074   /**
0075    Sets the name of the TBranch containing event objects.
0076    This is the only branch written to the TTree named by SetTreeName().
0077    */
0078   void SetBranchName(const std::string& = "event");
0079 
0080   /**
0081    Returns the name of the input text file containing Monte Carlo data.
0082    */
0083   std::string GetInputFileName() const;
0084 
0085   /**
0086    Returns the name of the ROOT tree file to create.
0087    */
0088   std::string GetOutputFileName() const;
0089 
0090   /**
0091    Returns the name of the TTree to write to the file named by
0092    SetOutputFileName().
0093    */
0094   std::string GetTreeName() const;
0095 
0096   /**
0097    Returns the name of the TBranch containing event objects.
0098    */
0099   std::string GetBranchName() const;
0100 
0101   /**
0102    Sets the maximum number of events to process. Processing will terminate
0103    when this number of events or the end of the input file is reached,
0104    whichever occurs first. A value <= 0 indicates to process all events in the
0105    input file (this is the default).
0106    */
0107   void SetMaxNEvents(Long64_t = 0);
0108 
0109   /**
0110    Returns the maximum number of events to process.
0111    */
0112   Long64_t GetMaxNEvents() const;
0113 
0114   /**
0115    Sets the event count interval at which to print a status message.
0116    A value <= 0 suppresses messages.
0117    */
0118   void SetMessageInterval(Long64_t = 10000);
0119 
0120   /**
0121    Prints the current configuration to the requested output stream.
0122    */
0123   void Print(std::ostream& stream) const;
0124 
0125   /**
0126    \overload
0127 
0128    To avoid hiding TObject::Print()
0129    */
0130   void Print(Option_t* = "not used") const;
0131 
0132   /**
0133    If set to true, prints messages during running.
0134    If set to false, runs silently except in the case of critical errors.
0135    */
0136   void SetBeVerbose(bool = false);
0137 
0138   /**
0139    Returns the verbosity i.e. whether to print status messages.
0140    */
0141   bool BeVerbose() const;
0142 
0143   /**
0144    Returns the file type information for the last processed file.
0145    Returns NULL if no input has been processed.
0146    Do not delete the returned object.
0147    */
0148   const erhic::FileType* GetFileType() const;
0149 
0150   /**
0151    Stores summary information about the last call to Forester::Plant().
0152    KK: Made public for rootcint
0153    */
0154   class Status{
0155   public:
0156     Status();
0157     virtual ~Status();
0158     virtual std::ostream& Print(std::ostream& os = std::cout) const;
0159 
0160   protected:
0161     virtual void StartTimer();
0162     virtual void StopTimer();
0163     virtual void ModifyEventCount(Long64_t count);
0164     virtual void ModifyParticleCount(Long64_t count);
0165 
0166     time_t mStartTime;
0167     time_t mEndTime;
0168     Long64_t mNEvents;
0169     Long64_t mNParticles;
0170 
0171     // The TStopwatch is mutable as "GetRealTime()" is non-const.
0172     mutable TStopwatch mTimer;
0173 
0174     friend class Forester;
0175 
0176     ClassDef(Status, 1);
0177   };
0178 
0179  protected:
0180   /**
0181    Prints a summary of the last call to Plant()
0182    to the requested output stream.
0183    */
0184   const Status& GetGetStatus() const {
0185     return mStatus;
0186   }
0187 
0188   /**
0189    Opens the input file and checks that it was produced by a
0190    supported Monte Carlo generator.
0191    Returns true upon success or false upon and I/O error or
0192    if the Monte Carlo generator is unsupported or cannot be determined.
0193    */
0194   bool OpenInput();
0195 
0196   /**
0197    Opens the output ROOT file and creates the TTree ready for filling.
0198    */
0199   bool SetupOutput();
0200 
0201   /**
0202    Writes output and takes end-of-file actions.
0203    */
0204   void Finish();
0205 
0206   /**
0207    Allocate an event buffer for the TTree based on the generator type.
0208    Returns false if the generator has not yet been successfully
0209    determined.
0210    */
0211   bool AllocateEvent();
0212 
0213   /**
0214    Aligns the input text file on the first line of the first event.
0215    After a successful call, mLine stores the first line of the event
0216    and true is returned.
0217    If unsuccessful, mLine is blank and false is returned.
0218    */
0219   bool FindFirstEvent();
0220 
0221   /** Prints the status of the current Plant() call to the standard output. */
0222   void PrintStatus() const;
0223 
0224   /**
0225    Prints the quit flag status. A return value of true indicates
0226    that the input file has ended or the mMaxNEvents has been reached.
0227    Processing the file will
0228    quit after the end of the next call to FinishEvent().
0229    */
0230   bool MustQuit() const;
0231 
0232   /** Set the quit flag. */
0233   void SetMustQuit(bool quit);
0234 
0235   // Member variables.
0236   // Those with comment //! will be treated as transient members by ROOT
0237   // and won't be written to a file.
0238   // IMPORTANT: There MUST be a space after //!
0239 
0240   Bool_t mQuit;  ///< Quit status. Set to true once EoF or max events reached
0241   Bool_t mVerbose;  ///< Verbosity flag
0242   TTree* mTree;  //! < Output TTree, owned by mRootFile
0243   VirtualEvent* mEvent;  //! < Stores event branch address
0244   const erhic::FileType* mFile;  //! < File type information
0245   TFile* mRootFile;  //! < Pointer to output ROOT file
0246   Long64_t mMaxNEvents;  ///< Maximum number of events to process
0247   Long64_t mInterval;  ///< Event interval between printing status messages
0248 
0249   std::shared_ptr<std::istream> mTextFile;  //! < Input text file
0250   std::string mInputName;  ///< Name of the input text file
0251   std::string mOutputName;  ///< Name of the output ROOT file
0252   std::string mTreeName;  ///< Name of the output TTree
0253   std::string mBranchName;  ///< Name of the event TBranch
0254   std::string mLine;  ///< Stores the latest text line read from the input file
0255   Status mStatus;  ///< Forester status information
0256   VirtualEventFactory* mFactory;  //! < Pointer to the event-builder object
0257 
0258   ClassDef(Forester, 3)
0259 };
0260 
0261 inline void Forester::SetInputFileName(const std::string& name) {
0262   mInputName = name;
0263 }
0264 
0265 inline void Forester::SetOutputFileName(const std::string& name) {
0266   mOutputName = name;
0267 }
0268 
0269 inline void Forester::SetTreeName(const std::string& name) {
0270   mTreeName = name;
0271 }
0272 
0273 inline void Forester::SetBranchName(const std::string& name) {
0274   mBranchName = name;
0275 }
0276 
0277 inline std::string Forester::GetInputFileName() const {
0278   return mInputName;
0279 }
0280 
0281 inline std::string Forester::GetOutputFileName() const {
0282   return mOutputName;
0283 }
0284 
0285 inline std::string Forester::GetTreeName() const {
0286   return mTreeName;
0287 }
0288 
0289 inline std::string Forester::GetBranchName() const {
0290   return mBranchName;
0291 }
0292 
0293 inline void Forester::SetMaxNEvents(Long64_t number) {
0294   mMaxNEvents = number;
0295 }
0296 
0297 inline Long64_t Forester::GetMaxNEvents() const {
0298   return mMaxNEvents;
0299 }
0300 
0301 inline void Forester::SetMessageInterval(Long64_t number) {
0302   mInterval = number;
0303 }
0304 
0305 inline bool Forester::MustQuit() const {
0306   return mQuit;
0307 }
0308 
0309 inline void Forester::SetMustQuit(bool flag) {
0310   mQuit = flag;
0311 }
0312 
0313 inline void Forester::SetBeVerbose(bool flag) {
0314   mVerbose = flag;
0315 }
0316 
0317 inline bool Forester::BeVerbose() const {
0318   return mVerbose;
0319 }
0320 
0321 inline const FileType* Forester::GetFileType() const {
0322   return mFile;
0323 }
0324 
0325 }  // namesce erhic
0326 
0327 #endif  // INCLUDE_EICSMEAR_ERHIC_FORESTER_H_