ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id: RooAddition.cxx 28259 2009-04-16 16:21:16Z wouter $
 * 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
// RooAddition calculates the sum of a set of RooAbsReal terms, or
// when constructed with two sets, it sums the product of the terms
// in the two sets. This class does not (yet) do any smart handling of integrals, 
// i.e. all integrals of the product are handled numerically
// END_HTML
//


#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include <math.h>

#include "RooAddition.h"
#include "RooAbsReal.h"
#include "RooAbsPdf.h"
#include "RooErrorHandler.h"
#include "RooArgSet.h"
#include "RooNLLVar.h"
#include "RooChi2Var.h"
#include "RooMsgService.h"

ClassImp(RooAddition)
;


//_____________________________________________________________________________
RooAddition::RooAddition()
{
  _setIter1 = _set1.createIterator() ;
  _setIter2 = _set2.createIterator() ;
}



//_____________________________________________________________________________
RooAddition::RooAddition(const char* name, const char* title, const RooArgSet& sumSet, Bool_t takeOwnership) :
  RooAbsReal(name, title),
  _set1("!set1","First set of components",this),
  _set2("!set2","Second set of components",this)
{
  // Constructor with a single set of RooAbsReals. The value of the function will be
  // the sum of the values in sumSet. If takeOwnership is true the RooAddition object
  // will take ownership of the arguments in sumSet

  _setIter1 = _set1.createIterator() ;
  _setIter2 = 0 ;

  TIterator* inputIter = sumSet.createIterator() ;
  RooAbsArg* comp ;
  while((comp = (RooAbsArg*)inputIter->Next())) {
    if (!dynamic_cast<RooAbsReal*>(comp)) {
      coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp->GetName() 
			    << " is not of type RooAbsReal" << endl ;
      RooErrorHandler::softAbort() ;
    }
    _set1.add(*comp) ;
    if (takeOwnership) {
      _ownedList.addOwned(*comp) ;
    }
  }

  delete inputIter ;
}



//_____________________________________________________________________________
RooAddition::RooAddition(const char* name, const char* title, const RooArgList& sumSet1, const RooArgList& sumSet2, Bool_t takeOwnership) :
  RooAbsReal(name, title),
  _set1("!set1","First set of components",this),
  _set2("!set2","Second set of components",this)
{
  // Constructor with two set of RooAbsReals. The value of the function will be
  //
  //  A = sum_i sumSet1(i)*sumSet2(i) 
  //
  // If takeOwnership is true the RooAddition object will take ownership of the arguments in sumSet

  _setIter1 = _set1.createIterator() ;
  _setIter2 = _set2.createIterator() ;

  if (sumSet1.getSize() != sumSet2.getSize()) {
    coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: input lists should be of equal length" << endl ;
    RooErrorHandler::softAbort() ;    
  }

  TIterator* inputIter1 = sumSet1.createIterator() ;
  RooAbsArg* comp ;
  while((comp = (RooAbsArg*)inputIter1->Next())) {
    if (!dynamic_cast<RooAbsReal*>(comp)) {
      coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp->GetName() 
			    << " in first list is not of type RooAbsReal" << endl ;
      RooErrorHandler::softAbort() ;
    }
    _set1.add(*comp) ;
    if (takeOwnership) {
      _ownedList.addOwned(*comp) ;
    }
  }
  delete inputIter1 ;


  TIterator* inputIter2 = sumSet2.createIterator() ;
  while((comp = (RooAbsArg*)inputIter2->Next())) {
    if (!dynamic_cast<RooAbsReal*>(comp)) {
      coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp->GetName() 
			    << " in first list is not of type RooAbsReal" << endl ;
      RooErrorHandler::softAbort() ;
    }
    _set2.add(*comp) ;
    if (takeOwnership) {
      _ownedList.addOwned(*comp) ;
    }
  }
  delete inputIter2 ;
}



//_____________________________________________________________________________
RooAddition::RooAddition(const RooAddition& other, const char* name) :
  RooAbsReal(other, name), 
  _set1("!set1",this,other._set1),
  _set2("!set2",this,other._set2)
{
  // Copy constructor

  _setIter1 = _set1.createIterator() ;
  if (other._setIter2) {
    _setIter2 = _set2.createIterator() ;
  } else {
    _setIter2 = 0 ;
  }
  
  // Member _ownedList is intentionally not copy-constructed -- ownership is not transferred
}



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

  if (_setIter1) delete _setIter1 ;
  if (_setIter2) delete _setIter2 ;
}




