Logo ROOT  
Reference Guide
RooAbsCategoryLegacyIterator.h
Go to the documentation of this file.
1 // Author: Stephan Hageboeck, CERN 3 Feb 2020
2 
3 /*****************************************************************************
4  * RooFit
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2019, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 #ifndef ROOABSCATEGORYLEGACYITERATOR_H_
18 #define ROOABSCATEGORYLEGACYITERATOR_H_
19 
20 #include "TIterator.h"
21 #include "RooAbsCategory.h"
22 #include <map>
23 #include <vector>
24 #include <string>
25 
27 
28 /// \deprecated Legacy class to iterate through legacy RooAbsCategory states.
29 /// Use RooAbsCategory::begin(), RooAbsCategory::end() or range-based for loops instead.
30 /// \ingroup Roofitlegacy
32  public:
33  RooAbsCategoryLegacyIterator(const std::map<std::string, RooAbsCategory::value_type>& stateNames) :
34  _origStateNames(&stateNames), index(-1) {
35  populate();
36  }
37 
38  virtual const TCollection *GetCollection() const override { return nullptr; }
39 
40  TObject* Next() override {
41  ++index;
42  return this->operator*();
43  }
44 
45  void Reset() override { populate(); index = -1; }
46 
47  TObject* operator*() const override {
48  if (! (0 <= index && index < (int)_origStateNames->size()) )
49  return nullptr;
50 
51  return const_cast<RooCatType*>(&_legacyStates[index]);
52  }
53 
55 
56  TIterator& operator=(const TIterator&) override {
57  throw std::logic_error("Assigning from another iterator is not supported for the RooAbsCategoryLegacyIterator.");
58  }
59 
60  private:
61  void populate() {
62  _legacyStates.clear();
63 
64  for (const auto& item : *_origStateNames) {
65  _legacyStates.emplace_back(item.first.c_str(), item.second);
66  }
67  std::sort(_legacyStates.begin(), _legacyStates.end(), [](const RooCatType& left, const RooCatType& right){
68  return left.getVal() < right.getVal();
69  });
70  }
71 
72  const std::map<std::string, RooAbsCategory::value_type>* _origStateNames;
73  std::vector<RooCatType> _legacyStates;
74  int index;
75 };
76 
77 
78 #endif /* ROOABSCATEGORYLEGACYITERATOR_H_ */
RooAbsCategoryLegacyIterator::Reset
void Reset() override
Definition: RooAbsCategoryLegacyIterator.h:57
RooAbsCategoryLegacyIterator::populate
void populate()
Definition: RooAbsCategoryLegacyIterator.h:73
RooAbsCategoryLegacyIterator::operator=
RooAbsCategoryLegacyIterator & operator=(const RooAbsCategoryLegacyIterator &)=default
RooAbsCategoryLegacyIterator::_origStateNames
const std::map< std::string, RooAbsCategory::value_type > * _origStateNames
Definition: RooAbsCategoryLegacyIterator.h:84
RooAbsCategoryLegacyIterator::operator*
TObject * operator*() const override
Return current object or nullptr.
Definition: RooAbsCategoryLegacyIterator.h:59
TIterator
Definition: TIterator.h:30
RooAbsCategoryLegacyIterator::GetCollection
virtual const TCollection * GetCollection() const override
Definition: RooAbsCategoryLegacyIterator.h:50
RooAbsCategoryLegacyIterator::Next
TObject * Next() override
Definition: RooAbsCategoryLegacyIterator.h:52
TIterator.h
RooAbsCategoryLegacyIterator::RooAbsCategoryLegacyIterator
RooAbsCategoryLegacyIterator(const std::map< std::string, RooAbsCategory::value_type > &stateNames)
Definition: RooAbsCategoryLegacyIterator.h:45
RooAbsCategoryLegacyIterator::_legacyStates
std::vector< RooCatType > _legacyStates
Definition: RooAbsCategoryLegacyIterator.h:85
RooAbsCategoryLegacyIterator
Definition: RooAbsCategoryLegacyIterator.h:31
RooAbsCategory.h
TObject
Definition: TObject.h:37
RooCatType
Definition: RooCatTypeLegacy.h:23
RooAbsCategoryLegacyIterator::index
int index
Definition: RooAbsCategoryLegacyIterator.h:86
TCollection
Definition: TCollection.h:63
RooCatTypeLegacy.h