/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
// 
// BEGIN_HTML
// RooAbsString is the common abstract base class for objects that represent a
// string value
// 
// Implementation of RooAbsString may be derived, there no interface
// is provided to modify the contents
// END_HTML
//
// 

#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include "TObjString.h"
#include "TH1.h"
#include "TTree.h"

#include "RooArgSet.h"
#include "RooAbsString.h"
#include "RooStringVar.h"
#include "RooMsgService.h"

using namespace std;

ClassImp(RooAbsString) 
;


//_____________________________________________________________________________
RooAbsString::RooAbsString() : RooAbsArg(), _len(128) , _value(new char[128])
{
  // Default constructor
}


//_____________________________________________________________________________
RooAbsString::RooAbsString(const char *name, const char *title, Int_t bufLen) : 
  RooAbsArg(name,title), _len(bufLen), _value(new char[bufLen]) 
{
  // Constructor

  setValueDirty() ;
  setShapeDirty() ;
}



//_____________________________________________________________________________
RooAbsString::RooAbsString(const RooAbsString& other, const char* name) : 
  RooAbsArg(other, name), _len(other._len), _value(new char[other._len])
{
  // Copy constructor

  strlcpy(_value,other._value,_len) ;
}



//_____________________________________________________________________________
RooAbsString::~RooAbsString()
{
  // Destructor

  delete[] _value ;
}



//_____________________________________________________________________________
const char* RooAbsString::getVal() const
{
  // Return value of object. Calculated if dirty, otherwise cached value is returned.

  if (isValueDirty()) {
    clearValueDirty() ;
    strlcpy(_value,traceEval(),_len) ;
  } 
  
  return _value ;
}



//_____________________________________________________________________________
Bool_t RooAbsString::operator==(const char* value) const
{
  // Equality operator comparing with a TString

  return !TString(getVal()).CompareTo(value) ;
}


//_____________________________________________________________________________
Bool_t RooAbsString::isIdentical(const RooAbsArg& other, Bool_t assumeSameType)  
{
  if (!assumeSameType) {
    const RooAbsString* otherString = dynamic_cast<const RooAbsString*>(&other) ;
    return otherString ? operator==(otherString->getVal()) : kFALSE ;
  } else {
    return !TString(getVal()).CompareTo(((RooAbsString&)other).getVal()) ; ;
  }
}



//_____________________________________________________________________________
Bool_t RooAbsString::operator==(const RooAbsArg& other) 
{
  // Equality operator comparing to another RooAbsArg

  const RooAbsString* otherString = dynamic_cast<const RooAbsString*>(&other) ;
  return otherString ? operator==(otherString->getVal()) : kFALSE ;
}



//_____________________________________________________________________________
Bool_t RooAbsString::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/) 
{
  //Read object contents from stream (dummy for now)
  return kFALSE ;
} 



//_____________________________________________________________________________
void RooAbsString::writeToStream(ostream& /*os*/, Bool_t /*compact*/) const
{
  //Write object contents to stream (dummy for now)
}



//_____________________________________________________________________________
void RooAbsString::printValue(ostream& os) const
{
  // Print value
  os << getVal() ;
}



//_____________________________________________________________________________
Bool_t RooAbsString::isValid() const 
{
  // Check if current value is valid
  return isValidString(getVal()) ;
}



//_____________________________________________________________________________
Bool_t RooAbsString::isValidString(const char* value, Bool_t /*printError*/) const 
{
  // Check if given string value is valid

  // Protect against string overflows
  if (TString(value).Length()>_len) return kFALSE ;

  return kTRUE ;
}


//_____________________________________________________________________________
Bool_t RooAbsString::traceEvalHook(const char* /*value*/) const 
{ 
  // Hook function for trace evaluation
  return kFALSE ; 
}



//_____________________________________________________________________________
const char* RooAbsString::traceEval() const
{
  // Calculate current value of object, with error tracing wrapper

  const char* value = evaluate() ;
  
  //Standard tracing code goes here
  if (!isValidString(value)) {
    cxcoutD(Tracing) << "RooAbsString::traceEval(" << GetName() << "): new output too long (>" << _len << " chars): " << value << endl ;
  }

  //Call optional subclass tracing code
  traceEvalHook(value) ;

  return value ;
}



//_____________________________________________________________________________
void RooAbsString::syncCache(const RooArgSet*) 
{ 
  // Forcibly bring internal cache up-to-date
  getVal() ; 
}



