Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22A real-to-category mapping defined by a series of thresholds.
23**/
24
25
27#include "RooMsgService.h"
28
29using std::endl, std::ostream;
30
31
32namespace {
33bool threshListSorter(const std::pair<double,RooAbsCategory::value_type>& lhs, const std::pair<double,RooAbsCategory::value_type>& rhs) {
34 return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);
35}
36}
37
38
39
40////////////////////////////////////////////////////////////////////////////////
41/// Constructor with input function to be mapped and name and index of default
42/// output state of unmapped values
43
45 const char* defOut, Int_t defIdx) :
46 RooAbsCategory(name, title),
47 _inputVar("inputVar","Input category",this,inputVar),
48 _defIndex(defIdx)
49{
51}
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Copy constructor
57
60 _inputVar("inputVar",this,other._inputVar),
61 _defIndex(other._defIndex)
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
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" << std::endl ;
81 return true;
82 }
83 }
84
85 // Add a threshold entry
87 if (newIdx == std::numeric_limits<value_type>::min()) {
88 if (catIdx == -99999) {
89 newIdx = defineState(catName).second;
90 } else {
92 }
93 }
94
95 _threshList.emplace_back(upperLimit, newIdx);
96 std::sort(_threshList.begin(), _threshList.end(), threshListSorter);
97
98 return false;
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 _defIndex;
116}
117
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Write object contents to given stream
122
123void RooThresholdCategory::writeToStream(ostream& os, bool compact) const
124{
125 if (compact) {
126 // Write value only
127 os << getCurrentLabel() ;
128 } else {
129 // Write mapping expression
130
131 // Scan list of threshold
132 for (const auto& thresh : _threshList) {
133 os << lookupName(thresh.second) << '[' << thresh.second << "]:<" << thresh.first << " ";
134 }
135 os << lookupName(_defIndex) << '[' << _defIndex << "]:*" ;
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
149void RooThresholdCategory::printMultiline(ostream& os, Int_t content, bool verbose, TString indent) const
150{
152
153 if (verbose) {
154 os << indent << "--- RooThresholdCategory ---" << std::endl
155 << indent << " Maps from " ;
156 _inputVar.arg().printStream(os,0,kStandard);
157
158 os << indent << " Threshold list" << std::endl ;
159 for (const auto& thresh : _threshList) {
160 os << indent << " input < " << thresh.first << " --> " ;
161 os << lookupName(thresh.second) << '[' << thresh.second << "]\n";
162 }
163 os << indent << " Default value is " << lookupName(_defIndex) << '[' << _defIndex << ']' << std::endl;
164 }
165}
166
167
#define coutW(a)
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
A space to attach TBranches.
virtual const char * getCurrentLabel() const
Return label string of current state.
const std::string & lookupName(value_type index) const
Get the name corresponding to the given index.
virtual const std::map< std::string, RooAbsCategory::value_type >::value_type & defineState(const std::string &label)
Define a new state with given label.
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Print info about this object to the specified stream.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
const T & arg() const
Return reference to object held in proxy.
A real-to-category mapping defined by a series of thresholds.
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to given stream.
bool addThreshold(double upperLimit, const char *catName, Int_t catIdx=-99999)
Insert threshold at value upperLimit.
value_type evaluate() const override
Calculate and return the value of the mapping function.
std::vector< std::pair< double, value_type > > _threshList
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print info about this threshold category to the specified stream.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139