using namespace std;
#include "hwalltrb2unpacker.h"
#include "walldef.h"
#include "hwalltrb2lookup.h"
#include "htrbnetaddressmapping.h"
#include "htrb2correction.h"
#include "hwallraw.h"
#include "hwallrawsim.h"
#include "hdebug.h"
#include "hades.h"
#include "hevent.h"
#include "hspectrometer.h"
#include "hdetector.h"
#include "hruntimedb.h"
#include "hcategory.h"
#include "hldsubevt.h"
#include "heventheader.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include "TCanvas.h"
#include "TStyle.h"
ClassImp (HWallTrb2Unpacker)
HWallTrb2Unpacker::HWallTrb2Unpacker (UInt_t id):HTrb2Unpacker (id)
{
pRawCat = NULL;
timeRef = kTRUE;
timeRefRemove = kFALSE;
timeShift = 7200.;
}
Bool_t HWallTrb2Unpacker::init (void)
{
pRawCat = gHades->getCurrentEvent ()->getCategory (catWallRaw);
if (!pRawCat) {
pRawCat = gHades->getSetup ()->getDetector ("Wall")->buildCategory (catWallRaw);
if (!pRawCat)
return kFALSE;
gHades->getCurrentEvent ()->addCategory (catWallRaw, pRawCat, "Wall");
}
loc.set(1,0);
lookup = (HWallTrb2Lookup *) (gHades->getRuntimeDb ()->getContainer ("WallTrb2Lookup"));
if (!lookup) return kFALSE;
if (correctINL) {
trbaddressmap=(HTrbnetAddressMapping*)(gHades->getRuntimeDb()->getContainer("TrbnetAddressMapping"));
if (!trbaddressmap) {
Error("init","No Pointer to parameter container TrbnetAddressMapping.");
return kFALSE;
}
}
return kTRUE;
}
Bool_t HWallTrb2Unpacker::finalize(void)
{
if(!quietMode && debugFlag > 0 ){
Info("finalize","No control histograms implemented");
}
return kTRUE;
}
Int_t HWallTrb2Unpacker::execute (void)
{
HWallRaw *pRaw = 0;
Int_t nEvt = 0;
Int_t m=0;
if (gHades->isCalibration ()) {
return 1;
}
if (gHades->getCurrentEvent()->getHeader()->getId() == 0xe) {
return 1;
}
if (pSubEvt) {
nEvt = gHades->getCurrentEvent ()->getHeader ()->getEventSeqNumber ();
Int_t DecodeOutput = 1;
while( 1 ) {
DecodeOutput = decode();
uStartPosition = uStartPosition + uSubBlockSize + 1;
if (DecodeOutput==10) continue;
if (DecodeOutput==0) {
Error("decode","subsubevent decoding failed!!! Evt Nr : %i SubEvtId: %x",nEvt,subEvtId);
continue;
}
if (DecodeOutput==100) {
uStartPosition=0;
if ( debugFlag > 0 ) Info("decode","DecodeOutput : %i",DecodeOutput);
break;
}
if(timeRef) correctRefTimeCh(32);
HWallTrb2LookupBoard *board = lookup->getBoard (uTrbNetAdress);
if (!board) {
Warning ("execute", "TrbNetAdress: %x (%i) unpacked but TRB Board not in lookup table",uTrbNetAdress,uTrbNetAdress);
return 1;
}
if(board->getSize() < 0) continue;
for (Int_t i=0; i<board->getSize(); i++) {
if (trbLeadingMult[i] < 1 ) continue;
if ( debugFlag > 0 ) cout <<"timeRefRemove "<<timeRefRemove<<" timeShift "<<timeShift<<endl;
if(timeRefRemove) {
if(TMath::Abs(trbLeadingTime[i][0])<0.1) continue;
}
trbLeadingTime[i][0]=trbLeadingTime[i][0]+timeShift;
if ( debugFlag > 0 ) printf("chan %d time %.0f adc %.0f\n",i,trbLeadingTime[i][0],trbADC[i][0]);
if(i == -1) continue;
HWallTrb2LookupChan *chan = board->getChannel (i);
loc[0] = (Int_t)chan->getCell();
if ( debugFlag > 0 ) printf("chan %d cell %d\n",i,loc[0]);
if(loc[0]>=0) {
pRaw=(HWallRaw *)pRawCat->getObject(loc);
if (!pRaw){
pRaw=(HWallRaw *)pRawCat->getSlot(loc);
if (pRaw) {
if(gHades->getEmbeddingMode()==0) {
pRaw=new (pRaw) HWallRaw;
}else{
pRaw=new (pRaw) HWallRawSim;
}
pRaw->setCell(loc[0]);
m=trbLeadingMult[i];
if(m>4) m=4;
for(Int_t chmult=0; chmult<m; chmult++){
pRaw->fill( trbLeadingTime[i][chmult], trbADC[i][chmult]);
}
}
else {
Error("execute()",
"Can't get slot\n");
return -1;
}
}
else {
#if DEBUG_LEVEL>4
Info("execute()",
"Slot already exists!\n");
#endif
}
}
}
}
}
return 1;
}