ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RooMappedCategory.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 // -- CLASS DESCRIPTION [CAT] --
18 // RooMappedCategory provides a category-to-category mapping defined
19 // by pattern matching on their state labels
20 //
21 // The mapping function consists of a series of wild card regular expressions.
22 // Each expression is matched to the input categories state labels, and an associated
23 // output state label.
24 
25 
26 #include "RooMappedCategory.h"
27 
28 #include "RooFit.h"
29 #include "RooStreamParser.h"
30 #include "RooMsgService.h"
31 
32 #include "Riostream.h"
33 #include "TBuffer.h"
34 #include "TString.h"
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 
39 using namespace std ;
40 
43 
44 
45 RooMappedCategory::RooMappedCategory(const char *name, const char *title, RooAbsCategory& inputCat, const char* defOut, Int_t defOutIdx) :
46  RooAbsCategory(name, title), _inputCat("input","Input category",this,inputCat)
47 {
48  // Constructor with input category and name of default output state, which is assigned
49  // to all input category states that do not follow any mapping rule.
50  if (defOutIdx==NoCatIdx) {
51  _defCat = (RooCatType*) defineType(defOut) ;
52  } else {
53  _defCat = (RooCatType*) defineType(defOut,defOutIdx) ;
54  }
55 }
56 
57 
59  RooAbsCategory(other,name), _inputCat("input",this,other._inputCat), _mapArray(other._mapArray)
60 {
62 }
63 
64 
65 
67 {
68  // Destructor
69 }
70 
71 
72 
73 Bool_t RooMappedCategory::map(const char* inKeyRegExp, const char* outKey, Int_t outIdx)
74 {
75  // Add mapping rule: any input category state label matching the 'inKeyRegExp'
76  // wildcard expression will be mapped to an output state with name 'outKey'
77  //
78  // Rules are evaluated in the order they were added. In case an input state
79  // matches more than one rule, the first rules output state will be assigned
80 
81  if (!inKeyRegExp || !outKey) return kTRUE ;
82 
83  // Check if pattern is already registered
84  if (_mapArray.find(inKeyRegExp)!=_mapArray.end()) {
85  coutE(InputArguments) << "RooMappedCategory::map(" << GetName() << "): ERROR expression "
86  << inKeyRegExp << " already mapped" << endl ;
87  return kTRUE ;
88  }
89 
90  // Check if output type exists, if not register
91  const RooCatType* outType = lookupType(outKey) ;
92  if (!outType) {
93  if (outIdx==NoCatIdx) {
94  outType = defineType(outKey) ;
95  } else {
96  outType = defineType(outKey,outIdx) ;
97  }
98  }
99  if (!outType) {
100  coutE(InputArguments) << "RooMappedCategory::map(" << GetName()
101  << "): ERROR, unable to output type " << outKey << endl ;
102  return kTRUE ;
103  }
104 
105  // Create new map entry ;
106  Entry e(inKeyRegExp,outType) ;
107  if (!e.ok()) {
108  coutE(InputArguments) << "RooMappedCategory::map(" << GetName()
109  << "): ERROR, expression " << inKeyRegExp << " didn't compile" << endl ;
110  return kTRUE ;
111  }
112 
113  _mapArray[inKeyRegExp] = e ;
114  return kFALSE ;
115 }
116 
117 
118 
120 {
121  // Calculate the current value of the object
122  const char* inKey = _inputCat.label() ;
123 
124  // Scan array of regexps
125  for ( std::map<string,Entry>::const_iterator iter = _mapArray.begin() ; iter != _mapArray.end() ; iter++) {
126  if (iter->second.match(inKey)) {
127  return iter->second.outCat() ;
128  }
129  }
130 
131  // Return default if nothing found
132  return *_defCat ;
133 }
134 
135 
137 {
138  // Print info about this mapped category to the specified stream. In addition to the info
139  // from RooAbsCategory::printStream() we add:
140  //
141  // Standard : input category
142  // Shape : default value
143  // Verbose : list of mapping rules
144 
145  RooAbsCategory::printMultiline(os,content,verbose,indent);
146 
147  if (verbose) {
148  os << indent << "--- RooMappedCategory ---" << endl
149  << indent << " Maps from " ;
151 
152  os << indent << " Default value is ";
154 
155  os << indent << " Mapping rules:" << endl;
156  for (std::map<string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; iter++) {
157  os << indent << " " << iter->first << " -> " << iter->second.outCat().GetName() << endl ;
158  }
159  }
160 }
161 
162 
163 Bool_t RooMappedCategory::readFromStream(istream& is, Bool_t compact, Bool_t /*verbose*/)
164 {
165  // Read object contents from given stream
166  if (compact) {
167  coutE(InputArguments) << "RooMappedCategory::readFromSteam(" << GetName() << "): can't read in compact mode" << endl ;
168  return kTRUE ;
169  } else {
170 
171  //Clear existing definitions, but preserve default output
172  TString defCatName(_defCat->GetName()) ;
173  _mapArray.clear() ;
174  clearTypes() ;
175  _defCat = (RooCatType*) defineType(defCatName) ;
176 
177  TString token,errorPrefix("RooMappedCategory::readFromStream(") ;
178  errorPrefix.Append(GetName()) ;
179  errorPrefix.Append(")") ;
180  RooStreamParser parser(is,errorPrefix) ;
181  parser.setPunctuation(":,") ;
182 
183  TString destKey,srcKey ;
184  Bool_t readToken(kTRUE) ;
185 
186  // Loop over definition sequences
187  while(1) {
188  if (readToken) token=parser.readToken() ;
189  if (token.IsNull()) break ;
190  readToken=kTRUE ;
191 
192  destKey = token ;
193  if (parser.expectToken(":",kTRUE)) return kTRUE ;
194 
195  // Loop over list of sources for this destination
196  while(1) {
197  srcKey = parser.readToken() ;
198  token = parser.readToken() ;
199 
200  // Map a value
201  if (map(srcKey,destKey)) return kTRUE ;
202 
203  // Unless next token is ',' current token
204  // is destination part of next sequence
205  if (token.CompareTo(",")) {
206  readToken = kFALSE ;
207  break ;
208  }
209  }
210  }
211  return kFALSE ;
212  }
213  //return kFALSE ; // statement unreachable (OSF)
214 }
215 
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Customized printing of arguments of a RooMappedCategory to more intuitively reflect the contents of the
219 /// product operator construction
220 
221 void RooMappedCategory::printMetaArgs(ostream& os) const
222 {
223  // Scan array of regexps
224  RooCatType prevOutCat ;
225  Bool_t first(kTRUE) ;
226  os << "map=(" ;
227  for (std::map<string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; iter++) {
228  if (iter->second.outCat().getVal()!=prevOutCat.getVal()) {
229  if (!first) { os << " " ; }
230  first=kFALSE ;
231 
232  os << iter->second.outCat().GetName() << ":" << iter->first ;
233  prevOutCat=iter->second.outCat() ;
234  } else {
235  os << "," << iter->first ;
236  }
237  }
238 
239  if (!first) { os << " " ; }
240  os << _defCat->GetName() << ":*" ;
241 
242  os << ") " ;
243 }
244 
245 
246 
247 
248 void RooMappedCategory::writeToStream(ostream& os, Bool_t compact) const
249 {
250  // Write object contents to given stream
251  if (compact) {
252  // Write value only
253  os << getLabel() ;
254  } else {
255  // Write mapping expression
256 
257  // Scan array of regexps
258  RooCatType prevOutCat ;
259  Bool_t first(kTRUE) ;
260  for (std::map<string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; iter++) {
261  if (iter->second.outCat().getVal()!=prevOutCat.getVal()) {
262  if (!first) { os << " " ; }
263  first=kFALSE ;
264 
265  os << iter->second.outCat().GetName() << "<-" << iter->first ;
266  prevOutCat=iter->second.outCat() ;
267  } else {
268  os << "," << iter->first ;
269  }
270  }
271 
272  if (!first) { os << " " ; }
273  os << _defCat->GetName() << ":*" ;
274  }
275 }
276 
277 
278 
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 
283 {
284  if (&other==this) return *this ;
285 
286  _expr = other._expr ;
287  _cat = other._cat ;
288 
289  if (_regexp) {
290  delete _regexp ;
291  }
292  _regexp = new TRegexp(_expr.Data(),kTRUE) ;
293 
294  return *this;
295 }
296 
297 
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 /// Mangle name : escape regexp character '+'
301 
303 {
304  TString t ;
305  const char *c = exp ;
306  while(*c) {
307  if (*c=='+') t.Append('\\') ;
308  t.Append(*c) ;
309  c++ ;
310  }
311  return t ;
312 }
313 
314 
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 
318 void RooMappedCategory::Entry::Streamer(TBuffer &R__b)
319 {
320  typedef ::RooMappedCategory::Entry ThisClass;
321 
322  // Stream an object of class RooWorkspace::CodeRepo.
323  if (R__b.IsReading()) {
324 
325  UInt_t R__s, R__c;
326  R__b.ReadVersion(&R__s, &R__c);
327 
328  // Stream contents of ClassFiles map
329  R__b >> _expr ;
330  _cat.Streamer(R__b) ;
331  _regexp = new TRegexp(_expr.Data(),kTRUE) ;
332  R__b.CheckByteCount(R__s, R__c, ThisClass::IsA());
333 
334  } else {
335 
336  UInt_t R__c;
337  R__c = R__b.WriteVersion(ThisClass::IsA(), kTRUE);
338 
339  // Stream contents of ClassRelInfo map
340  R__b << _expr ;
341  _cat.Streamer(R__b) ;
342 
343  R__b.SetByteCount(R__c, kTRUE);
344 
345  }
346 }
#define coutE(a)
Definition: RooMsgService.h:35
Bool_t IsReading() const
Definition: TBuffer.h:83
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'.
return c
RooCatType * _defCat
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
Regular expression class.
Definition: TRegexp.h:35
void clearTypes()
Delete all currently defined states.
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Entry & operator=(const Entry &other)
TString mangle(const char *exp) const
Mangle name : escape regexp character '+'.
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
virtual RooCatType evaluate() const
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
TString & Append(const char *cs)
Definition: TString.h:492
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state...
Definition: RooCatType.h:23
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.
const RooCatType * defineType(const char *label)
Define a new state with given name.
void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
Bool_t map(const char *inKeyRegExp, const char *outKeyName, Int_t outKeyNum=NoCatIdx)
ClassImp(RooMappedCategory) ClassImp(RooMappedCategory
TThread * t[5]
Definition: threadsh1.C:13
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
TString readToken()
Read one token separated by any of the know punctuation characters This function recognizes and handl...
TClass * IsA() const
unsigned int UInt_t
Definition: RtypesCore.h:42
bool verbose
bool first
Definition: line3Dfit.C:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
Bool_t IsNull() const
Definition: TString.h:387
static void indent(ostringstream &buf, int indent_level)
Bool_t expectToken(const TString &expected, Bool_t zapOnError=kFALSE)
Read the next token and return kTRUE if it is identical to the given 'expected' token.
std::map< std::string, RooMappedCategory::Entry > _mapArray
void printMetaArgs(std::ostream &os) const
Customized printing of arguments of a RooMappedCategory to more intuitively reflect the contents of t...
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream (dummy for now)
void setPunctuation(const TString &punct)
Change list of characters interpreted as punctuation.
Int_t getVal() const
Definition: RooCatType.h:80
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooCategoryProxy _inputCat
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
virtual const char * getLabel() const
Return label string of current state.
const RooAbsCategory & arg() const
double exp(double)
const char * label() const
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:45
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:372
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
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.