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
32
33namespace {
34bool threshListSorter(const std::pair<double,RooAbsCategory::value_type>& lhs, const std::pair<double,RooAbsCategory::value_type>& rhs) {
35 return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);
36}
37}
38
39
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor with input function to be mapped and name and index of default
43/// output state of unmapped values
44
45RooThresholdCategory::RooThresholdCategory(const char *name, const char *title, RooAbsReal& inputVar,
46 const char* defOut, Int_t defIdx) :
47 RooAbsCategory(name, title),
48 _inputVar("inputVar","Input category",this,inputVar),
49 _defIndex(defIdx)
50{
51 defineState(defOut, defIdx);
52}
53
54
55
56////////////////////////////////////////////////////////////////////////////////
57/// Copy constructor
58
60 RooAbsCategory(other,name),
61 _inputVar("inputVar",this,other._inputVar),
62 _defIndex(other._defIndex)
63{
64 for (const auto& cat : other._threshList){
65 _threshList.push_back(cat);
66 }
67 std::sort(_threshList.begin(), _threshList.end(), threshListSorter);
68}
69
70
71////////////////////////////////////////////////////////////////////////////////
72/// Insert threshold at value upperLimit. All values below upper limit (and above any lower
73/// thresholds, if any) will be mapped to a state name 'catName' with index 'catIdx'
74
75bool RooThresholdCategory::addThreshold(double upperLimit, const char* catName, Int_t catIdx)
76{
77 // Check if identical threshold values is not defined yet
78 for (const auto& thresh : _threshList) {
79 if (thresh.first == upperLimit) {
80 coutW(InputArguments) << "RooThresholdCategory::addThreshold(" << GetName()
81 << ") threshold at " << upperLimit << " already defined" << endl ;
82 return true;
83 }
84 }
85
86 // Add a threshold entry
87 value_type newIdx = lookupIndex(catName);
88 if (newIdx == std::numeric_limits<value_type>::min()) {
89 if (catIdx == -99999) {
90 newIdx = defineState(catName).second;
91 } else {
92 newIdx = defineState(catName, catIdx).second;
93 }
94 }
95
96 _threshList.emplace_back(upperLimit, newIdx);
97 std::sort(_threshList.begin(), _threshList.end(), threshListSorter);
98
99 return false;
100}
101
102
103
104////////////////////////////////////////////////////////////////////////////////
105/// Calculate and return the value of the mapping function
106
108{
109 // Scan the threshold list
110 for (const auto& thresh : _threshList) {
111 if (_inputVar < thresh.first)
112 return thresh.second;
113 }
114
115 // Return default if nothing found
116 return _defIndex;
117}
118
119
120
121////////////////////////////////////////////////////////////////////////////////
122/// Write object contents to given stream
123
124void RooThresholdCategory::writeToStream(ostream& os, bool compact) const
125{
126 if (compact) {
127 // Write value only
128 os << getCurrentLabel() ;
129 } else {
130 // Write mapping expression
131
132 // Scan list of threshold
133 for (const auto& thresh : _threshList) {
134 os << lookupName(thresh.second) << '[' << thresh.second << "]:<" << thresh.first << " ";
135 }
136 os << lookupName(_defIndex) << '[' << _defIndex << "]:*" ;
137 }
138}
139
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// Print info about this threshold category to the specified stream. In addition to the info
144/// from RooAbsCategory::printStream() we add:
145///
146/// Standard : input category
147/// Shape : default value
148/// Verbose : list of thresholds
149
150void RooThresholdCategory::printMultiline(ostream& os, Int_t content, bool verbose, TString indent) const
151{
152 RooAbsCategory::printMultiline(os,content,verbose,indent);
153
154 if (verbose) {
155 os << indent << "--- RooThresholdCategory ---" << endl
156 << indent << " Maps from " ;
158
159 os << indent << " Threshold list" << endl ;
160 for (const auto& thresh : _threshList) {
161 os << indent << " input < " << thresh.first << " --> " ;
162 os << lookupName(thresh.second) << '[' << thresh.second << "]\n";
163 }
164 os << indent << " Default value is " << lookupName(_defIndex) << '[' << _defIndex << ']' << std::endl;
165 }
166}
167
168
#define coutW(a)
#define ClassImp(name)
Definition Rtypes.h:382
static void indent(ostringstream &buf, int indent_level)
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
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.
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