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