BLNTuple.hh

Go to the documentation of this file.
00001 //      BLNTuple.hh
00002 /*
00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com
00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved.
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 http://www.gnu.org/copyleft/gpl.html
00017 */
00018 
00019 #ifndef BLNTUPLE_HH
00020 #define BLNTUPLE_HH
00021 
00022 #include <map>
00023 #include <vector>
00024 #include <set>
00025 #include "globals.hh"
00026 #include "G4ThreeVector.hh"
00027 
00028 class BLNTupleHandler; // forward declarations
00029 class BLNTupleCallback;
00030 
00031 /**     class BLNTuple is an interface class to an NTuple, independent of
00032  *      implementation (HistoScope, BLTrackFile, Root, ...).
00033  *
00034  *      Each type of NTuple will derive a class from this one, and implement
00035  *      the virtual functions.
00036  **/
00037 class BLNTuple {
00038 protected:
00039         G4String name;
00040         G4String fields;
00041         int nData;
00042         std::vector<BLNTupleCallback*> callback;
00043         /// Constructor.
00044         BLNTuple(G4String _name, G4String _fields) :
00045                 name(_name), fields(_fields), callback() { nData = 0; }
00046         static bool enableOutput;
00047         static std::map<G4String,BLNTupleHandler*> *handler;
00048         static std::vector<BLNTuple *> list;
00049         static std::set<G4String> nameSet;
00050         static G4String defaultType;
00051         static BLNTupleHandler *forceHandler;
00052         friend class BLNTupleHandler;
00053 public:
00054         /// Destructor.
00055         virtual ~BLNTuple() { callback.clear(); }
00056 
00057         /// create() will create a new NTuple for writing.
00058         /// type must be a known type of NTuple implementation ("" => default).
00059         /// "category/name" is the name of the NTuple (the "/" may be omitted 
00060         /// if category is null, depending on type).
00061         /// fields is a : separated list of the field names.
00062         /// filename is a hint, and is not used by all types.
00063         static BLNTuple *create(G4String type, G4String category, G4String name,
00064                                         G4String fields, G4String filename);
00065 
00066         /// read() will open an NTuple for reading.
00067         /// type must be a known type of NTuple implementation ("" is default).
00068         /// "category/name" is the name of the NTuple (the "/" may be omitted 
00069         /// if category is null, depending on type).
00070         /// fields is a : separated list of the field names (not used by all
00071         /// types, in which case it is up to the user to make sure the file
00072         /// fields match the required ones).
00073         /// filename is a hint, and is not used by all types.
00074         /// The NTuple should be explicitly closed by calling its close().
00075         static BLNTuple *read(G4String type, G4String category, G4String name,
00076                                         G4String fields, G4String filename);
00077 
00078         /// appendRow() adds a row to an NTuple created for writing.
00079         /// NOTE: data[] must contain n doubles, and n must be the same
00080         /// as the number of fields in the NTuple.
00081         virtual void appendRow(double data[], int n) = 0;
00082 
00083         /// readRow() reads a row from an NTuple open for reading.
00084         /// returns true if valid row, false if EOF or error.
00085         /// ndata must be equal to the value of getNData(), and data[]
00086         /// must be at least that long.
00087         virtual bool readRow(double data[], int ndata) = 0;
00088 
00089         /// getName() returns the name of the NTuple (not including category)
00090         G4String getName() const { return name; }
00091 
00092         /// getFields() returns a colon-separated list of the fields.
00093         G4String getFields() const { return fields; }
00094 
00095         /// getNData() returns the # data items required.
00096         int getNData() const { return nData; }
00097 
00098         /// doCallbacks() will call all registered callbacks
00099         void doCallbacks(double data[], int ndata);
00100 
00101         /// annotate() will add an annotation line to any ASCII format.
00102         /// Normally line should begin with a "#", and have no "\r" or "\n".
00103         /// line == "" will output an empty line to ASCII files.
00104         /// Ignored for NTuples that don't support annotations.
00105         virtual void annotate(G4String line) { }
00106 
00107         /// needsReference() returns true if this NTuple needs the Reference
00108         /// particle.
00109         virtual bool needsReference() { return false; }
00110 
00111         /// flush() will flush the NTuple to disk, if supported.
00112         virtual void flush() = 0;
00113 
00114         /// close() will close the NTuple.
00115         virtual void close() = 0;
00116 
00117         /// do Summary() will print a 1-line summary to stdout, if supported.
00118         virtual void doSummary() = 0;
00119 
00120         /// registerCallback() registers a callback with this BLNTuple.
00121         void registerCallback(BLNTupleCallback *cb) { callback.push_back(cb); }
00122 
00123         /// getList() returns the list of all BLNTuples.
00124         static std::vector<BLNTuple*> getList() { return list; }
00125 
00126         /// isUniqueName() returns true if the name is unique. Also enters
00127         /// the name into the nameSet.
00128         static bool isUniqueName(G4String name);
00129 
00130         /// disableOutputFiles() prevents any output files from being written.
00131         static void disableOutputFiles() { enableOutput = false; }
00132 
00133         /// getEnableOutput() returns enableOutput;
00134         static bool getEnableOutput() { return enableOutput; }
00135 
00136         /// registerHandler() registers a BLNTupleHandler. This defines
00137         /// a new type of NTuple that can be created. Can be called in
00138         /// static initializers without worrying about ordering.
00139         static void registerHandler(G4String typeName, BLNTupleHandler *h);
00140 
00141         /// getHandler() returns the BLNTupleHandler for a given type.
00142         /// returns 0 for unknown type.
00143         static BLNTupleHandler *getHandler(G4String type);
00144 
00145         /// flushAll() writes all NTuples to file(s). This is a "checkpoint
00146         /// dump".
00147         /// Loops over all NTuples in the order they were created.
00148         /// Ignores NTuples open for reading.
00149         static void flushAll();
00150 
00151         /// closeAll() writes all NTuples to file(s) and closes them.
00152         /// Loops over all NTuples in the order they were created.
00153         /// Ignores NTuples open for reading.
00154         static void closeAll();
00155 
00156         /// summary() will summarize the NTuples to stdout
00157         /// Loops over all NTuples in the order they were created.
00158         /// Ignores NTuples open for reading.
00159         static void summary();
00160 
00161         /// update() will update real-time histogram interface(s)
00162         static void update();
00163 
00164         /// setDefaultType() sets the default type for NTuples
00165         static void setDefaultType(G4String name) { defaultType = name; }
00166 
00167         /// getFormatList() returns a list of known formats.
00168         static G4String getFormatList();
00169         
00170         /// getForceHandler() will return the forced handler (NULL if none).
00171         static BLNTupleHandler *getForceHandler() { return forceHandler; }
00172         
00173         /// setForceHandler() will force the specified handler to be used,
00174         /// regardless of type. Used to force an MPI handler when in MPI mode.
00175         static void setForceHandler(BLNTupleHandler *f) { forceHandler = f; }
00176 };
00177 
00178 /** class BLNTupleHandler defines a handler for a single type of NTuple.
00179  *  To be useful it must be registered with BLNTuple::registerHandler()
00180  *  -- this is normally done in the class default constructor, and a
00181  *  static object is defined.
00182  **/
00183 class BLNTupleHandler {
00184 protected:
00185         std::map<G4String,BLNTupleHandler*> *handler()
00186                 { return BLNTuple::handler; }
00187 public:
00188         virtual BLNTuple *create(G4String type, G4String category, 
00189                         G4String name, G4String fields, G4String filename) = 0;
00190         virtual BLNTuple *read(G4String type, G4String category, G4String name,
00191                                 G4String fields, G4String filename) = 0;
00192 };
00193 
00194 /** class BLNTupleCallback defines a callback for BLNTuple::appendRow().
00195  **/
00196 class BLNTupleCallback {
00197 public:
00198         /// callback() is called by BLNTuple::appendRow() for every registered
00199         /// callback. The first argument is always this, identifying the
00200         /// BLNTuple that is doing the calling.
00201         virtual void ntupleCallback(BLNTuple *ntuple, double data[], int ndata)
00202                                                                         = 0;
00203 };
00204 
00205 #endif // BLNTUPLE_HH
g4beamline