Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id: RooCategory.h,v 1.27 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 #ifndef ROO_CATEGORY
0017 #define ROO_CATEGORY
0018 
0019 #include "RooAbsCategoryLValue.h"
0020 #include "RooSharedProperties.h"
0021 
0022 #include <vector>
0023 #include <map>
0024 #include <string>
0025 
0026 class RooCategorySharedProperties;
0027 
0028 class RooCategory final : public RooAbsCategoryLValue {
0029 public:
0030   // Constructor, assignment etc.
0031   RooCategory() ;
0032   RooCategory(const char *name, const char *title);
0033   RooCategory(const char* name, const char* title, const std::map<std::string, int>& allowedStates);
0034   RooCategory(const RooCategory& other, const char* name=nullptr) ;
0035   RooCategory& operator=(const RooCategory&) = delete;
0036   ~RooCategory() override;
0037   TObject* clone(const char* newname) const override { return new RooCategory(*this,newname); }
0038 
0039   /// Return current index.
0040   value_type getCurrentIndex() const final {
0041     return RooCategory::evaluate();
0042   }
0043 
0044   bool setIndex(Int_t index, bool printError = true) override;
0045   using RooAbsCategoryLValue::setIndex;
0046   bool setLabel(const char* label, bool printError = true) override;
0047   using RooAbsCategoryLValue::setLabel;
0048 
0049   // I/O streaming interface (machine readable)
0050   bool readFromStream(std::istream& is, bool compact, bool verbose=false) override;
0051   void writeToStream(std::ostream& os, bool compact) const override ;
0052 
0053   bool defineType(const std::string& label);
0054   bool defineType(const std::string& label, Int_t index);
0055   void defineTypes(const std::map<std::string, int>& allowedStates);
0056   value_type& operator[](const std::string& stateName);
0057   std::map<std::string, RooAbsCategory::value_type>& states();
0058 
0059   /// \cond LEGACY
0060   bool defineType(const char* label) {
0061     return defineType(std::string(label));
0062   }
0063   bool defineType(const char* label, Int_t index) {
0064     return defineType(std::string(label), index);
0065   }
0066   /// \endcond
0067 
0068   /// Clear all defined category states.
0069   void clear() {
0070     clearTypes();
0071   }
0072 
0073   void clearRange(const char* name, bool silent) ;
0074   void setRange(const char* rangeName, const char* stateNameList) ;
0075   void addToRange(const char* rangeName, RooAbsCategory::value_type stateIndex);
0076   void addToRange(const char* rangeName, const char* stateNameList) ;
0077 
0078 
0079   /// \name RooFit interface
0080   /// @{
0081 
0082   /// Tell whether we can be stored in a dataset. Always true for RooCategory.
0083   inline bool isFundamental() const override {
0084     return true;
0085   }
0086 
0087   /// Does our value or shape depend on any other arg? Always false for RooCategory.
0088   bool isDerived() const override {
0089     return false;
0090   }
0091 
0092   bool isStateInRange(const char* rangeName, RooAbsCategory::value_type stateIndex) const ;
0093   bool isStateInRange(const char* rangeName, const char* stateName) const ;
0094   /// Check if the currently defined category state is in the range with the given name.
0095   /// If no ranges are defined, the state counts as being in range.
0096   bool inRange(const char* rangeName) const override {
0097     return isStateInRange(rangeName, RooCategory::evaluate());
0098   }
0099   /// Returns true if category has a range with given name defined.
0100   bool hasRange(const char* rangeName) const override {
0101     return _ranges->find(rangeName) != _ranges->end();
0102   }
0103 
0104   ///@}
0105 
0106 protected:
0107   /// \copydoc RooAbsCategory::evaluate() const
0108   /// Returns the currently set state index. If this is invalid,
0109   /// returns the first-set index.
0110   value_type evaluate() const override {
0111     if (hasIndex(_currentIndex))
0112       return _currentIndex;
0113 
0114     if (_insertionOrder.empty()) {
0115       return invalidCategory().second;
0116     } else {
0117       auto item = stateNames().find(_insertionOrder.front());
0118       assert(item != stateNames().end());
0119       return item->second;
0120     }
0121   }
0122 
0123   /// This category's shape does not depend on others, and does not need recomputing.
0124   void recomputeShape() override { };
0125 
0126 private:
0127 
0128   using RangeMap_t = std::map<std::string, std::vector<value_type>>;
0129   /// Map range names to allowed category states. Note that this must be shared between copies,
0130   /// so categories in datasets have the same ranges as their counterparts outside of the dataset.
0131   std::shared_ptr<RangeMap_t> _ranges{new RangeMap_t()}; //!
0132   RangeMap_t* _rangesPointerForIO{nullptr}; ///< Pointer to the same object as _ranges, but not shared for I/O.
0133 
0134   void installLegacySharedProp(const RooCategorySharedProperties* sp);
0135   void installSharedRange(std::unique_ptr<RangeMap_t>&& rangeMap);
0136   /// Helper for restoring shared ranges from old versions of this class read from files. Maps TUUID names to shared ranges.
0137   static std::map<RooSharedProperties::UUID, std::weak_ptr<RangeMap_t>> _uuidToSharedRangeIOHelper;
0138   /// Helper for restoring shared ranges from current versions of this class read from files. Maps category names to shared ranges.
0139   static std::map<std::string, std::weak_ptr<RangeMap_t>> _sharedRangeIOHelper;
0140 
0141   ClassDefOverride(RooCategory, 3) // Discrete valued variable type
0142 };
0143 
0144 #endif