// @(#)root/roostats:$Id: FrequentistCalculator.h 37084 2010-11-29 21:37:13Z moneta $
// Author: Sven Kreiss, Kyle Cranmer   Nov 2010
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOSTATS_FrequentistCalculator
#define ROOSTATS_FrequentistCalculator

//_________________________________________________
/*
BEGIN_HTML
<p>
The use of ToyMCSampler as the TestStatSampler is assumed.
</p>
END_HTML
*/
//



#ifndef ROOSTATS_HypoTestCalculatorGeneric
#include "RooStats/HypoTestCalculatorGeneric.h"
#endif

#ifndef ROOSTATS_ToyMCSampler
#include "RooStats/ToyMCSampler.h"
#endif

#ifndef ROOSTATS_DetailedOutputAggregator
#include "RooStats/DetailedOutputAggregator.h"
#endif

#include "RooFitResult.h"

namespace RooStats {

   class FrequentistCalculator : public HypoTestCalculatorGeneric {

   public:
      FrequentistCalculator(
                        const RooAbsData &data,
                        const ModelConfig &altModel,
                        const ModelConfig &nullModel,
                        TestStatSampler* sampler=0
      ) :
         HypoTestCalculatorGeneric(data, altModel, nullModel, sampler),
         fConditionalMLEsNull(NULL),
         fConditionalMLEsAlt(NULL),
         fNToysNull(-1),
         fNToysAlt(-1),
         fNToysNullTail(0),
         fNToysAltTail(0),
	 fFitInfo(NULL),
	 fStoreFitInfo(false)
      {
      }

      ~FrequentistCalculator() {
         if( fConditionalMLEsNull ) delete fConditionalMLEsNull;
	 if( fConditionalMLEsAlt ) delete fConditionalMLEsAlt;
	 if( fFitInfo ) delete fFitInfo;
      }


      // set number of toys
      void SetToys(int toysNull, int toysAlt) { fNToysNull = toysNull; fNToysAlt = toysAlt; }

      // set least number of toys in tails
      void SetNToysInTails(int toysNull, int toysAlt) { fNToysNullTail = toysNull; fNToysAltTail = toysAlt; }

      // set given nuisance parameters to a specific value that will be used instead of their
      // profiled value for Null toys
      void SetConditionalMLEsNull( const RooArgSet* c ) {
         if( fConditionalMLEsNull ) delete fConditionalMLEsNull;
         
         if( c ) fConditionalMLEsNull = (const RooArgSet*)c->snapshot();
         else fConditionalMLEsNull = NULL;
      }

      // set given nuisance parameters to a specific value that will be used instead of their
      // profiled value for Alternate toys
      void SetConditionalMLEsAlt( const RooArgSet* c ) {
         if( fConditionalMLEsAlt ) delete fConditionalMLEsAlt;
         
         if( c ) fConditionalMLEsAlt = (const RooArgSet*)c->snapshot();
         else fConditionalMLEsAlt = NULL;
      }

      void StoreFitInfo(bool val = true) {
	      fStoreFitInfo = val;
      }

      const RooArgSet* GetFitInfo() const {
	      return fFitInfo;
      }

   protected:
      // configure TestStatSampler for the Null run
      int PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const;

      // configure TestStatSampler for the Alt run
      int PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const;

      void PreHook() const;
      void PostHook() const;

   protected:
      // MLE inputs
      const RooArgSet* fConditionalMLEsNull;
      const RooArgSet* fConditionalMLEsAlt;
   
      // different number of toys for null and alt
      int fNToysNull;
      int fNToysAlt;

      // adaptive sampling
      int fNToysNullTail;
      int fNToysAltTail;

   private:
      mutable RooArgSet* fFitInfo;
      bool fStoreFitInfo;

   protected:
      ClassDef(FrequentistCalculator,1)
   };
}

