/* ======================================================================== *\
!
! *
! * This file is part of MARS, the MAGIC Analysis and Reconstruction
! * Software. It is distributed to you in the hope that it can be a useful
! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
! * It is distributed WITHOUT ANY WARRANTY.
! *
! * Permission to use, copy, modify and distribute this software and its
! * documentation for any purpose is hereby granted without fee,
! * provided that the above copyright notice appear in all copies and
! * that both that copyright notice and this permission notice appear
! * in supporting documentation. It is provided "as is" without express
! * or implied warranty.
! *
!
!
!   Author(s): Thomas Bretz, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
!
!   Copyright: MAGIC Software Development, 2002-2003
!
!
\* ======================================================================== */

//////////////////////////////////////////////////////////////////////////////
//
//  MEventRateCalc
//
//  This task calculates the event rates from the event times and numbers.
//
//  The algorithm is explained in Process()
//
//  Input Containers:
//    MTime
//
//  Output Containers:
//    MEventRate
//    MTimeRate [MTime] (missing)
//
//  FIXME: For convinience we could implement a mode which always takes
//         n events to calculate the event rate and sets the corresponding
//         time. This mode could be used to UPADTE files with the event
//         rate.
//
//////////////////////////////////////////////////////////////////////////////
#include "MEventRateCalc.h"

#include "MParList.h"

#include "MLog.h"
#include "MLogManip.h"

#include "MTime.h"
#include "MEventRate.h"

ClassImp(MEventRateCalc);

using namespace std;

// --------------------------------------------------------------------------
//
// Default constructor.
//
MEventRateCalc::MEventRateCalc(const char *name, const char *title)
    : fTimes(1000)
{
    fName  = name  ? name  : "MEventRateCalc";
    fTitle = title ? title : "Calculate trigger rate";
}

// --------------------------------------------------------------------------
//
// The PreProcess searches for the following input containers:
//   MTime
//
// The PreProcess searches for the following input containers:
//   MEventRate
//
// Reset all times in the buffer
//
Int_t MEventRateCalc::PreProcess(MParList *pList)
{
    fTime = (MTime*)pList->FindObject("MTime");
    if (!fTime)
    {
        *fLog << err << "MTime not found... aborting." << endl;
        return kFALSE;
    }

    fRate = (MEventRate*)pList->FindCreateObj("MEventRate");
    if (!fRate)
        return kFALSE;

    memset(fTimes.GetArray(), 0, sizeof(Double_t)*fTimes.GetSize());

    return kTRUE;
}

// --------------------------------------------------------------------------
//
// Calculate the events rate as (t1-t0)/n while t1 is the n-th event after
// t0. If there are not yet enough events in the buffer n is the number
// of available events. Otherwise the number setup in SetNumEvents.
//
Int_t MEventRateCalc::Process()
{
    const ULong_t exec = GetNumExecutions()-1;

    const UInt_t n = fTimes.GetSize();

    const UInt_t n1 = exec;
    const UInt_t n2 = exec>=n ? exec+1 : 0;

    fTimes[n1%n] = *fTime;

    const UInt_t cnt = n1<n2 ? n : n1-n2;

    const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);

    fRate->SetRate(exec>1?rate:0, cnt);
    fRate->SetReadyToSave();

    return kTRUE;
}
