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