Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:20

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id: RooCmdConfig.h,v 1.12 2007/05/11 09:11:30 verkerke Exp $
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *                                                                           *
0009  * Copyright (c) 2000-2005, Regents of the University of California          *
0010  *                          and Stanford University. All rights reserved.    *
0011  *                                                                           *
0012  * Redistribution and use in source and binary forms,                        *
0013  * with or without modification, are permitted according to the terms        *
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
0015  *****************************************************************************/
0016 
0017 #ifndef ROO_CMD_CONFIG
0018 #define ROO_CMD_CONFIG
0019 
0020 #include <RooCmdArg.h>
0021 #include <RooStringView.h>
0022 
0023 #include <TList.h>
0024 #include <TObjString.h>
0025 #include <TObject.h>
0026 #include <TString.h>
0027 
0028 #include <string>
0029 
0030 class RooArgSet;
0031 
0032 class RooCmdConfig : public TObject {
0033 public:
0034 
0035   RooCmdConfig(RooStringView methodName);
0036   RooCmdConfig(const RooCmdConfig& other) ;
0037 
0038   /// If flag is true verbose messaging is activated
0039   void setVerbose(bool flag) {
0040     _verbose = flag ;
0041   }
0042   /// If flag is true the processing of unrecognized RooCmdArgs
0043   /// is not considered an error
0044   void allowUndefined(bool flag=true) {
0045     _allowUndefined = flag ;
0046   }
0047   void defineDependency(const char* refArgName, const char* neededArgName) ;
0048 
0049   template<class... Args_t>
0050   void defineRequiredArgs(const char* first, Args_t && ... args);
0051 
0052   template<class... Args_t>
0053   void defineMutex(const char* head, Args_t && ... tail);
0054   void defineMutex(const char*) {} // to end the recursion of defineMutex()
0055 
0056   bool defineInt(const char* name, const char* argName, int intNum, int defValue=0) ;
0057   bool defineDouble(const char* name, const char* argName, int doubleNum, double defValue=0.0) ;
0058   bool defineString(const char* name, const char* argName, int stringNum, const char* defValue="",bool appendMode=false) ;
0059   bool defineObject(const char* name, const char* argName, int setNum, const TObject* obj=nullptr, bool isArray=false) ;
0060   bool defineSet(const char* name, const char* argName, int setNum, const RooArgSet* set=nullptr) ;
0061 
0062   bool process(const RooCmdArg& arg) ;
0063   template<class... Args_t>
0064   bool process(const RooCmdArg& arg, Args_t && ...args);
0065   bool process(const RooLinkedList& argList) ;
0066   template<typename It_t>
0067   bool process(It_t begin, It_t end);
0068 
0069   int getInt(const char* name, int defaultValue=0) const;
0070   double getDouble(const char* name, double defaultValue=0.0) const;
0071   const char* getString(const char* name, const char* defaultValue="",bool convEmptyToNull=false) const;
0072   TObject* getObject(const char* name, TObject* obj=nullptr) const;
0073   RooArgSet* getSet(const char* name, RooArgSet* set=nullptr) const;
0074   const RooLinkedList& getObjectList(const char* name) const;
0075 
0076   bool ok(bool verbose) const ;
0077 
0078   std::string missingArgs() const ;
0079 
0080   RooLinkedList filterCmdList(RooLinkedList& cmdInList, const char* cmdNameList, bool removeFromInList=true) const;
0081   static void stripCmdList(RooLinkedList& cmdList, const char* cmdsToPurge);
0082   bool hasProcessed(const char* cmdName) const ;
0083 
0084   void print() const;
0085 
0086 
0087   template<class ...Args_t>
0088   static int decodeIntOnTheFly(
0089           const char* callerID, const char* cmdArgName, int intIdx, int defVal, Args_t && ...args);
0090 
0091   template<class ...Args_t>
0092   static std::string decodeStringOnTheFly(
0093           const char* callerID, const char* cmdArgName, int intIdx, const char* defVal, Args_t && ...args);
0094 
0095   template<class ...Args_t>
0096   static TObject* decodeObjOnTheFly(
0097           const char* callerID, const char* cmdArgName, int objIdx, TObject* defVal, Args_t && ...args);
0098 
0099   template<class ...Args_t>
0100   static RooArgSet* decodeSetOnTheFly(
0101           const char* callerID, const char* cmdArgName, int objIdx, RooArgSet* defVal, Args_t && ...args);
0102 
0103   static double decodeDoubleOnTheFly(const char* callerID, const char* cmdArgName, int idx, double defVal,
0104       std::initializer_list<std::reference_wrapper<const RooCmdArg>> args);
0105 
0106 protected:
0107 
0108   template<class T>
0109   struct Var {
0110     std::string name;
0111     std::string argName;
0112     T val;
0113     bool appendMode;
0114     int num;
0115   };
0116 
0117   std::string _name;
0118 
0119   bool _verbose = false;
0120   bool _error = false;
0121   bool _allowUndefined = false;
0122 
0123   std::vector<Var<int>> _iList ; ///< Integer list
0124   std::vector<Var<double>> _dList ; ///< Double list
0125   std::vector<Var<std::string>> _sList ; ///< String list
0126   std::vector<Var<RooLinkedList>> _oList ; ///< Object list
0127   std::vector<Var<RooArgSet*>> _cList ; ///< RooArgSet list
0128 
0129   TList _rList ; ///< Required cmd list
0130   TList _fList ; ///< Forbidden cmd list
0131   TList _mList ; ///< Mutex cmd list
0132   TList _yList ; ///< Dependency cmd list
0133   TList _pList ; ///< Processed cmd list
0134 
0135   ClassDefOverride(RooCmdConfig,0) // Configurable parse of RooCmdArg objects
0136 };
0137 
0138 
0139 ////////////////////////////////////////////////////////////////////////////////
0140 /// Add condition that any of listed arguments must be processed
0141 /// for parsing to be declared successful
0142 template<class... Args_t>
0143 void RooCmdConfig::defineRequiredArgs(const char* first, Args_t && ... args) {
0144   for(auto const& arg : {first, args...}) {
0145       if (arg) _rList.Add(new TObjString(arg));
0146   }
0147 }
0148 
0149 
0150 //////////////////////////////////////////////////////////////////////////////////
0151 /// Define arguments where any pair is mutually exclusive
0152 template<class... Args_t>
0153 void RooCmdConfig::defineMutex(const char* head, Args_t && ... tail) {
0154   for(auto const& item : {tail...}) {
0155     _mList.Add(new TNamed(head,item));
0156     _mList.Add(new TNamed(item,head));
0157   }
0158   defineMutex(tail...);
0159 }
0160 
0161 
0162 ////////////////////////////////////////////////////////////////////////////////
0163 /// Process given RooCmdArgs
0164 template<class... Args_t>
0165 bool RooCmdConfig::process(const RooCmdArg& arg, Args_t && ...args) {
0166   bool result = false;
0167   for(auto r : {process(arg), process(std::forward<Args_t>(args))...}) result |= r;
0168   return result;
0169 }
0170 
0171 
0172 ////////////////////////////////////////////////////////////////////////////////
0173 /// Process several RooCmdArg using iterators.
0174 template<typename It_t>
0175 bool RooCmdConfig::process(It_t begin, It_t end) {
0176   bool result = false;
0177   for (auto it = begin; it != end; ++it) {
0178     result |= process(*it);
0179   }
0180   return result;
0181 }
0182 
0183 
0184 ////////////////////////////////////////////////////////////////////////////////
0185 /// Static decoder function allows to retrieve integer property from set of RooCmdArgs
0186 /// For use in base member initializers in constructors
0187 
0188 template<class ...Args_t>
0189 int RooCmdConfig::decodeIntOnTheFly(
0190         const char* callerID, const char* cmdArgName, int intIdx, int defVal, Args_t && ...args)
0191 {
0192   RooCmdConfig pc(callerID) ;
0193   pc.allowUndefined() ;
0194   pc.defineInt("theInt",cmdArgName,intIdx,defVal) ;
0195   pc.process(std::forward<Args_t>(args)...);
0196   return pc.getInt("theInt") ;
0197 }
0198 
0199 
0200 ////////////////////////////////////////////////////////////////////////////////
0201 /// Static decoder function allows to retrieve string property from set of RooCmdArgs
0202 /// For use in base member initializers in constructors
0203 
0204 template<class ...Args_t>
0205 std::string RooCmdConfig::decodeStringOnTheFly(
0206         const char* callerID, const char* cmdArgName, int strIdx, const char* defVal, Args_t && ...args)
0207 {
0208   RooCmdConfig pc(callerID) ;
0209   pc.allowUndefined() ;
0210   pc.defineString("theString",cmdArgName,strIdx,defVal) ;
0211   pc.process(std::forward<Args_t>(args)...);
0212   const char* ret =  pc.getString("theString",nullptr,true) ;
0213 
0214   return ret ? ret : "";
0215 }
0216 
0217 
0218 ////////////////////////////////////////////////////////////////////////////////
0219 /// Static decoder function allows to retrieve object property from set of RooCmdArgs
0220 /// For use in base member initializers in constructors
0221 
0222 template<class ...Args_t>
0223 TObject* RooCmdConfig::decodeObjOnTheFly(
0224         const char* callerID, const char* cmdArgName, int objIdx, TObject* defVal, Args_t && ...args)
0225 {
0226   RooCmdConfig pc(callerID) ;
0227   pc.allowUndefined() ;
0228   pc.defineObject("theObj",cmdArgName,objIdx,defVal) ;
0229   pc.process(std::forward<Args_t>(args)...);
0230   return pc.getObject("theObj") ;
0231 }
0232 
0233 
0234 template<class ...Args_t>
0235 RooArgSet* RooCmdConfig::decodeSetOnTheFly(
0236         const char* callerID, const char* cmdArgName, int objIdx, RooArgSet* defVal, Args_t && ...args)
0237 {
0238   RooCmdConfig pc(callerID) ;
0239   pc.allowUndefined() ;
0240   pc.defineSet("theObj",cmdArgName,objIdx,defVal) ;
0241   pc.process(std::forward<Args_t>(args)...);
0242   return pc.getSet("theObj") ;
0243 }
0244 
0245 
0246 #endif