Logo ROOT  
Reference Guide
RooThresholdCategory.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 RooThresholdCategory.cxx
19\class RooThresholdCategory
20\ingroup Roofitcore
21
22Class RooThresholdCategory provides a real-to-category mapping defined
23by a series of thresholds.
24**/
25
26
28#include "RooMsgService.h"
29
30using namespace std;
31
33
34namespace {
35bool threshListSorter(const std::pair<double,RooCatType>& lhs, const std::pair<double,RooCatType>& rhs) {
36 return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second.getVal() < rhs.second.getVal());
37}
38}
39
40
41
42////////////////////////////////////////////////////////////////////////////////
43/// Constructor with input function to be mapped and name and index of default
44/// output state of unmapped values
45
46RooThresholdCategory::RooThresholdCategory(const char *name, const char *title, RooAbsReal& inputVar,
47 const char* defOut, Int_t defIdx) :
48 RooAbsCategory(name, title), _inputVar("inputVar","Input category",this,inputVar)
49{
50 _defCat = defineType(defOut,defIdx);
51}
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Copy constructor
57
59 RooAbsCategory(other,name), _inputVar("inputVar",this,other._inputVar)
60{
62
63 for (const auto& cat : other._threshList){
64 _threshList.push_back(cat);
65 }
66 std::sort(_threshList.begin(), _threshList.end(), threshListSorter);
67}
68
69
70////////////////////////////////////////////////////////////////////////////////
71/// Insert threshold at value upperLimit. All values below upper limit (and above any lower
72/// thresholds, if any) will be mapped to a state name 'catName' with index 'catIdx'
73
74Bool_t RooThresholdCategory::addThreshold(Double_t upperLimit, const char* catName, Int_t catIdx)
75{
76 // Check if identical threshold values is not defined yet
77 for (const auto& thresh : _threshList) {
78 if (thresh.first == upperLimit) {
79 coutW(InputArguments) << "RooThresholdCategory::addThreshold(" << GetName()
80 << ") threshold at " << upperLimit << " already defined" << endl ;
81 return kTRUE ;
82 }
83 }
84
85 // Add a threshold entry
86 const RooCatType* type = lookupType(catName,kFALSE) ;
87 if (!type) {
88 if (catIdx==-99999) {
89 type=defineType(catName) ;
90 } else {
91 type=defineType(catName,catIdx) ;
92 }
93 }
94
95 _threshList.emplace_back(upperLimit, *type);
96 std::sort(_threshList.begin(), _threshList.end(), threshListSorter);
97
98 return kFALSE ;
99}
100
101
102
103////////////////////////////////////////////////////////////////////////////////
104/// Calculate and return the value of the mapping function
105
107{
108 // Scan the threshold list
109 for (const auto& thresh : _threshList) {
110 if (_inputVar<thresh.first)
111 return thresh.second;
112 }
113
114 // Return default if nothing found
115 return *_defCat;
116}
117
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Write object contents to given stream
122
123void RooThresholdCategory::writeToStream(ostream& os, Bool_t compact) const
124{
125 if (compact) {
126 // Write value only
127 os << getLabel() ;
128 } else {
129 // Write mapping expression
130
131 // Scan list of threshold
132 for (const auto& thresh : _threshList) {
133 os << thresh.second.GetName() << ":<" << thresh.first << " " ;
134 }
135 os << _defCat->GetName() << ":*" ;
136 }
137}
138
139
140
141////////////////////////////////////////////////////////////////////////////////
142/// Print info about this threshold category to the specified stream. In addition to the info
143/// from RooAbsCategory::printStream() we add:
144///
145/// Standard : input category
146/// Shape : default value
147/// Verbose : list of thresholds
148
150{
152
153 if (verbose) {
154 os << indent << "--- RooThresholdCategory ---" << endl
155 << indent << " Maps from " ;
157
158 os << indent << " Threshold list" << endl ;
159 for (const auto& thresh : _threshList) {
160 os << indent << " input < " << thresh.first << " --> " ;
161 thresh.second.printStream(os,kName|kValue,kSingleLine) ;
162 }
163 os << indent << " Default value is " ;
165
166
167 }
168}
169
170
#define coutW(a)
Definition: RooMsgService.h:33
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
virtual const char * getLabel() const
Return label string of current state.
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.
const RooCatType * defineType(const char *label)
Define a new state with given name.
const RooCatType * lookupType(Int_t index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return 0 for no match.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:59
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
Definition: RooCatType.h:22
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:44
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer,...
const T & arg() const
Return reference to object held in proxy.
Class RooThresholdCategory provides a real-to-category mapping defined by a series of thresholds.
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print info about this threshold category to the specified stream.
void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
const RooCatType * _defCat
Bool_t addThreshold(Double_t upperLimit, const char *catName, Int_t catIdx=-99999)
Insert threshold at value upperLimit.
std::vector< std::pair< double, RooCatType > > _threshList
virtual RooCatType evaluate() const
Calculate and return the value of the mapping function.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:131
@ InputArguments
Definition: RooGlobalFunc.h:68