//_____________________________________________________________________________
void RooAbsString::copyCache(const RooAbsArg* source, Bool_t /*valueOnly*/, Bool_t setValDirty) 
{
  // Copy cache of another RooAbsArg to our cache
  //
  // Warning: This function copies the cached values of source,
  //          it is the callers responsibility to make sure the cache is clean

  RooAbsString* other = dynamic_cast<RooAbsString*>(const_cast<RooAbsArg*>(source)) ;
  assert(other!=0) ;

  strlcpy(_value,other->_value,_len) ;
  if (setValDirty) {
    setValueDirty() ;
  }
}



//_____________________________________________________________________________
void RooAbsString::attachToTree(TTree& t, Int_t bufSize)
{
  // Attach object to a branch of given TTree

  // First determine if branch is taken
  TBranch* branch ;
  if ((branch = t.GetBranch(GetName()))) {
    t.SetBranchAddress(GetName(),_value) ;
    if (branch->GetCompressionLevel()<0) {
      cxcoutD(DataHandling) << "RooAbsString::attachToTree(" << GetName() << ") Fixing compression level of branch " << GetName() << endl ;
      branch->SetCompressionLevel(1) ;
    }
  } else {
    TString format(GetName());
    format.Append("/C");
    branch = t.Branch(GetName(), _value, (const Text_t*)format, bufSize);
    branch->SetCompressionLevel(1) ;
  }
}
 


//_____________________________________________________________________________
void RooAbsString::fillTreeBranch(TTree& t) 
{
  // Fill tree branch associated with this object

  // First determine if branch is taken
  TBranch* branch = t.GetBranch(GetName()) ;
  if (!branch) { 
    coutE(DataHandling) << "RooAbsString::fillTreeBranch(" << GetName() << ") ERROR: not attached to tree" << endl ;
    assert(0) ;
  }
  branch->Fill() ;  
}



//_____________________________________________________________________________
void RooAbsString::setTreeBranchStatus(TTree& t, Bool_t active) 
{
  // (De)Activate associated tree branch

  TBranch* branch = t.GetBranch(GetName()) ;
  if (branch) { 
    t.SetBranchStatus(GetName(),active?1:0) ;
  }
}



