Logo ROOT  
Reference Guide
RooSuperCategory.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
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-2005, 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 /**
18 \file RooSuperCategory.cxx
19 \class RooSuperCategory
20 \ingroup Roofitcore
21 
22 The RooSuperCategory can join several RooAbsCategoryLValue objects into
23 a single category. For this, it uses a RooMultiCategory, which takes care
24 of enumerating all the permutations of possible states.
25 In addition, the super category derives from RooAbsCategoryLValue, *i.e.*, it allows for
26 setting its state (opposed to the RooMultiCategory, which just reacts
27 to the states of its subcategories). This requires that all input categories
28 are lvalues as well. This is because a modification of the state of the
29 supercategory will propagate to its input categories.
30 **/
31 
32 #include "RooSuperCategory.h"
33 
34 #include "RooFit.h"
35 #include "Riostream.h"
36 #include "RooStreamParser.h"
37 #include "RooArgSet.h"
39 #include "RooAbsCategoryLValue.h"
40 #include "RooMsgService.h"
41 
42 #include "TString.h"
43 #include "TClass.h"
44 
45 using namespace std;
46 
48 
51  _multiCat("MultiCatProxy", "Stores a RooMultiCategory", this, true, true, true) { }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Construct a super category from other categories.
55 /// \param[in] name Name of this object
56 /// \param[in] title Title (for e.g. printing)
57 /// \param[in] inputCatList RooArgSet with category objects. These all need to derive from RooAbsCategoryLValue, *i.e.*
58 /// one needs to be able to assign to them.
59 RooSuperCategory::RooSuperCategory(const char *name, const char *title, const RooArgSet& inputCategories) :
60  RooAbsCategoryLValue(name, title),
61  _multiCat("MultiCatProxy", "Stores a RooMultiCategory", this,
62  *new RooMultiCategory((std::string(name) + "_internalMultiCat").c_str(), title, inputCategories), true, true, true)
63 {
64  // Check category list
65  for (const auto arg : inputCategories) {
66  if (!arg->IsA()->InheritsFrom(RooAbsCategoryLValue::Class())) {
67  coutE(InputArguments) << "RooSuperCategory::RooSuperCategory(" << GetName() << "): input category " << arg->GetName()
68  << " is not an lvalue. Use RooMultiCategory instead." << endl ;
69  throw std::invalid_argument("Arguments of RooSuperCategory must be lvalues.");
70  }
71  }
72  setShapeDirty();
73 }
74 
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Copy constructor
79 
81  RooAbsCategoryLValue(other, name),
82  _multiCat("MultiCatProxy", this, other._multiCat)
83 {
85  setShapeDirty();
86 }
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Make an iterator over all state permutations of
91 /// the input categories of this supercategory.
92 /// The iterator just generates state names, it does not set them.
94 {
95  return new RooMultiCatIter(_multiCat->inputCatList());
96 }
97 
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Set the value of the super category to the specified index.
101 /// This will propagate to the sub-categories, and set their state accordingly.
102 bool RooSuperCategory::setIndex(Int_t index, Bool_t printError)
103 {
104  if (index < 0) {
105  if (printError)
106  coutE(InputArguments) << "RooSuperCategory can only have positive index states. Got " << index << std::endl;
107  return true;
108  }
109 
110  bool error = false;
111  for (auto arg : _multiCat->_catSet) {
112  auto cat = static_cast<RooAbsCategoryLValue*>(arg);
113  if (cat->size() == 0) {
114  if (printError)
115  coutE(InputArguments) << __func__ << ": Found a category with zero states. Cannot set state for '"
116  << cat->GetName() << "'." << std::endl;
117  continue;
118  }
119  const value_type thisIndex = index % cat->size();
120  error |= cat->setOrdinal(thisIndex);
121  index = (index - thisIndex) / cat->size();
122  }
123 
124  return error;
125 }
126 
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Set the value of the super category by specifying the state name.
131 /// This looks up the corresponding index number, and calls setIndex().
132 Bool_t RooSuperCategory::setLabel(const char* label, Bool_t printError)
133 {
134  const value_type index = _multiCat->lookupIndex(label);
135  return setIndex(index, printError);
136 }
137 
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Print the state of this object to the specified output stream.
141 
143 {
145 
146  if (verbose) {
147  os << indent << "--- RooSuperCategory ---" << '\n';
148  os << indent << " Internal RooMultiCategory:" << '\n';
149  _multiCat->printMultiline(os, content, verbose, indent+" ");
150 
151  os << std::endl;
152  }
153 }
154 
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Check that all input category states are in the given range.
158 Bool_t RooSuperCategory::inRange(const char* rangeName) const
159 {
160  for (const auto c : _multiCat->inputCatList()) {
161  auto cat = static_cast<RooAbsCategoryLValue*>(c);
162  if (!cat->inRange(rangeName)) {
163  return false;
164  }
165  }
166 
167  return true;
168 }
169 
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Check that any of the input categories has a range with the given name.
173 Bool_t RooSuperCategory::hasRange(const char* rangeName) const
174 {
175  for (const auto c : _multiCat->inputCatList()) {
176  auto cat = static_cast<RooAbsCategoryLValue*>(c);
177  if (cat->hasRange(rangeName)) return true;
178  }
179 
180  return false;
181 }
RooStreamParser.h
c
#define c(i)
Definition: RSha256.hxx:119
RooSuperCategory.h
RooSuperCategory::MakeIterator
TIterator * MakeIterator() const
Make an iterator over all state permutations of the input categories of this supercategory.
Definition: RooSuperCategory.cxx:93
RooSuperCategory::printMultiline
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const override
Print the state of this object to the specified output stream.
Definition: RooSuperCategory.cxx:142
RooMultiCategory::_catSet
RooSetProxy _catSet
Definition: RooMultiCategory.h:53
RooMsgService.h
RooAbsCategory::getCurrentIndex
virtual value_type getCurrentIndex() const
Return index number of current state.
Definition: RooAbsCategory.cxx:114
RooFit.h
RooMultiCatIter
Definition: RooMultiCatIter.h:29
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooArgSet.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
coutE
#define coutE(a)
Definition: RooMsgService.h:33
TClass.h
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
TString
Definition: TString.h:136
RooSuperCategory::setIndex
virtual bool setIndex(value_type index, bool printError=true) override
Set the value of the super category to the specified index.
Definition: RooSuperCategory.cxx:102
TString.h
bool
TIterator
Definition: TIterator.h:30
RooMultiCategory
Definition: RooMultiCategory.h:28
RooSuperCategory::_multiCat
RooTemplateProxy< RooMultiCategory > _multiCat
Definition: RooSuperCategory.h:67
RooSuperCategory::RooSuperCategory
RooSuperCategory()
Definition: RooSuperCategory.cxx:49
RooSuperCategory::setLabel
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE) override
Set the value of the super category by specifying the state name.
Definition: RooSuperCategory.cxx:132
RooMultiCategory::printMultiline
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const override
Print the state of this object to the specified output stream.
Definition: RooMultiCategory.cxx:166
RooMultiCategory::inputCatList
const RooArgSet & inputCatList() const
Definition: RooMultiCategory.h:44
RooAbsCategory::printMultiline
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
Definition: RooAbsCategory.cxx:406
RooSuperCategory::inRange
virtual Bool_t inRange(const char *rangeName) const override
Check that all input category states are in the given range.
Definition: RooSuperCategory.cxx:158
RooAbsArg::setShapeDirty
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:510
RooSuperCategory::hasRange
virtual Bool_t hasRange(const char *rangeName) const override
Check that any of the input categories has a range with the given name.
Definition: RooSuperCategory.cxx:173
RooAbsCategoryLValue.h
RooSuperCategory
Definition: RooSuperCategory.h:27
RooAbsCategory::lookupIndex
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Definition: RooAbsCategory.cxx:283
name
char name[80]
Definition: TGX11.cxx:110
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooAbsCategoryLValue
Definition: RooAbsCategoryLValue.h:25
Class
void Class()
Definition: Class.C:29
Riostream.h
RooMultiCatIter.h
RooArgSet
Definition: RooArgSet.h:28
int