Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:09:05

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