//_____________________________________________________________________________
RooAbsArg *RooAbsString::createFundamental(const char* newname) const 
{
  // Create a RooStringVar fundamental object with our properties.

  RooStringVar *fund= new RooStringVar(newname?newname:GetName(),GetTitle(),"") ; 
  return fund;
}
 RooAbsString.cxx:1
 RooAbsString.cxx:2
 RooAbsString.cxx:3
 RooAbsString.cxx:4
 RooAbsString.cxx:5
 RooAbsString.cxx:6
 RooAbsString.cxx:7
 RooAbsString.cxx:8
 RooAbsString.cxx:9
 RooAbsString.cxx:10
 RooAbsString.cxx:11
 RooAbsString.cxx:12
 RooAbsString.cxx:13
 RooAbsString.cxx:14
 RooAbsString.cxx:15
 RooAbsString.cxx:16
 RooAbsString.cxx:17
 RooAbsString.cxx:18
 RooAbsString.cxx:19
 RooAbsString.cxx:20
 RooAbsString.cxx:21
 RooAbsString.cxx:22
 RooAbsString.cxx:23
 RooAbsString.cxx:24
 RooAbsString.cxx:25
 RooAbsString.cxx:26
 RooAbsString.cxx:27
 RooAbsString.cxx:28
 RooAbsString.cxx:29
 RooAbsString.cxx:30
 RooAbsString.cxx:31
 RooAbsString.cxx:32
 RooAbsString.cxx:33
 RooAbsString.cxx:34
 RooAbsString.cxx:35
 RooAbsString.cxx:36
 RooAbsString.cxx:37
 RooAbsString.cxx:38
 RooAbsString.cxx:39
 RooAbsString.cxx:40
 RooAbsString.cxx:41
 RooAbsString.cxx:42
 RooAbsString.cxx:43
 RooAbsString.cxx:44
 RooAbsString.cxx:45
 RooAbsString.cxx:46
 RooAbsString.cxx:47
 RooAbsString.cxx:48
 RooAbsString.cxx:49
 RooAbsString.cxx:50
 RooAbsString.cxx:51
 RooAbsString.cxx:52
 RooAbsString.cxx:53
 RooAbsString.cxx:54
 RooAbsString.cxx:55
 RooAbsString.cxx:56
 RooAbsString.cxx:57
 RooAbsString.cxx:58
 RooAbsString.cxx:59
 RooAbsString.cxx:60
 RooAbsString.cxx:61
 RooAbsString.cxx:62
 RooAbsString.cxx:63
 RooAbsString.cxx:64
 RooAbsString.cxx:65
 RooAbsString.cxx:66
 RooAbsString.cxx:67
 RooAbsString.cxx:68
 RooAbsString.cxx:69
 RooAbsString.cxx:70
 RooAbsString.cxx:71
 RooAbsString.cxx:72
 RooAbsString.cxx:73
 RooAbsString.cxx:74
 RooAbsString.cxx:75
 RooAbsString.cxx:76
 RooAbsString.cxx:77
 RooAbsString.cxx:78
 RooAbsString.cxx:79
 RooAbsString.cxx:80
 RooAbsString.cxx:81
 RooAbsString.cxx:82
 RooAbsString.cxx:83
 RooAbsString.cxx:84
 RooAbsString.cxx:85
 RooAbsString.cxx:86
 RooAbsString.cxx:87
 RooAbsString.cxx:88
 RooAbsString.cxx:89
 RooAbsString.cxx:90
 RooAbsString.cxx:91
 RooAbsString.cxx:92
 RooAbsString.cxx:93
 RooAbsString.cxx:94
 RooAbsString.cxx:95
 RooAbsString.cxx:96
 RooAbsString.cxx:97
 RooAbsString.cxx:98
 RooAbsString.cxx:99
 RooAbsString.cxx:100
 RooAbsString.cxx:101
 RooAbsString.cxx:102
 RooAbsString.cxx:103
 RooAbsString.cxx:104
 RooAbsString.cxx:105
 RooAbsString.cxx:106
 RooAbsString.cxx:107
 RooAbsString.cxx:108
 RooAbsString.cxx:109
 RooAbsString.cxx:110
 RooAbsString.cxx:111
 RooAbsString.cxx:112
 RooAbsString.cxx:113
 RooAbsString.cxx:114
 RooAbsString.cxx:115
 RooAbsString.cxx:116
 RooAbsString.cxx:117
 RooAbsString.cxx:118
 RooAbsString.cxx:119
 RooAbsString.cxx:120
 RooAbsString.cxx:121
 RooAbsString.cxx:122
 RooAbsString.cxx:123
 RooAbsString.cxx:124
 RooAbsString.cxx:125
 RooAbsString.cxx:126
 RooAbsString.cxx:127
 RooAbsString.cxx:128
 RooAbsString.cxx:129
 RooAbsString.cxx:130
 RooAbsString.cxx:131
 RooAbsString.cxx:132
 RooAbsString.cxx:133
 RooAbsString.cxx:134
 RooAbsString.cxx:135
 RooAbsString.cxx:136
 RooAbsString.cxx:137
 RooAbsString.cxx:138
 RooAbsString.cxx:139
 RooAbsString.cxx:140
 RooAbsString.cxx:141
 RooAbsString.cxx:142
 RooAbsString.cxx:143
 RooAbsString.cxx:144
 RooAbsString.cxx:145
 RooAbsString.cxx:146
 RooAbsString.cxx:147
 RooAbsString.cxx:148
 RooAbsString.cxx:149
 RooAbsString.cxx:150
 RooAbsString.cxx:151
 RooAbsString.cxx:152
 RooAbsString.cxx:153
 RooAbsString.cxx:154
 RooAbsString.cxx:155
 RooAbsString.cxx:156
 RooAbsString.cxx:157
 RooAbsString.cxx:158
 RooAbsString.cxx:159
 RooAbsString.cxx:160
 RooAbsString.cxx:161
 RooAbsString.cxx:162
 RooAbsString.cxx:163
 RooAbsString.cxx:164
 RooAbsString.cxx:165
 RooAbsString.cxx:166
 RooAbsString.cxx:167
 RooAbsString.cxx:168
 RooAbsString.cxx:169
 RooAbsString.cxx:170
 RooAbsString.cxx:171
 RooAbsString.cxx:172
 RooAbsString.cxx:173
 RooAbsString.cxx:174
 RooAbsString.cxx:175
 RooAbsString.cxx:176
 RooAbsString.cxx:177
 RooAbsString.cxx:178
 RooAbsString.cxx:179
 RooAbsString.cxx:180
 RooAbsString.cxx:181
 RooAbsString.cxx:182
 RooAbsString.cxx:183
 RooAbsString.cxx:184
 RooAbsString.cxx:185
 RooAbsString.cxx:186
 RooAbsString.cxx:187
 RooAbsString.cxx:188
 RooAbsString.cxx:189
 RooAbsString.cxx:190
 RooAbsString.cxx:191
 RooAbsString.cxx:192
 RooAbsString.cxx:193
 RooAbsString.cxx:194
 RooAbsString.cxx:195
 RooAbsString.cxx:196
 RooAbsString.cxx:197
 RooAbsString.cxx:198
 RooAbsString.cxx:199
 RooAbsString.cxx:200
 RooAbsString.cxx:201
 RooAbsString.cxx:202
 RooAbsString.cxx:203
 RooAbsString.cxx:204
 RooAbsString.cxx:205
 RooAbsString.cxx:206
 RooAbsString.cxx:207
 RooAbsString.cxx:208
 RooAbsString.cxx:209
 RooAbsString.cxx:210
 RooAbsString.cxx:211
 RooAbsString.cxx:212
 RooAbsString.cxx:213
 RooAbsString.cxx:214
 RooAbsString.cxx:215
 RooAbsString.cxx:216
 RooAbsString.cxx:217
 RooAbsString.cxx:218
 RooAbsString.cxx:219
 RooAbsString.cxx:220
 RooAbsString.cxx:221
 RooAbsString.cxx:222
 RooAbsString.cxx:223
 RooAbsString.cxx:224
 RooAbsString.cxx:225
 RooAbsString.cxx:226
 RooAbsString.cxx:227
 RooAbsString.cxx:228
 RooAbsString.cxx:229
 RooAbsString.cxx:230
 RooAbsString.cxx:231
 RooAbsString.cxx:232
 RooAbsString.cxx:233
 RooAbsString.cxx:234
 RooAbsString.cxx:235
 RooAbsString.cxx:236
 RooAbsString.cxx:237
 RooAbsString.cxx:238
 RooAbsString.cxx:239
 RooAbsString.cxx:240
 RooAbsString.cxx:241
 RooAbsString.cxx:242
 RooAbsString.cxx:243
 RooAbsString.cxx:244
 RooAbsString.cxx:245
 RooAbsString.cxx:246
 RooAbsString.cxx:247
 RooAbsString.cxx:248
 RooAbsString.cxx:249
 RooAbsString.cxx:250
 RooAbsString.cxx:251
 RooAbsString.cxx:252
 RooAbsString.cxx:253
 RooAbsString.cxx:254
 RooAbsString.cxx:255
 RooAbsString.cxx:256
 RooAbsString.cxx:257
 RooAbsString.cxx:258
 RooAbsString.cxx:259
 RooAbsString.cxx:260
 RooAbsString.cxx:261
 RooAbsString.cxx:262
 RooAbsString.cxx:263
 RooAbsString.cxx:264
 RooAbsString.cxx:265
 RooAbsString.cxx:266
 RooAbsString.cxx:267
 RooAbsString.cxx:268
 RooAbsString.cxx:269
 RooAbsString.cxx:270
 RooAbsString.cxx:271
 RooAbsString.cxx:272
 RooAbsString.cxx:273
 RooAbsString.cxx:274
 RooAbsString.cxx:275
 RooAbsString.cxx:276
 RooAbsString.cxx:277
 RooAbsString.cxx:278
 RooAbsString.cxx:279
 RooAbsString.cxx:280
 RooAbsString.cxx:281
 RooAbsString.cxx:282
 RooAbsString.cxx:283
 RooAbsString.cxx:284
 RooAbsString.cxx:285
 RooAbsString.cxx:286
 RooAbsString.cxx:287
 RooAbsString.cxx:288
 RooAbsString.cxx:289
 RooAbsString.cxx:290
 RooAbsString.cxx:291
 RooAbsString.cxx:292
 RooAbsString.cxx:293
 RooAbsString.cxx:294
 RooAbsString.cxx:295
 RooAbsString.cxx:296
 RooAbsString.cxx:297
 RooAbsString.cxx:298
 RooAbsString.cxx:299