#endif
 FrequentistCalculator.h:1
 FrequentistCalculator.h:2
 FrequentistCalculator.h:3
 FrequentistCalculator.h:4
 FrequentistCalculator.h:5
 FrequentistCalculator.h:6
 FrequentistCalculator.h:7
 FrequentistCalculator.h:8
 FrequentistCalculator.h:9
 FrequentistCalculator.h:10
 FrequentistCalculator.h:11
 FrequentistCalculator.h:12
 FrequentistCalculator.h:13
 FrequentistCalculator.h:14
 FrequentistCalculator.h:15
 FrequentistCalculator.h:16
 FrequentistCalculator.h:17
 FrequentistCalculator.h:18
 FrequentistCalculator.h:19
 FrequentistCalculator.h:20
 FrequentistCalculator.h:21
 FrequentistCalculator.h:22
 FrequentistCalculator.h:23
 FrequentistCalculator.h:24
 FrequentistCalculator.h:25
 FrequentistCalculator.h:26
 FrequentistCalculator.h:27
 FrequentistCalculator.h:28
 FrequentistCalculator.h:29
 FrequentistCalculator.h:30
 FrequentistCalculator.h:31
 FrequentistCalculator.h:32
 FrequentistCalculator.h:33
 FrequentistCalculator.h:34
 FrequentistCalculator.h:35
 FrequentistCalculator.h:36
 FrequentistCalculator.h:37
 FrequentistCalculator.h:38
 FrequentistCalculator.h:39
 FrequentistCalculator.h:40
 FrequentistCalculator.h:41
 FrequentistCalculator.h:42
 FrequentistCalculator.h:43
 FrequentistCalculator.h:44
 FrequentistCalculator.h:45
 FrequentistCalculator.h:46
 FrequentistCalculator.h:47
 FrequentistCalculator.h:48
 FrequentistCalculator.h:49
 FrequentistCalculator.h:50
 FrequentistCalculator.h:51
 FrequentistCalculator.h:52
 FrequentistCalculator.h:53
 FrequentistCalculator.h:54
 FrequentistCalculator.h:55
 FrequentistCalculator.h:56
 FrequentistCalculator.h:57
 FrequentistCalculator.h:58
 FrequentistCalculator.h:59
 FrequentistCalculator.h:60
 FrequentistCalculator.h:61
 FrequentistCalculator.h:62
 FrequentistCalculator.h:63
 FrequentistCalculator.h:64
 FrequentistCalculator.h:65
 FrequentistCalculator.h:66
 FrequentistCalculator.h:67
 FrequentistCalculator.h:68
 FrequentistCalculator.h:69
 FrequentistCalculator.h:70
 FrequentistCalculator.h:71
 FrequentistCalculator.h:72
 FrequentistCalculator.h:73
 FrequentistCalculator.h:74
 FrequentistCalculator.h:75
 FrequentistCalculator.h:76
 FrequentistCalculator.h:77
 FrequentistCalculator.h:78
 FrequentistCalculator.h:79
 FrequentistCalculator.h:80
 FrequentistCalculator.h:81
 FrequentistCalculator.h:82
 FrequentistCalculator.h:83
 FrequentistCalculator.h:84
 FrequentistCalculator.h:85
 FrequentistCalculator.h:86
 FrequentistCalculator.h:87
 FrequentistCalculator.h:88
 FrequentistCalculator.h:89
 FrequentistCalculator.h:90
 FrequentistCalculator.h:91
 FrequentistCalculator.h:92
 FrequentistCalculator.h:93
 FrequentistCalculator.h:94
 FrequentistCalculator.h:95
 FrequentistCalculator.h:96
 FrequentistCalculator.h:97
 FrequentistCalculator.h:98
 FrequentistCalculator.h:99
 FrequentistCalculator.h:100
 FrequentistCalculator.h:101
 FrequentistCalculator.h:102
 FrequentistCalculator.h:103
 FrequentistCalculator.h:104
 FrequentistCalculator.h:105
 FrequentistCalculator.h:106
 FrequentistCalculator.h:107
 FrequentistCalculator.h:108
 FrequentistCalculator.h:109
 FrequentistCalculator.h:110
 FrequentistCalculator.h:111
 FrequentistCalculator.h:112
 FrequentistCalculator.h:113
 FrequentistCalculator.h:114
 FrequentistCalculator.h:115
 FrequentistCalculator.h:116
 FrequentistCalculator.h:117
 FrequentistCalculator.h:118
 FrequentistCalculator.h:119
 FrequentistCalculator.h:120
 FrequentistCalculator.h:121
 FrequentistCalculator.h:122
 FrequentistCalculator.h:123
 FrequentistCalculator.h:124
 FrequentistCalculator.h:125
 FrequentistCalculator.h:126
 FrequentistCalculator.h:127
 FrequentistCalculator.h:128
 FrequentistCalculator.h:129
 FrequentistCalculator.h:130
 FrequentistCalculator.h:131
 FrequentistCalculator.h:132
 FrequentistCalculator.h:133
 FrequentistCalculator.h:134