Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TParameter.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/base:$Id$
0002 // Author: Maarten Ballintijn   21/06/2004
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TParameter
0013 #define ROOT_TParameter
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TParameter<AParamType>                                               //
0019 //                                                                      //
0020 // Named parameter, streamable and storable.                            //
0021 //                                                                      //
0022 //////////////////////////////////////////////////////////////////////////
0023 
0024 #include "Riostream.h"
0025 
0026 #include "TObject.h"
0027 
0028 #include "TCollection.h"
0029 
0030 #include "TString.h"
0031 
0032 #include "TROOT.h"
0033 
0034 template <class AParamType>
0035 class TParameter : public TObject {
0036 
0037 public:
0038    // Defines options / status while merging:
0039    enum EStatusBits { kMultiply   = BIT(16),    // Use multiplication
0040                       kMax        = BIT(17),    // Take max value
0041                       kMin        = BIT(18),    // Take min value
0042                       kFirst      = BIT(19),    // Take the first value
0043                       kLast       = BIT(20),    // Take the last value
0044                       kIsConst    = BIT(21)     // Set if all values are equal
0045    };
0046 
0047 private:
0048    TString     fName;
0049    AParamType  fVal;
0050 
0051    void        Reset() { ResetBit(kMultiply); ResetBit(kMax); ResetBit(kMin);
0052                          ResetBit(kFirst); ResetBit(kLast); }
0053 
0054 public:
0055    TParameter(): fVal() { Reset(); SetBit(kIsConst); }
0056    TParameter(const char *name, const AParamType &val)
0057              : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
0058    TParameter(const char *name, const AParamType &val, char mergemode)
0059              : fName(name), fVal(val) { SetMergeMode(mergemode); SetBit(kIsConst);}
0060    virtual ~TParameter()
0061    {
0062       // Required since we overload TObject::Hash.
0063       ROOT::CallRecursiveRemoveIfNeeded(*this);
0064    }
0065 
0066    const char       *GetName() const override { return fName; }
0067    const AParamType &GetVal() const { return fVal; }
0068    Bool_t            IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
0069    void              SetVal(const AParamType &val) { fVal = val; }
0070 
0071    // Merging modes:
0072    //  '+'             addition ('OR' for booleans)               [default]
0073    //  '*'             multiplication ('AND' for booleans)
0074    //  'M'             maximum ('OR' for booleans)
0075    //  'm'             minimum ('AND' for booleans)
0076    //  'f'             first value
0077    //  'l'             last value
0078    void SetMergeMode(char mergemode = '+') {
0079       Reset();
0080       if (mergemode == '*') {
0081          SetBit(kMultiply);
0082       } else if (mergemode == 'M') {
0083          SetBit(kMax);
0084       } else if (mergemode == 'm') {
0085          SetBit(kMin);
0086       } else if (mergemode == 'f') {
0087          SetBit(kFirst);
0088       } else if (mergemode == 'l') {
0089          SetBit(kLast);
0090       }
0091    }
0092    ULong_t  Hash() const override { return fName.Hash(); }
0093    Bool_t   IsSortable() const override { return kTRUE; }
0094    Int_t    Compare(const TObject *obj) const override
0095    {
0096       // Compare two TParameter objects. Returns 0 when equal, -1 when this is
0097       // smaller and +1 when bigger (like strcmp).
0098 
0099       if (this == obj) return 0;
0100       return fName.CompareTo(obj->GetName());
0101    }
0102 
0103    void ls(Option_t *) const override
0104    {
0105       // Print this parameter content
0106       TROOT::IndentLevel();
0107       std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
0108    }
0109 
0110    void Print(Option_t *) const override
0111    {
0112       // Print this parameter content
0113       TROOT::IndentLevel();
0114       std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
0115    }
0116 
0117    virtual Int_t Merge(TCollection *in);
0118 
0119    ClassDefOverride(TParameter,2)  //Named templated parameter type
0120 };
0121 
0122 template <class AParamType>
0123 inline Int_t TParameter<AParamType>::Merge(TCollection *in) {
0124    // Merge objects in the list.
0125    // Returns the number of objects that were in the list.
0126    TIter nxo(in);
0127    Int_t n = 0;
0128    while (TObject *o = nxo()) {
0129       TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
0130       if (c) {
0131          // Check if constant
0132          if (fVal != c->GetVal()) ResetBit(kIsConst);
0133          if (TestBit(kMultiply)) {
0134             // Multiply
0135             fVal *= c->GetVal();
0136          } else if (TestBit(kMax)) {
0137             // Take max
0138             if (c->GetVal() > fVal) fVal = c->GetVal();
0139          } else if (TestBit(kMin)) {
0140             // Take min
0141             if (c->GetVal() < fVal) fVal = c->GetVal();
0142          } else if (TestBit(kLast)) {
0143             // Take the last
0144             fVal = c->GetVal();
0145          } else if (!TestBit(kFirst)) {
0146             // Add, if not asked to take the first
0147             fVal += c->GetVal();
0148          }
0149          n++;
0150       }
0151    }
0152 
0153    return n;
0154 }
0155 
0156 // Specialization of Merge for Bool_t
0157 template <>
0158 inline Int_t TParameter<Bool_t>::Merge(TCollection *in)
0159 {
0160    // Merge bool objects in the list.
0161    // Returns the number of objects that were in the list.
0162    TIter nxo(in);
0163    Int_t n = 0;
0164    while (TObject *o = nxo()) {
0165       TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
0166       if (c) {
0167          // Check if constant
0168          if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
0169          if (TestBit(TParameter::kMultiply) || TestBit(kMin)) {
0170             // And
0171             fVal &= (Bool_t) c->GetVal();
0172          } else if (TestBit(kLast)) {
0173             // Take the last
0174             fVal = (Bool_t) c->GetVal();
0175          } else if (!TestBit(kFirst) || TestBit(kMax)) {
0176             // Or
0177             fVal |= (Bool_t) c->GetVal();
0178          }
0179          n++;
0180       }
0181    }
0182 
0183    return n;
0184 }
0185 
0186 // FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
0187 // When building with -fmodules, it instantiates all pending instantiations,
0188 // instead of delaying them until the end of the translation unit.
0189 // We 'got away with' probably because the use and the definition of the
0190 // explicit specialization do not occur in the same TU.
0191 //
0192 // In case we are building with -fmodules, we need to forward declare the
0193 // specialization in order to compile the dictionary G__Core.cxx.
0194 template <> void TParameter<Long64_t>::Streamer(TBuffer &R__b);
0195 template<> TClass *TParameter<Long64_t>::Class();
0196 
0197 #endif