// @(#)root/hist:$Id$
// Author: Christophe.Delaere@cern.ch   21/08/2002

///////////////////////////////////////////////////////////////////////////
//
// TLimitDataSource
//
// This class serves as interface to feed data into the TLimit routines
//
///////////////////////////////////////////////////////////////////////////

#include "TLimitDataSource.h"
#include "TH1.h"
#include "TVectorD.h"
#include "TObjString.h"
#include "TRandom3.h"

ClassImp(TLimitDataSource)

TLimitDataSource::TLimitDataSource() 
{
   // Default constructor
   fDummyTA.SetOwner();
   fDummyIds.SetOwner();
}

TLimitDataSource::TLimitDataSource(TH1 * s, TH1 * b, TH1 * d) 
{
   // Another constructor, directly adds one channel
   // with signal, background and data given as input.
   fDummyTA.SetOwner();
   fDummyIds.SetOwner();
   AddChannel(s, b, d);
}

TLimitDataSource::TLimitDataSource(TH1 * s, TH1 * b, TH1 * d,
                                   TVectorD * es, TVectorD * eb, TObjArray * names)
{
   // Another constructor, directly adds one channel
   // with signal, background and data given as input.
   fDummyTA.SetOwner();
   fDummyIds.SetOwner();
   AddChannel(s, b, d, es, eb, names);
}

void TLimitDataSource::AddChannel(TH1 * s, TH1 * b, TH1 * d)
{
   // Adds a channel with signal, background and data given as input.
   
   TVectorD *empty;
   TRandom3 generator;
   fSignal.AddLast(s);
   fBackground.AddLast(b);
   fCandidates.AddLast(d);
   char rndname[20];
   snprintf(rndname,20, "rndname%f", generator.Rndm());
   empty = new TVectorD(1);
   fErrorOnSignal.AddLast(empty);
   fDummyTA.AddLast(empty);
   snprintf(rndname,20, "rndname%f", generator.Rndm());
   empty = new TVectorD(1);
   fErrorOnBackground.AddLast(empty);
   fDummyTA.AddLast(empty);
   TObjArray *dummy = new TObjArray(0);
   fIds.AddLast(dummy);
   fDummyIds.AddLast(dummy);
}

void TLimitDataSource::AddChannel(TH1 * s, TH1 * b, TH1 * d, TVectorD * es,
                                  TVectorD * eb, TObjArray * names)
{
   // Adds a channel with signal, background and data given as input.
   // In addition, error sources are defined.
   // TH1 are here used for convenience: each bin has to be seen as 
   // an error source (relative).
   // names is an array of strings containing the names of the sources.
   // Sources with the same name are correlated.
   
   fSignal.AddLast(s);
   fBackground.AddLast(b);
   fCandidates.AddLast(d);
   fErrorOnSignal.AddLast(es);
   fErrorOnBackground.AddLast(eb);
   fIds.AddLast(names);
}

void TLimitDataSource::SetOwner(bool swtch)
{
   // Gives to the TLimitDataSource the ownership of the various objects
   // given as input.
   // Objects are then deleted by the TLimitDataSource destructor.
   
   fSignal.SetOwner(swtch);
   fBackground.SetOwner(swtch);
   fCandidates.SetOwner(swtch);
   fErrorOnSignal.SetOwner(swtch);
   fErrorOnBackground.SetOwner(swtch);
   fIds.SetOwner(swtch);
   fDummyTA.SetOwner(!swtch);
   fDummyIds.SetOwner(!swtch);
}

 TLimitDataSource.cxx:1
 TLimitDataSource.cxx:2
 TLimitDataSource.cxx:3
 TLimitDataSource.cxx:4
 TLimitDataSource.cxx:5
 TLimitDataSource.cxx:6
 TLimitDataSource.cxx:7
 TLimitDataSource.cxx:8
 TLimitDataSource.cxx:9
 TLimitDataSource.cxx:10
 TLimitDataSource.cxx:11
 TLimitDataSource.cxx:12
 TLimitDataSource.cxx:13
 TLimitDataSource.cxx:14
 TLimitDataSource.cxx:15
 TLimitDataSource.cxx:16
 TLimitDataSource.cxx:17
 TLimitDataSource.cxx:18
 TLimitDataSource.cxx:19
 TLimitDataSource.cxx:20
 TLimitDataSource.cxx:21
 TLimitDataSource.cxx:22
 TLimitDataSource.cxx:23
 TLimitDataSource.cxx:24
 TLimitDataSource.cxx:25
 TLimitDataSource.cxx:26
 TLimitDataSource.cxx:27
 TLimitDataSource.cxx:28
 TLimitDataSource.cxx:29
 TLimitDataSource.cxx:30
 TLimitDataSource.cxx:31
 TLimitDataSource.cxx:32
 TLimitDataSource.cxx:33
 TLimitDataSource.cxx:34
 TLimitDataSource.cxx:35
 TLimitDataSource.cxx:36
 TLimitDataSource.cxx:37
 TLimitDataSource.cxx:38
 TLimitDataSource.cxx:39
 TLimitDataSource.cxx:40
 TLimitDataSource.cxx:41
 TLimitDataSource.cxx:42
 TLimitDataSource.cxx:43
 TLimitDataSource.cxx:44
 TLimitDataSource.cxx:45
 TLimitDataSource.cxx:46
 TLimitDataSource.cxx:47
 TLimitDataSource.cxx:48
 TLimitDataSource.cxx:49
 TLimitDataSource.cxx:50
 TLimitDataSource.cxx:51
 TLimitDataSource.cxx:52
 TLimitDataSource.cxx:53
 TLimitDataSource.cxx:54
 TLimitDataSource.cxx:55
 TLimitDataSource.cxx:56
 TLimitDataSource.cxx:57
 TLimitDataSource.cxx:58
 TLimitDataSource.cxx:59
 TLimitDataSource.cxx:60
 TLimitDataSource.cxx:61
 TLimitDataSource.cxx:62
 TLimitDataSource.cxx:63
 TLimitDataSource.cxx:64
 TLimitDataSource.cxx:65
 TLimitDataSource.cxx:66
 TLimitDataSource.cxx:67
 TLimitDataSource.cxx:68
 TLimitDataSource.cxx:69
 TLimitDataSource.cxx:70
 TLimitDataSource.cxx:71
 TLimitDataSource.cxx:72
 TLimitDataSource.cxx:73
 TLimitDataSource.cxx:74
 TLimitDataSource.cxx:75
 TLimitDataSource.cxx:76
 TLimitDataSource.cxx:77
 TLimitDataSource.cxx:78
 TLimitDataSource.cxx:79
 TLimitDataSource.cxx:80
 TLimitDataSource.cxx:81
 TLimitDataSource.cxx:82
 TLimitDataSource.cxx:83
 TLimitDataSource.cxx:84
 TLimitDataSource.cxx:85
 TLimitDataSource.cxx:86
 TLimitDataSource.cxx:87
 TLimitDataSource.cxx:88
 TLimitDataSource.cxx:89
 TLimitDataSource.cxx:90
 TLimitDataSource.cxx:91
 TLimitDataSource.cxx:92
 TLimitDataSource.cxx:93
 TLimitDataSource.cxx:94
 TLimitDataSource.cxx:95
 TLimitDataSource.cxx:96
 TLimitDataSource.cxx:97
 TLimitDataSource.cxx:98
 TLimitDataSource.cxx:99
 TLimitDataSource.cxx:100
 TLimitDataSource.cxx:101
 TLimitDataSource.cxx:102