//_____________________________________________________________________________
Double_t RooAddition::evaluate() const 
{
  // Calculate and return current value of self

  Double_t sum(0);
  RooAbsReal* comp ;
  const RooArgSet* nset = _set1.nset() ;

  _setIter1->Reset() ;

  if (_setIter2 && _set2.getSize()==0) {
    delete _setIter2 ;
    _setIter2=0 ;
  }

  if (!_setIter2) {

    while((comp=(RooAbsReal*)_setIter1->Next())) {
      sum += comp->getVal(nset) ;
    }

  } else {

    RooAbsReal* comp2 ;
    _setIter2->Reset() ;
    while((comp=(RooAbsReal*)_setIter1->Next())) {
      comp2 = (RooAbsReal*)_setIter2->Next() ;
      sum += comp->getVal(nset)*comp2->getVal(nset) ;
    }


  }
  
  return sum ;
}



//_____________________________________________________________________________
Double_t RooAddition::defaultErrorLevel() const 
{
  // Return the default error level for MINUIT error analysis
  // If the addition contains one or more RooNLLVars and 
  // no RooChi2Vars, return the defaultErrorLevel() of
  // RooNLLVar. If the addition contains one ore more RooChi2Vars
  // and no RooNLLVars, return the defaultErrorLevel() of
  // RooChi2Var. If the addition contains neither or both
  // issue a warning message and return a value of 1

  RooAbsReal* nllArg(0) ;
  RooAbsReal* chi2Arg(0) ;

  RooAbsArg* arg ;

  _setIter1->Reset() ;
  while((arg=(RooAbsArg*)_setIter1->Next())) {
    if (dynamic_cast<RooNLLVar*>(arg)) {
      nllArg = (RooAbsReal*)arg ;
    }
    if (dynamic_cast<RooChi2Var*>(arg)) {
      chi2Arg = (RooAbsReal*)arg ;
    }
  }

  if (_setIter2) {
    _setIter2->Reset() ;
    while((arg=(RooAbsArg*)_setIter2->Next())) {
      if (dynamic_cast<RooNLLVar*>(arg)) {
	nllArg = (RooAbsReal*)arg ;
      }
      if (dynamic_cast<RooChi2Var*>(arg)) {
	chi2Arg = (RooAbsReal*)arg ;
      }
    }
  }

  if (nllArg && !chi2Arg) {
    coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() 
		   << ") Summation contains a RooNLLVar, using its error level" << endl ;
    return nllArg->defaultErrorLevel() ;
  } else if (chi2Arg && !nllArg) {
    coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() 
		   << ") Summation contains a RooChi2Var, using its error level" << endl ;
    return chi2Arg->defaultErrorLevel() ;
  } else if (!nllArg && !chi2Arg) {
    coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() << ") WARNING: "
		   << "Summation contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << endl ;
  } else {
    coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() << ") WARNING: "
		   << "Summation contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << endl ;
  }

  return 1.0 ;
}


//_____________________________________________________________________________
void RooAddition::printMetaArgs(ostream& os) const 
{
  // Customized printing of arguments of a RooAddition to more intuitively reflect the contents of the
  // product operator construction

  _setIter1->Reset() ;
  if (_setIter2) {
    _setIter2->Reset() ;
  }

  Bool_t first(kTRUE) ;
    
  RooAbsArg* arg1, *arg2 ;
  if (_set2.getSize()!=0) { 

    while((arg1=(RooAbsArg*)_setIter1->Next())) {
      if (!first) {
	os << " + " ;
      } else {
	first = kFALSE ;
      }
      arg2=(RooAbsArg*)_setIter2->Next() ;
      os << arg1->GetName() << " * " << arg2->GetName() ;
    }

  } else {
    
    while((arg1=(RooAbsArg*)_setIter1->Next())) {
      if (!first) {
	os << " + " ;
      } else {
	first = kFALSE ;
      }
      os << arg1->GetName() ; 
    }  

  }

  os << " " ;    
}

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