38 ClassImp(RooMappedCategory);
39 ClassImp(RooMappedCategory::Entry);
41 class RooMappedCategoryCache :
public RooAbsCache {
43 RooMappedCategoryCache(RooAbsArg* owner = 0) : RooAbsCache(owner)
45 RooMappedCategoryCache(
const RooAbsCache& other, RooAbsArg* owner = 0) :
46 RooAbsCache(other, owner)
50 const RooCatType* lookup(Int_t idx)
const
53 virtual void wireCache()
54 { _map.clear(); initialise(); }
56 virtual Bool_t redirectServersHook(
const RooAbsCollection& , Bool_t , Bool_t , Bool_t )
57 { _map.clear(); initialise();
return kFALSE; }
60 mutable std::map<Int_t, const RooCatType*> _map;
66 const RooMappedCategory& parent = *
static_cast<const RooMappedCategory*
>(_owner);
67 std::unique_ptr<TIterator> tit(static_cast<const RooAbsCategory&>(
68 parent._inputCat.arg()).typeIterator());
69 for (
const RooCatType* inCat = static_cast<const RooCatType*>(tit->Next());
70 inCat; inCat =
static_cast<const RooCatType*
>(tit->Next())) {
71 const char* inKey = inCat->GetName();
74 for (std::map<std::string, RooMappedCategory::Entry>::const_iterator
75 iter = parent._mapArray.begin(),
76 end = parent._mapArray.end(); end != iter; ++iter) {
77 if (iter->second.match(inKey)) {
79 _map[inCat->getVal()] = &(iter->second.outCat());
83 if (!found) _map[inCat->getVal()] = parent._defCat;
88 RooMappedCategory::RooMappedCategory(
const char *name,
const char *title, RooAbsCategory& inputCat,
const char* defOut, Int_t defOutIdx) :
89 RooAbsCategory(name, title), _inputCat(
"input",
"Input category",this,inputCat),
94 if (defOutIdx==NoCatIdx) {
95 _defCat = (RooCatType*) defineType(defOut) ;
97 _defCat = (RooCatType*) defineType(defOut,defOutIdx) ;
102 RooMappedCategory::RooMappedCategory(
const RooMappedCategory& other,
const char *name) :
103 RooAbsCategory(other,name), _inputCat(
"input",this,other._inputCat), _mapArray(other._mapArray),
106 _defCat = (RooCatType*) lookupType(other._defCat->GetName()) ;
111 RooMappedCategory::~RooMappedCategory()
119 Bool_t RooMappedCategory::map(
const char* inKeyRegExp,
const char* outKey, Int_t outIdx)
127 if (!inKeyRegExp || !outKey)
return kTRUE ;
130 if (_mapArray.find(inKeyRegExp)!=_mapArray.end()) {
131 coutE(InputArguments) <<
"RooMappedCategory::map(" << GetName() <<
"): ERROR expression "
132 << inKeyRegExp <<
" already mapped" << std::endl ;
137 const RooCatType* outType = lookupType(outKey) ;
139 if (outIdx==NoCatIdx) {
140 outType = defineType(outKey) ;
142 outType = defineType(outKey,outIdx) ;
146 coutE(InputArguments) <<
"RooMappedCategory::map(" << GetName()
147 <<
"): ERROR, unable to output type " << outKey << std::endl ;
152 Entry e(inKeyRegExp,outType) ;
154 coutE(InputArguments) <<
"RooMappedCategory::map(" << GetName()
155 <<
"): ERROR, expression " << inKeyRegExp <<
" didn't compile" << std::endl ;
159 _mapArray[inKeyRegExp] = e ;
165 RooCatType RooMappedCategory::evaluate()
const
167 const RooMappedCategoryCache* cache = getOrCreateCache();
168 return *(cache->lookup(Int_t(_inputCat)));
171 const RooMappedCategoryCache* RooMappedCategory::getOrCreateCache()
const
173 if (!_mapcache) _mapcache =
new RooMappedCategoryCache(
174 const_cast<RooMappedCategory*>(
this));
178 void RooMappedCategory::printMultiline(std::ostream& os, Int_t content, Bool_t verbose, TString indent)
const
187 RooAbsCategory::printMultiline(os,content,verbose,indent);
190 os << indent <<
"--- RooMappedCategory ---" << std::endl
191 << indent <<
" Maps from " ;
192 _inputCat.arg().printStream(os,0,kStandard);
194 os << indent <<
" Default value is ";
195 _defCat->printStream(os,kName|kValue,kSingleLine);
197 os << indent <<
" Mapping rules:" << std::endl;
198 for (std::map<std::string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; ++iter) {
199 os << indent <<
" " << iter->first <<
" -> " << iter->second.outCat().GetName() << std::endl ;
205 Bool_t RooMappedCategory::readFromStream(std::istream& is, Bool_t compact, Bool_t )
209 coutE(InputArguments) <<
"RooMappedCategory::readFromSteam(" << GetName() <<
"): can't read in compact mode" << std::endl ;
214 TString defCatName(_defCat->GetName()) ;
219 _defCat = (RooCatType*) defineType(defCatName) ;
221 TString token,errorPrefix(
"RooMappedCategory::readFromStream(") ;
222 errorPrefix.Append(GetName()) ;
223 errorPrefix.Append(
")") ;
224 RooStreamParser parser(is,errorPrefix) ;
225 parser.setPunctuation(
":,") ;
227 TString destKey,srcKey ;
228 Bool_t readToken(kTRUE) ;
232 if (readToken) token=parser.readToken() ;
233 if (token.IsNull())
break ;
237 if (parser.expectToken(
":",kTRUE))
return kTRUE ;
241 srcKey = parser.readToken() ;
242 token = parser.readToken() ;
245 if (map(srcKey,destKey))
return kTRUE ;
249 if (token.CompareTo(
",")) {
265 void RooMappedCategory::printMetaArgs(std::ostream& os)
const
268 RooCatType prevOutCat ;
269 Bool_t first(kTRUE) ;
271 for (std::map<std::string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; ++iter) {
272 if (iter->second.outCat().getVal()!=prevOutCat.getVal()) {
273 if (!first) { os <<
" " ; }
276 os << iter->second.outCat().GetName() <<
":" << iter->first ;
277 prevOutCat=iter->second.outCat() ;
279 os <<
"," << iter->first ;
283 if (!first) { os <<
" " ; }
284 os << _defCat->GetName() <<
":*" ;
292 void RooMappedCategory::writeToStream(std::ostream& os, Bool_t compact)
const
302 RooCatType prevOutCat ;
303 Bool_t first(kTRUE) ;
304 for (std::map<std::string,Entry>::const_iterator iter = _mapArray.begin() ; iter!=_mapArray.end() ; ++iter) {
305 if (iter->second.outCat().getVal()!=prevOutCat.getVal()) {
306 if (!first) { os <<
" " ; }
309 os << iter->second.outCat().GetName() <<
"<-" << iter->first ;
310 prevOutCat=iter->second.outCat() ;
312 os <<
"," << iter->first ;
316 if (!first) { os <<
" " ; }
317 os << _defCat->GetName() <<
":*" ;
326 RooMappedCategory::Entry& RooMappedCategory::Entry::operator=(
const RooMappedCategory::Entry& other)
328 if (&other==
this)
return *this ;
330 _expr = other._expr ;
336 _regexp =
new TRegexp(_expr.Data(),kTRUE) ;
346 TString RooMappedCategory::Entry::mangle(
const char* exp)
const
349 const char *c = exp ;
351 if (*c==
'+') t.Append(
'\\') ;
362 void RooMappedCategory::Entry::Streamer(TBuffer &R__b)
364 typedef ::RooMappedCategory::Entry ThisClass;
367 if (R__b.IsReading()) {
370 R__b.ReadVersion(&R__s, &R__c);
374 _cat.Streamer(R__b) ;
375 _regexp =
new TRegexp(_expr.Data(),kTRUE) ;
376 R__b.CheckByteCount(R__s, R__c, ThisClass::IsA());
381 R__c = R__b.WriteVersion(ThisClass::IsA(), kTRUE);
385 _cat.Streamer(R__b) ;
387 R__b.SetByteCount(R__c, kTRUE);