/////////////////////////////////////////////////////////////////////////////
//
// MRawEvtHeader 
//    
// One Event is a sample of FADC measurements of different Pixels 
// (Photomultipliers) from the Camera of MAGIC. So all data (FADC) of the 
// interesting pixels are the modules of an event. To describe pixels the 
// class MRawPixel is used and the class MRawCrate to describe Crates.
// To define a single events some other data members are needed 
// (Time of the events, tirgger pattern of event..)
// 
// To describe one event on the level of FADC values the Class MRawEvtHeader is
// created. It has the following data members: 
//
// UInt_t    fDAQEvtNumber
// -----------------------
// This it the number of the Event in one 
// data run. The first event in this run get
// the number zero. The next one is one bigger.
//
// Assuming that one run takes 1 hour and a
// triggerrate of 1kHz the number must be able
// to reach 3.6e6 Events. To reach this number
// you need at least 22 bits. This is the reason
// why we use an integer (of root type UInt_t)
// with a range to 4.2e9. 
//
// MTime   fRawEvtTime
// -------------------
// Time of the event. 
// The start point of the time determination can be
// the millenium. From that start point the time is
// measured in 200ns-count. One day for example
// contains 432.e9 counts. An array of two unsigned Int is able to 
// contain 1.8e19 200ns-counts. This corresponds to 41.e6
// days. This should be more than the livetime of MAGIC.
// Private member of MTime.h
//
// UInt_t  fNumTrigLvl1
// --------------------
//
// Number of first level trigger
// This member counts the number of First Level Trigger
// between the last and this event. May be that due to 
// dead time of the DAQ this number is different from 1.
// If the DAQ is fast enough, this value should be 1. 
// This may be usefull in GammaRayBursts and if we 
// apply a data reduction in the DAQ-chain, which selects
// only good events. 
//
// UInt_t  fNumTrigLvl2
// ------------------ -
//
// Number of second level trigger
// This member counts the number of Second Level Trigger
// between the last and this event. 
//
// UInt_t  fTrigPattern[2]
// -----------------------
// Trigger Pattern used for this event
// Each event triggers for a particular configuration and each  
// configuration shoul have an ID (which is not fixed yet).
//
// UShort_t fAllLowGainOn
// ----------------------
// Type of Trigger. 
// This is a Byte (8 bit) to indicated if any of the pixels 
// have a non-negligible low gain (1) or not (0) 
//
/////////////////////////////////////////////////////////////////////////////
#include "MRawEvtHeader.h"

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>

#include "MTime.h"
#include "MArrayB.h"
#include "MRawRunHeader.h"

ClassImp(MRawEvtHeader)

MRawEvtHeader::MRawEvtHeader(const char *name, const char *title) 
{
    *fName  = name  ? name  : "MRawEvtHeader";
    *fTitle = title ? title : "Raw Event Header Information";

    //
    //   set all member to zero, init the pointer to ClonesArray,
    //

    fPixLoGainOn = new MArrayB;

    Clear();
}


MRawEvtHeader::~MRawEvtHeader()
{
}

void MRawEvtHeader::Init(MRawRunHeader *rh, MTime *t)
{
    //
    // you have to init the conatainer before you can read from
    // a raw binary file
    //

    //
    // this is the number of entries in the array like specification
    //
    UInt_t fN = (rh->GetNumCrates() * rh->GetNumPixInCrate() + 7) / 8;

    //
    // initialize the array
    //
    fPixLoGainOn->Set(fN);

    //
    // this is the conatiner where we have to store the time of the event we
    // read from the input stream
    //
    fTime = t;
}

void MRawEvtHeader::Clear(Option_t *)
{
    //
    //   Implementation of the Clear function
    //
    //   Resets all members to zero, clear the list of Pixels
    //
    fDAQEvtNumber   = 0;
    fNumTrigLvl1    = 0;
    fNumTrigLvl2    = 0;
    fTrigPattern[0] = 0;
    fTrigPattern[1] = 0;
    fTrigType       = 0;
    fNumLoGainOn    = 0;
}

void MRawEvtHeader::Print(Option_t *o)
{
    //
    //  This member function prints all Data of one Event on screen.
    //
    cout << "DAQEvtNr: " << dec << fDAQEvtNumber << "  (";
    cout << "Trigger: ";
    cout << "NumLvl1=" << fNumTrigLvl1 << " ";
    cout << "NumLvl2=" << fNumTrigLvl2 << " ";
    cout << "Pattern=" << hex << setfill('0');
    cout << setw(2) << fTrigPattern[0];
    cout << setw(2) << fTrigPattern[1] << " " << dec;

    cout << "Type=";
    switch (fTrigType)
    {
    case 0:
        cout << "Trigger";
        break;
    case 1:
        cout << "Pedestal";
        break;
    case 2:
        cout << "Calibration";
        break;
    }
    cout << ")" << endl;
    cout << "Number of Lo Gains On: " << fNumLoGainOn << endl;

    for (int i=0; i<fPixLoGainOn->GetSize(); i++)
    {
        for (int j=0; j<8; j++)
        {
            const UInt_t on = (*fPixLoGainOn)[i]&(1<<j) ? 1 : 0;
            cout << on;
        }
    }

    cout << endl;

    cout << endl;

}

void MRawEvtHeader::FillHeader(UInt_t uiN, Float_t ulTP)
{
    //
    // used to set the header information (eg. from MC)
    //
    fDAQEvtNumber = uiN;
    fTrigPattern[0] = (UInt_t) (ulTP/4294967296.0) ;
    fTrigPattern[1] = (UInt_t) (ulTP-fTrigPattern[0]*4294967296.0);
}

int MRawEvtHeader::ReadEvt(ifstream &fin)
{
    //
    // read the EVENT HEADER information from the input stream
    // return FALSE if there is now header anymore, else TRUE
    //
    fin.read((Byte_t*)&fDAQEvtNumber, 4);

    UInt_t fAbsTime[2];
    fin.read((Byte_t*)fAbsTime,       8);

    //
    // store the time of the event in the corresponding container
    //
    fTime->SetTime(fAbsTime[0], fAbsTime[1]);

    Byte_t dummy[4];

    fin.read((Byte_t*)&fNumTrigLvl1,  4);
    fin.read((Byte_t*)&fNumTrigLvl2,  4);
    fin.read((Byte_t*)fTrigPattern,   8);
    fin.read((Byte_t*)&fTrigType,     2);
    fin.read((Byte_t*)dummy,          2); // was fAllLoGainOn
    fin.read((Byte_t*)fPixLoGainOn->GetArray(), fPixLoGainOn->GetSize());

    fNumLoGainOn = 0;
    for (int i=0; i<fPixLoGainOn->GetSize(); i++)
        for (int j=0; j<8; j++)
            if ((*fPixLoGainOn)[i] & (1<<j))
                fNumLoGainOn++;

    fin.read((Byte_t*)&dummy, 4);

    return !fin.eof();
}
