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 
22 The RooThresholdCategory provides a real-to-category mapping defined
23 by a series of thresholds.
24 **/
25 
26 
27 #include "RooThresholdCategory.h"
28 #include "RooMsgService.h"
29 
30 using namespace std;
31 
33 
34 namespace {
35 bool 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 
46 RooThresholdCategory::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 
76 Bool_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 
125 void 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 
152 {
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 
RooThresholdCategory::evaluate
virtual value_type evaluate() const
Calculate and return the value of the mapping function.
Definition: RooThresholdCategory.cxx:108
RooThresholdCategory::_threshList
std::vector< std::pair< double, value_type > > _threshList
Definition: RooThresholdCategory.h:62
RooMsgService.h
RooTemplateProxy::arg
const T & arg() const
Return reference to object held in proxy.
Definition: RooTemplateProxy.h:271
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooPrintable::kStandard
@ kStandard
Definition: RooPrintable.h:34
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooAbsCategory::lookupName
const std::string & lookupName(value_type index) const
Get the name corresponding to the given index.
Definition: RooAbsCategory.cxx:201
coutW
#define coutW(a)
Definition: RooMsgService.h:32
RooThresholdCategory::addThreshold
Bool_t addThreshold(Double_t upperLimit, const char *catName, Int_t catIdx=-99999)
Insert threshold at value upperLimit.
Definition: RooThresholdCategory.cxx:76
RooThresholdCategory.h
RooAbsCategory::defineState
virtual const std::map< std::string, RooAbsCategory::value_type >::value_type & defineState(const std::string &label)
Define a new state with given label.
Definition: RooAbsCategory.cxx:213
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
RooAbsReal
Definition: RooAbsReal.h:61
TString
Definition: TString.h:136
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:130
bool
RooAbsCategory
Definition: RooAbsCategory.h:38
RooThresholdCategory::printMultiline
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.
Definition: RooThresholdCategory.cxx:151
RooThresholdCategory::_defIndex
const value_type _defIndex
Definition: RooThresholdCategory.h:61
RooThresholdCategory::_inputVar
RooRealProxy _inputVar
Definition: RooThresholdCategory.h:60
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
RooThresholdCategory::RooThresholdCategory
RooThresholdCategory()
Definition: RooThresholdCategory.h:44
Double_t
double Double_t
Definition: RtypesCore.h:59
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
RooPrintable::printStream
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,...
Definition: RooPrintable.cxx:75
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooThresholdCategory
Definition: RooThresholdCategory.h:26
RooThresholdCategory::writeToStream
void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
Definition: RooThresholdCategory.cxx:125
int