]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWGLF/FORWARD/analysis2/AliOADBForward.h
Mega commit of many changes to PWGLFforward
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliOADBForward.h
diff --git a/PWGLF/FORWARD/analysis2/AliOADBForward.h b/PWGLF/FORWARD/analysis2/AliOADBForward.h
new file mode 100644 (file)
index 0000000..30cceb2
--- /dev/null
@@ -0,0 +1,614 @@
+// -*- mode: C++ -*-
+#ifndef ALIOADBFORWARD_H
+#define ALIOADBFORWARD_H
+#include <TNamed.h>
+#include <TString.h>
+#include <TMap.h>
+class TFile;
+class TTree;
+class TBrowser;
+class TList;
+
+/**
+ * Container/handler of Forward calibration objects. 
+ */
+class AliOADBForward : public TObject
+{
+public:
+  //=== Options ======================================================
+  /**
+   * Options for selecting the entries according to the run number
+   * given in the query. 
+   */
+  enum ERunSelectMode { 
+    kDefault = 0,
+    /** 
+     * Select only entries from exactly the run given. 
+     */
+    kExact = 1,
+    /** 
+     * Select the entry with the largest run number 
+     */
+    kNewest = 2,
+    /**
+     * Select entries from the run nearest (possibly constrained to be
+     * older or newer) to the run given in the query 
+     */ 
+    kNear = 3, 
+    /** 
+     * Select entries from runs that are older than the run given in
+     * the query. The oldest entry with the largest run number
+     * smaller than the given run is selected.  
+     */
+    kOlder = 4, 
+    /**
+     * Select entries from runs that are newer than the run given.
+     * Select the entries with the smallest run number which is larger
+     * than the given run.
+     */
+    kNewer = 5
+  };
+  static const char* Mode2String(ERunSelectMode mode);
+  static ERunSelectMode String2Mode(const TString& str);
+  static ERunSelectMode Int2Mode(Int_t mode);
+  /**
+   * Various flags
+   * 
+   */
+  enum { 
+    /** 
+     * The maximum distance to the given run when selecting in kNear
+     * mode.  Currently this is set to a large number to allow
+     * selection of any entry.  This should change 
+     */
+    kMaxNearDistance = 1000000
+  };
+  //=== Entry ========================================================
+  /**
+   * An entry in the FORWARD OADB database tables
+   * 
+   */
+  class Entry : public TObject
+  {
+  public:
+    /** 
+     * Constructor 
+     * 
+     * @param runNo   Run number 
+     * @param sys     Collision system (1:pp, 2:pbpb, 3:ppb) 
+     * @param sNN     Center of mass energy (GeV)
+     * @param field   L3 magnetic field (kG)
+     * @param mc      True if for MC only
+     * @param o       Correction object
+     */
+    Entry(ULong_t  runNo  = 0, 
+         UShort_t sys    = 0, 
+         UShort_t sNN    = 0, 
+         Short_t  field  = 0, 
+         Bool_t   mc     = false, 
+         Bool_t   sat    = false,
+         TObject* o      = 0);
+    /** 
+     * Copy constructor
+     * 
+     * @param o Object to copy from
+     */
+    Entry(const Entry& o);
+    /** 
+     * Destructor 
+     */
+    virtual ~Entry() {}
+    /** 
+     * Assignment operator 
+     * 
+     * @param o Object to assign from 
+     * 
+     * @return Reference to this object
+     */
+    Entry& operator=(const Entry& o);
+    /** 
+     * Get the title of the entry 
+     * 
+     * @return stringified fields
+     */
+    const char* GetTitle() const;
+    /** 
+     * Print this entry 
+     * 
+     * @param option Not used 
+     */
+    void Print(Option_t* option="") const; //*MENU*
+    ULong_t  fRunNo;           // Run number 
+    UShort_t fSys;             // Collision system (1: pp, 2: pbpb, 3: ppb)
+    UShort_t fSNN;             // Center of mass energy 
+    Short_t  fField;           // L3 magnetic field
+    Bool_t   fMC;              // True if only for MC 
+    Bool_t   fSatellite;       // Satelitte events
+    TObject* fData;            // Correction object
+    UInt_t   fTimestamp;       // When the object was stored 
+    ULong_t  fAliROOTRevision; // Revision of AliROOT used 
+    TString  fAuthor;          // Author of calibration/correction
+
+    ClassDef(Entry,2); // Entry in PWGLF/Forward OADB
+  };
+
+  //=== Table ========================================================
+  /** 
+   * A table on the Forward OADB - the underlying storage is a TTree
+   * containing Entry objects.
+   * 
+   */
+  class Table : public TObject
+  {
+  public:
+    /** 
+     * Constructor 
+     * 
+     * @param tree   Tree 
+     * @param isNew  Whether to make the branch 
+     * 
+     * @return 
+     */    
+    Table(TTree* tree, Bool_t isNew, ERunSelectMode mode=kNear);
+    /** 
+     * Copy constructor 
+     * 
+     * @param other Object to copy from 
+     */
+    Table(const Table& other);
+    /** 
+     * Destructor. 
+     *
+     * Closes the corresponding file 
+     */
+    ~Table();
+    /** 
+     * Assignemt operator
+     * 
+     * @param other Object to assign form 
+     * 
+     * @return Reference to this object 
+     */
+    Table& operator=(const Table& other);
+    /** 
+     * Set the verbosity
+     * 
+     * @param verb 
+     */
+    void SetVerbose(Bool_t verb=true) { fVerbose = verb; }
+    // -----------------------------------------------------------------
+    /** 
+     * Get the name of the tree 
+     * 
+     * @return Table name 
+     */
+    const Char_t* GetTableName() const;
+    /** 
+     * Overload TObject::GetName 
+     * 
+     * @return Name 
+     */
+    const Char_t* GetName() const;
+
+    // -----------------------------------------------------------------
+    /** 
+     * @{ 
+     * @name Open/close/flush 
+     */
+    /** 
+     * Flush to disk 
+     * 
+     * @return true on success
+     */
+    Bool_t Update();
+    /** 
+     * Close connection
+     * 
+     * @return true on success 
+     */
+    Bool_t Close();
+    /* @} */
+    // -----------------------------------------------------------------
+    /** 
+     * @{ 
+     * @name Queries 
+     */
+    /** 
+     * Query the tree 
+     * 
+     * @param run    Run number 
+     * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
+     * @param sNN    Center of mass energy (GeV)
+     * @param fld    L3 magnetic field (kG)
+     * @param mc     For MC only 
+     * @param sat    For satellite events
+     * @param mode   How to select on the run number 
+     * 
+     * @return Found entry number or negative number in case of problems
+     */
+    Int_t Query(ULong_t        runNo  = 0,
+               ERunSelectMode mode   = kNear,
+               UShort_t       sys    = 0,
+               UShort_t       sNN    = 0, 
+               Short_t        fld    = 999,
+               Bool_t         mc     = false,
+               Bool_t         sat    = false) const;
+    /** 
+     * Run a query with pre-build conditions
+     * 
+     * @param q     query string 
+     * @param runNo The given run number 
+     * @param mode  Run selection mode 
+     * 
+     * @return Entry number of selected entry 
+     */
+    Int_t Query(ULong_t        runNo,
+               ERunSelectMode mode,
+               const TString& q) const;
+    /** 
+     * Insert a new entry into the tree 
+     * 
+     * @param o         Object to write 
+     * @param runNo     Run number 
+     * @param sys       Collision system (1: pp, 2:PbPb, 3:pPb)
+     * @param sNN       Center of mass energy (GeV)
+     * @param field     L3 magnetic field (kG)
+     * @param mc        If true, only for MC 
+     * @param aliRev    AliROOT revision
+     * 
+     * @return true on success 
+     */
+    Bool_t Insert(TObject* o, 
+                 ULong_t  runNo, 
+                 UShort_t sys, 
+                 UShort_t sNN, 
+                 Short_t  field, 
+                 Bool_t   mc=false, 
+                 Bool_t   sat=false,
+                 ULong_t  aliRev=0,
+                 const TString& author="");
+    /** 
+     * Query the tree for an object.  The strategy is as follows. 
+     * 
+     *  - First query with all fields 
+     *    - If this returns a single entry, return that. 
+     *    - If not, then ignore the run number (if given)
+     *      - If this returns a single entry, return that 
+     *      - Otherwise, give up and return null
+     *
+     * This allow us to specify default objects for a period, and for
+     * collision system, energy, and field setting.
+     *
+     * @param run    Run number 
+     * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
+     * @param sNN    Center of mass energy (GeV)
+     * @param fld    L3 magnetic field (kG)
+     * @param mc     For MC only 
+     * 
+     * @return Found entry, or null
+     */
+    Entry* Get(ULong_t        run    = 0,
+              ERunSelectMode mode   = kNear,
+              UShort_t       sys    = 0,
+              UShort_t       sNN    = 0, 
+              Short_t        fld    = 0,
+              Bool_t         mc     = false,
+              Bool_t         sat    = false) const;
+    /** 
+     * Query the tree for an object.  The strategy is as follows. 
+     * 
+     *  - First query with all fields 
+     *    - If this returns a single entry, return that. 
+     *    - If not, then ignore the run number (if given)
+     *      - If this returns a single entry, return that 
+     *      - Otherwise, give up and return null
+     *
+     * This allow us to specify default objects for a period, and for
+     * collision system, energy, and field setting.
+     *
+     * @param run    Run number 
+     * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
+     * @param sNN    Center of mass energy (GeV)
+     * @param fld    L3 magnetic field (kG)
+     * @param mc     For MC only 
+     * 
+     * @return Found data, or null
+     */
+    TObject* GetData(ULong_t        run    = 0,
+                    ERunSelectMode mode   = kNear,
+                    UShort_t       sys    = 0,
+                    UShort_t       sNN    = 0, 
+                    Short_t        fld    = 0,
+                    Bool_t         mc     = false,
+                    Bool_t         sat    = false) const;
+    /* @} */
+    /** 
+     * Print the contents of the tree 
+     * 
+     * @param option Passed to TTree::Scan
+     */
+    void Print(Option_t* option="") const; //*MENU*
+    /** 
+     * Browse this table 
+     */
+    void Browse(TBrowser* b); 
+    /** 
+     * Check if the tree was opened. 
+     * 
+     * @param rw If true, also check if the file is read/write 
+     *
+     * @return true if everything is dandy
+     */
+    Bool_t IsOpen(Bool_t rw=false) const; 
+
+    TTree*         fTree;     // Our tree
+    Entry*         fEntry;    // Entry cache 
+    Bool_t         fVerbose;  // To be verbose or not 
+    ERunSelectMode fMode;
+
+
+    ClassDef(Table,1); 
+  };
+  // === Interface ===================================================
+  /** 
+   * Constructor 
+   * 
+   * @param file File that stores the DB
+   */
+  AliOADBForward();
+  /**
+   * Destructor
+   * 
+   */
+  ~AliOADBForward();
+  // --- Open/close/update -------------------------------------------
+  /** 
+   * @{ 
+   * @name Input/output 
+   */
+  /** 
+   * Open a file containing tables.  Note, this member function can be
+   * called multiple times to open tables in different files.
+   * 
+   * If a table is already associated with this handler, it will not
+   * be re-associated.
+   *
+   * @param fileName  Path to file to get/write tables from/in
+   * @param rw        if true, open read+write, otherwise read-only
+   * @param tables    Tables to open 
+   * @param verb      Verbosity flag 
+   * 
+   * @return true on success 
+   */
+  Bool_t Open(const TString& fileName, 
+             const TString& tables  = "*", 
+             Bool_t         rw      = false, 
+             Bool_t         verb    = false);
+  /** 
+   * Open a file containing tables.  Note, this member function can be
+   * called multiple times to open tables in different files.
+   *
+   * If a table is already associated with this handler, it will not
+   * be re-associated.
+   * 
+   * @param file    File to get/write tables from/in
+   * @param rw      if true, open read+write, otherwise read-only
+   * @param tables  Tables to open 
+   * @param verb    Verbosity flag 
+   * 
+   * @return true on success 
+   */
+  Bool_t Open(TFile*         file,
+             const TString& tables, 
+             Bool_t         rw      = false, 
+             Bool_t         verb    = false);
+  /** 
+   * Close this database 
+   * 
+   * 
+   * @return true on success
+   */
+  Bool_t Close();
+  /** 
+   * Flush to disk 
+   * 
+   * @return true on success
+   */
+  Bool_t Update();
+  /* @} */
+  // --- Queries -----------------------------------------------------
+  /** 
+   * @{ 
+   * @name Queries 
+   */
+  /** 
+   * Query the table for an object.  The strategy is as follows. 
+   * 
+   *  - First query with all fields 
+   *    - If this returns a single entry, return that. 
+   *    - If not, then ignore the run number (if given)
+   *      - If this returns a single entry, return that 
+   *      - Otherwise, give up and return null
+   *
+   * This allow us to specify default objects for a period, and for
+   * collision system, energy, and field setting.
+   *
+   * @param run    Run number 
+   * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
+   * @param sNN    Center of mass energy (GeV)
+   * @param fld    L3 magnetic field (kG)
+   * @param mc     For MC only 
+   * 
+   * @return Found entry, or null
+   */
+  Entry* Get(const TString& table, 
+            ULong_t        run        = 0,
+            ERunSelectMode mode       = kNear, 
+            UShort_t       sys        = 0,
+            UShort_t       sNN        = 0, 
+            Short_t        fld        = 0,
+            Bool_t         mc         = false,
+            Bool_t         sat        = false) const;
+  /** 
+   * Query the table for an object.  The strategy is as follows. 
+   * 
+   *  - First query with all fields 
+   *    - If this returns a single entry, return that. 
+   *    - If not, then ignore the run number (if given)
+   *      - If this returns a single entry, return that 
+   *      - Otherwise, give up and return null
+   *
+   * This allow us to specify default objects for a period, and for
+   * collision system, energy, and field setting.
+   *
+   * @param run    Run number 
+   * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
+   * @param sNN    Center of mass energy (GeV)
+   * @param fld    L3 magnetic field (kG)
+   * @param mc     For MC only 
+   * 
+   * @return Found data, or null
+   */
+  TObject* GetData(const TString& table, 
+                  ULong_t        run    = 0,
+                  ERunSelectMode mode = kNear, 
+                  UShort_t       sys    = 0,
+                  UShort_t       sNN    = 0, 
+                  Short_t        fld    = 0,
+                  Bool_t         mc     = false,
+                  Bool_t         sat    = false) const;
+  // --- Insert ------------------------------------------------------
+  /** 
+   * Insert a new entry into the table 
+   * 
+   * @param o         Object to write 
+   * @param runNo     Run number 
+   * @param sys       Collision system (1: pp, 2:PbPb, 3:pPb)
+   * @param sNN       Center of mass energy (GeV)
+   * @param field     L3 magnetic field (kG)
+   * @param mc        If true, only for MC 
+   * @param aliRev    AliROOT revision
+   * 
+   * @return true on success 
+   */
+  Bool_t Insert(const TString& table, 
+               TObject*       o, 
+               ULong_t        runNo, 
+               UShort_t       sys, 
+               UShort_t       sNN, 
+               Short_t        field, 
+               Bool_t         mc=false, 
+               Bool_t         sat=false,
+               ULong_t        aliRev=0,
+               const TString& author="");
+  /* @} */
+  /** 
+   * Print the content of all tables
+   * 
+   * @param option Passed on to tables 
+   */  
+  void Print(const Option_t* option="") const; //*MENU*
+  /** 
+   * Browse this database
+   * 
+   * @param b Browser 
+   */
+  void Browse(TBrowser* b); 
+  /** 
+   * Find a table by name 
+   * 
+   * @param name  Name of table 
+   * @param quite Do not print warning if not found 
+   * @return Table or null 
+   */
+  Table* FindTable(const TString& name, Bool_t quite=false) const;
+  /** 
+   * Get all tables 
+   * 
+   * 
+   * @return Map of all tables
+   */
+  const TMap& GetTables() const { return fTables; }
+protected:
+  /** 
+   * Get a list of associated files 
+   * 
+   * @param files On return, contains list of files 
+   * 
+   * @return Number of associated files found 
+   */
+  Int_t GetFiles(TList& files) const;
+  /** 
+   * Get a table (TTree) from a file 
+   * 
+   * @param file   File to look in 
+   * @param rw     If true, open read/write, false read-only
+   * @param name   Name of the table 
+   * @param mode   Default mode for new table, or override mode 
+   *               for existing tables if not default
+   * 
+   * @return Table or (if rw=true) possibly newly created table
+   */
+  Table* GetTableFromFile(TFile* file, Bool_t rw, 
+                         const TString& name,
+                         const TString& mode) const;
+  /** 
+   * Helper function to append to query string 
+   * 
+   * @param q         String to attach to
+   * @param s         What to attach 
+   * @param andNotOr  If true, assume @b and, otherwise @b or
+   */
+  static void AppendToQuery(TString& q, const TString& s, Bool_t andNotOr=true);
+  /** 
+   * Helper function to build a query string 
+   * 
+   * @param sys    Collision system (1:pp, 2: PbPb, 3:pPb)
+   * @param sNN    Collision energy in GeV
+   * @param fld    L3-Field strength and polarity 
+   * @param mc     For Monte-carlo
+   * @param sat    For satelitte collisions 
+   * 
+   * @return Query string 
+   */
+  static TString Conditions(UShort_t       sys    = 0,
+                           UShort_t       sNN    = 0, 
+                           Short_t        fld    = 999,
+                           Bool_t         mc     = false,
+                           Bool_t         sat    = false);
+  TMap fTables;
+
+  ClassDef(AliOADBForward,0); // PWGLF/Forward OADB interface
+
+public:
+  // =================================================================
+  /** 
+   * @{ 
+   * @name Tests
+   */
+  static void TestGet(AliOADBForward& t, 
+                     const TString& table,
+                     ULong_t  runNo      = 0,
+                     ERunSelectMode mode = kNear,
+                     UShort_t sys        = 2,
+                     UShort_t sNN        = 2760, 
+                     Short_t  fld        = -5,
+                     Bool_t   mc         = false,
+                     Bool_t   sat        = false);
+  static void TestInsert(AliOADBForward& t, 
+                        const TString&  table,
+                        ULong_t         runNo  = 0,
+                        UShort_t        sys    = 2,
+                        UShort_t        sNN    = 2760, 
+                        Short_t         fld    = -5,
+                        Bool_t          mc     = false,
+                        Bool_t          sat    = false);
+  static void Test();
+  /* @} */
+};
+
+#endif
+/* Local Variables:
+ *  mode: C++
+ * End:
+ */