ROOT  6.06/09
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 //
19 // BEGIN_HTML
20 // Class RooThresholdCategory provides a real-to-category mapping defined
21 // by a series of thresholds.
22 // END_HTML
23 //
24 
25 
26 #include "RooFit.h"
27 
28 #include "Riostream.h"
29 #include "Riostream.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include "TString.h"
33 #include "RooThresholdCategory.h"
34 #include "RooStreamParser.h"
35 #include "RooThreshEntry.h"
36 #include "RooMsgService.h"
37 
38 using namespace std;
39 
41 
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Constructor with input function to be mapped and name and index of default
46 /// output state of unmapped values
47 
48 RooThresholdCategory::RooThresholdCategory(const char *name, const char *title, RooAbsReal& inputVar,
49  const char* defOut, Int_t defIdx) :
50  RooAbsCategory(name, title), _inputVar("inputVar","Input category",this,inputVar)
51 {
52  _defCat = (RooCatType*) defineType(defOut,defIdx) ;
53  _threshIter = _threshList.MakeIterator() ;
54 }
55 
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Copy constructor
60 
62  RooAbsCategory(other,name), _inputVar("inputVar",this,other._inputVar)
63 {
65 
66  other._threshIter->Reset() ;
67  RooThreshEntry* te ;
68  while((te=(RooThreshEntry*)other._threshIter->Next())) {
69  _threshList.Add(new RooThreshEntry(*te)) ;
70  }
71 
73 }
74 
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Destructor
79 
81 {
83  delete _threshIter ;
84 }
85 
86 
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Insert threshold at value upperLimit. All values below upper limit (and above any lower
90 /// thresholds, if any) will be mapped to a state name 'catName' with index 'catIdx'
91 
92 Bool_t RooThresholdCategory::addThreshold(Double_t upperLimit, const char* catName, Int_t catIdx)
93 {
94  // Check if identical threshold values is not defined yet
95  _threshIter->Reset() ;
96  RooThreshEntry* te ;
97  while ((te=(RooThreshEntry*)_threshIter->Next())) {
98  if (te->thresh() == upperLimit) {
99  coutW(InputArguments) << "RooThresholdCategory::addThreshold(" << GetName()
100  << ") threshold at " << upperLimit << " already defined" << endl ;
101  return kTRUE ;
102  }
103  }
104 
105 
106  // Add a threshold entry
107  const RooCatType* type = lookupType(catName,kFALSE) ;
108  if (!type) {
109  if (catIdx==-99999) {
110  type=defineType(catName) ;
111  } else {
112  type=defineType(catName,catIdx) ;
113  }
114  }
115  te = new RooThreshEntry(upperLimit,*type) ;
116  _threshList.Add(te) ;
117 
118  return kFALSE ;
119 }
120 
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Calculate and return the value of the mapping function
125 
127 {
128  // Scan the threshold list
129  _threshIter->Reset() ;
130  RooThreshEntry* te ;
131  while((te=(RooThreshEntry*)_threshIter->Next())) {
132  if (_inputVar<te->thresh()) return te->cat() ;
133  }
134 
135  // Return default if nothing found
136  return *_defCat ;
137 }
138 
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Write object contents to given stream
143 
144 void RooThresholdCategory::writeToStream(ostream& os, Bool_t compact) const
145 {
146  if (compact) {
147  // Write value only
148  os << getLabel() ;
149  } else {
150  // Write mapping expression
151 
152  // Scan list of threshold
153  _threshIter->Reset() ;
154  RooThreshEntry* te ;
155  while((te=(RooThreshEntry*)_threshIter->Next())) {
156  os << te->cat().GetName() << ":<" << te->thresh() << " " ;
157  }
158  os << _defCat->GetName() << ":*" ;
159  }
160 }
161 
162 
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Print info about this threshold category to the specified stream. In addition to the info
166 /// from RooAbsCategory::printStream() we add:
167 ///
168 /// Standard : input category
169 /// Shape : default value
170 /// Verbose : list of thresholds
171 
173 {
174  RooAbsCategory::printMultiline(os,content,verbose,indent);
175 
176  if (verbose) {
177  os << indent << "--- RooThresholdCategory ---" << endl
178  << indent << " Maps from " ;
180 
181  os << indent << " Threshold list" << endl ;
182  _threshIter->Reset() ;
183  RooThreshEntry* te ;
184  while((te=(RooThreshEntry*)_threshIter->Next())) {
185  os << indent << " input < " << te->thresh() << " --> " ;
187  }
188  os << indent << " Default value is " ;
190 
191 
192  }
193 }
194 
195 
virtual ~RooThresholdCategory()
Destructor.
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
virtual void Reset()=0
ClassImp(RooThresholdCategory) RooThresholdCategory
Constructor with input function to be mapped and name and index of default output state of unmapped v...
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, which is interpreted as an OR of 'enum ContentsOptions' values and in the style given by 'enum StyleOption'.
Double_t thresh() const
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:34
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
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.
virtual RooCatType evaluate() const
do not persist
Bool_t addThreshold(Double_t upperLimit, const char *catName, Int_t catIdx=-99999)
Insert threshold at value upperLimit.
const RooCatType * defineType(const char *label)
Define a new state with given name.
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void indent(ostringstream &buf, int indent_level)
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
int type
Definition: TGX11.cxx:120
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual TObject * Next()=0
virtual const char * getLabel() const
Return label string of current state.
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition: TList.cxx:603
void Add(TObject *obj)
const Bool_t kTRUE
Definition: Rtypes.h:91
const RooCatType & cat() const
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:45
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.
const RooCatType * lookupType(Int_t index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return 0 for no match.