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 "globals.hh"
00025 #include "G4ThreeVector.hh"
00026 
00027 class BLNTupleHandler; // forward declaration
00028 
00029 
00030 /**     class BLNTuple is an interface class to an NTuple, independent of
00031  *      implementation (HistoScope, BLTrackFile, ...).
00032  *
00033  *      Each type of NTuple will derive a class from this one, and implement
00034  *      the virtual functions.
00035  **/
00036 class BLNTuple {
00037 protected:
00038         /// Constructor.
00039         BLNTuple() { }
00040         static bool enableOutput;
00041         static std::map<G4String,BLNTupleHandler*> *handler;
00042         static std::vector<BLNTuple *> list;
00043         static G4String defaultType;
00044         static BLNTupleHandler *forceHandler;
00045         friend class BLNTupleHandler;
00046 public:
00047         /// Destructor.
00048         virtual ~BLNTuple() { }
00049 
00050         /// create() will create a new NTuple for writing.
00051         /// type must be a known type of NTuple implementation ("" is default).
00052         /// "category/name" is the name of the NTuple (the "/" may be omitted 
00053         /// if category is null, depending on type).
00054         /// fields is a : separated list of the field names.
00055         /// filename is a hint, and is not used by all types.
00056         static BLNTuple *create(G4String type, G4String category, G4String name,
00057                                         G4String fields, G4String filename);
00058 
00059         /// read() will open an NTuple for reading.
00060         /// type must be a known type of NTuple implementation ("" is default).
00061         /// "category/name" is the name of the NTuple (the "/" may be omitted 
00062         /// if category is null, depending on type).
00063         /// fields is a : separated list of the field names (not used by all
00064         /// types, in which case it is up to the user to make sure the file
00065         /// fields match the required ones).
00066         /// filename is a hint, and is not used by all types.
00067         /// The NTuple should be explicitly closed by calling its close().
00068         static BLNTuple *read(G4String type, G4String category, G4String name,
00069                                         G4String fields, G4String filename);
00070 
00071         /// appendRow() adds a row to an NTuple created for writing.
00072         /// NOTE: data[] must contain n floats, and n must be the same
00073         /// as the number of fields in the NTuple.
00074         virtual void appendRow(float data[], int n) = 0;
00075 
00076         /// readRow() reads a row from an NTuple open for reading.
00077         /// returns true if valid row, false if EOF or error.
00078         /// ndata must be equal to the value of getNData(), and data[]
00079         /// must be at least that long.
00080         virtual bool readRow(float data[], int ndata) = 0;
00081 
00082         /// getNData() returns the # data items required.
00083         virtual int getNData() = 0;
00084 
00085         /// getFieldNames() returns a colon-separated list of the field names
00086         /// for each row. Returns an empty string if not supported.
00087         virtual G4String getFieldNames() { return ""; }
00088 
00089         /// annotate() will add an annotation line to any ASCII format.
00090         /// Normally line should begin with a "#", and have no "\r" or "\n".
00091         /// line == "" will output an empty line to ASCII files.
00092         /// Ignored for NTuples that don't support annotations.
00093         virtual void annotate(G4String line) { }
00094 
00095         /// needsReference() returns true if this NTuple needs the Reference
00096         /// particle.
00097         virtual bool needsReference() { return false; }
00098 
00099         /// flush() will flush the NTuple to disk, if supported.
00100         virtual void flush() = 0;
00101 
00102         /// close() will close the NTuple.
00103         virtual void close() = 0;
00104 
00105         /// do Summary() will print a 1-line summary to stdout, if supported.
00106         virtual void doSummary() = 0;
00107 
00108         /// noOutputFiles() prevents any output files from being written.
00109         static void noOutputFiles() { enableOutput = false; }
00110 
00111         /// getEnableOutput() returns enableOutput;
00112         static bool getEnableOutput() { return enableOutput; }
00113 
00114         /// registerHandler() registers a BLNTupleHandler. This defines
00115         /// a new type of NTuple that can be created. Can be called in
00116         /// static initializers without worrying about ordering.
00117         static void registerHandler(G4String typeName, BLNTupleHandler *h);
00118 
00119         /// getHandler() returns the BLNTupleHandler for a given type.
00120         static BLNTupleHandler *getHandler(G4String type);
00121 
00122         /// flushAll() writes all NTuples to file(s). This is a "checkpoint
00123         /// dump".
00124         /// Loops over all NTuples in the order they were created.
00125         /// Ignores NTuples open for reading.
00126         static void flushAll();
00127 
00128         /// closeAll() writes all NTuples to file(s) and closes them.
00129         /// Loops over all NTuples in the order they were created.
00130         /// Ignores NTuples open for reading.
00131         static void closeAll();
00132 
00133         /// summary() will summarize the NTuples to stdout
00134         /// Loops over all NTuples in the order they were created.
00135         /// Ignores NTuples open for reading.
00136         static void summary();
00137 
00138         /// update() will update real-time histogram interface(s)
00139         static void update();
00140 
00141         /// setDefaultType() sets the default type for NTuples
00142         static void setDefaultType(G4String name) { defaultType = name; }
00143 
00144         /// getFormatList() returns a list of known formats.
00145         static G4String getFormatList();
00146         
00147         /// getForceHandler() will return the forced handler (NULL if none).
00148         static BLNTupleHandler *getForceHandler() { return forceHandler; }
00149         
00150         /// setForceHandler() will force the specified handler to be used,
00151         /// regardless of type. Used to force an MPI handler when in MPI mode.
00152         static void setForceHandler(BLNTupleHandler *f) { forceHandler = f; }
00153 };
00154 
00155 /** class BLNTupleHandler defines a handler for a single type of NTuple.
00156  *  To be useful it must be registered with BLNTuple::registerHandler()
00157  *  -- this is normally done in the class default constructor, and a
00158  *  static object is defined.
00159  **/
00160 class BLNTupleHandler {
00161 protected:
00162         std::map<G4String,BLNTupleHandler*> *handler()
00163                 { return BLNTuple::handler; }
00164 public:
00165         virtual BLNTuple *create(G4String type, G4String category, 
00166                         G4String name, G4String fields, G4String filename) = 0;
00167         virtual BLNTuple *read(G4String type, G4String category, G4String name,
00168                                 G4String fields, G4String filename) = 0;
00169 };
00170 
00171 #endif // BLNTUPLE_HH
g4beamline