Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 09:30:32

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 {
0040 // clang++ <v20 (-Wshadow) complains about shadowing TAttMarker.h global enum EMarkerStyle. Let's silence warning:
0041 #if defined(__clang__) && __clang_major__ < 20
0042 #pragma clang diagnostic push
0043 #pragma clang diagnostic ignored "-Wshadow"
0044 #endif
0045       kMultiply   = BIT(16),    // Use multiplication
0046 #if defined(__clang__) && __clang_major__ < 20
0047 #pragma clang diagnostic pop
0048 #endif
0049       kMax        = BIT(17),    // Take max value
0050       kMin        = BIT(18),    // Take min value
0051       kFirst      = BIT(19),    // Take the first value
0052       kLast       = BIT(20),    // Take the last value
0053       kIsConst    = BIT(21)     // Set if all values are equal
0054    };
0055 
0056 private:
0057    TString     fName;
0058    AParamType  fVal;
0059 
0060    void        Reset() { ResetBit(kMultiply); ResetBit(kMax); ResetBit(kMin);
0061                          ResetBit(kFirst); ResetBit(kLast); }
0062 
0063 public:
0064    TParameter(): fVal() { Reset(); SetBit(kIsConst); }
0065    TParameter(const char *name, const AParamType &val)
0066              : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
0067    TParameter(const char *name, const AParamType &val, char mergemode)
0068              : fName(name), fVal(val) { SetMergeMode(mergemode); SetBit(kIsConst);}
0069    virtual ~TParameter()
0070    {
0071       // Required since we overload TObject::Hash.
0072       ROOT::CallRecursiveRemoveIfNeeded(*this);
0073    }
0074 
0075    const char       *GetName() const override { return fName; }
0076    const AParamType &GetVal() const { return fVal; }
0077    Bool_t            IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
0078    void              SetVal(const AParamType &val) { fVal = val; }
0079 
0080    // Merging modes:
0081    //  '+'             addition ('OR' for booleans)               [default]
0082    //  '*'             multiplication ('AND' for booleans)
0083    //  'M'             maximum ('OR' for booleans)
0084    //  'm'             minimum ('AND' for booleans)
0085    //  'f'             first value
0086    //  'l'             last value
0087    void SetMergeMode(char mergemode = '+') {
0088       Reset();
0089       if (mergemode == '*') {
0090          SetBit(kMultiply);
0091       } else if (mergemode == 'M') {
0092          SetBit(kMax);
0093       } else if (mergemode == 'm') {
0094          SetBit(kMin);
0095       } else if (mergemode == 'f') {
0096          SetBit(kFirst);
0097       } else if (mergemode == 'l') {
0098          SetBit(kLast);
0099       }
0100    }
0101    ULong_t  Hash() const override { return fName.Hash(); }
0102    Bool_t   IsSortable() const override { return kTRUE; }
0103    Int_t    Compare(const TObject *obj) const override
0104    {
0105       // Compare two TParameter objects. Returns 0 when equal, -1 when this is
0106       // smaller and +1 when bigger (like strcmp).
0107 
0108       if (this == obj) return 0;
0109       return fName.CompareTo(obj->GetName());
0110    }
0111 
0112    void ls(Option_t *) const override
0113    {
0114       // Print this parameter content
0115       TROOT::IndentLevel();
0116       std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
0117    }
0118 
0119    void Print(Option_t *) const override
0120    {
0121       // Print this parameter content
0122       TROOT::IndentLevel();
0123       std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
0124    }
0125 
0126    virtual Int_t Merge(TCollection *in);
0127 
0128    ClassDefOverride(TParameter,2)  //Named templated parameter type
0129 };
0130 
0131 template <class AParamType>
0132 inline Int_t TParameter<AParamType>::Merge(TCollection *in) {
0133    // Merge objects in the list.
0134    // Returns the number of objects that were in the list.
0135    TIter nxo(in);
0136    Int_t n = 0;
0137    while (TObject *o = nxo()) {
0138       TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
0139       if (c) {
0140          // Check if constant
0141          if (fVal != c->GetVal()) ResetBit(kIsConst);
0142          if (TestBit(kMultiply)) {
0143             // Multiply
0144             fVal *= c->GetVal();
0145          } else if (TestBit(kMax)) {
0146             // Take max
0147             if (c->GetVal() > fVal) fVal = c->GetVal();
0148          } else if (TestBit(kMin)) {
0149             // Take min
0150             if (c->GetVal() < fVal) fVal = c->GetVal();
0151          } else if (TestBit(kLast)) {
0152             // Take the last
0153             fVal = c->GetVal();
0154          } else if (!TestBit(kFirst)) {
0155             // Add, if not asked to take the first
0156             fVal += c->GetVal();
0157          }
0158          n++;
0159       }
0160    }
0161 
0162    return n;
0163 }
0164 
0165 // Specialization of Merge for Bool_t
0166 template <>
0167 inline Int_t TParameter<Bool_t>::Merge(TCollection *in)
0168 {
0169    // Merge bool objects in the list.
0170    // Returns the number of objects that were in the list.
0171    TIter nxo(in);
0172    Int_t n = 0;
0173    while (TObject *o = nxo()) {
0174       TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
0175       if (c) {
0176          // Check if constant
0177          if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
0178          if (TestBit(TParameter::kMultiply) || TestBit(kMin)) {
0179             // And
0180             fVal &= (Bool_t) c->GetVal();
0181          } else if (TestBit(kLast)) {
0182             // Take the last
0183             fVal = (Bool_t) c->GetVal();
0184          } else if (!TestBit(kFirst) || TestBit(kMax)) {
0185             // Or
0186             fVal |= (Bool_t) c->GetVal();
0187          }
0188          n++;
0189       }
0190    }
0191 
0192    return n;
0193 }
0194 
0195 // FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
0196 // When building with -fmodules, it instantiates all pending instantiations,
0197 // instead of delaying them until the end of the translation unit.
0198 // We 'got away with' probably because the use and the definition of the
0199 // explicit specialization do not occur in the same TU.
0200 //
0201 // In case we are building with -fmodules, we need to forward declare the
0202 // specialization in order to compile the dictionary G__Core.cxx.
0203 template <> void TParameter<Long64_t>::Streamer(TBuffer &R__b);
0204 template<> TClass *TParameter<Long64_t>::Class();
0205 
0206 #endif