Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 Class 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 
32 ClassImp(RooThresholdCategory);
33 
34 namespace {
35 bool 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 
46 RooThresholdCategory::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 
58 RooThresholdCategory::RooThresholdCategory(const RooThresholdCategory& other, const char *name) :
59  RooAbsCategory(other,name), _inputVar("inputVar",this,other._inputVar)
60 {
61  _defCat = lookupType(other._defCat->GetName());
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 
74 Bool_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 
106 RooCatType RooThresholdCategory::evaluate() const
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 
123 void 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 
149 void RooThresholdCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
150 {
151  RooAbsCategory::printMultiline(os,content,verbose,indent);
152 
153  if (verbose) {
154  os << indent << "--- RooThresholdCategory ---" << endl
155  << indent << " Maps from " ;
156  _inputVar.arg().printStream(os,0,kStandard);
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 " ;
164  _defCat->printStream(os,kValue,kSingleLine);
165 
166 
167  }
168 }
169 
170