Skip to content

Commit

Permalink
Restore prescale decoding in CodaDecoder
Browse files Browse the repository at this point in the history
Also, add checks to the THaRun initialization logic to detect
uninitialized prescale factors. These now cause run initialization to
fail, as it should, if prescale factors are requested.

The prescale event decoder should really be placed in an event type
handler object, but doing so is a lot more messy than I would like it
to be. Here's why:

The run initialization uses a private instance of the decoder, and only
the decoder, to scan for essential run information. If the prescale
processing (and other processing) is put in separate event type handlers,
those handlers would have to be processed at this point of the code as
well. Then the question arises, which handlers do we instantiate? What
if the run asks for more than just prescales, or simply prescales are
differently? How would we set up the correct prescale handler object
for the decoder we want to use?

Currently, this is all neatly handled via polymorphism of THaEvData.
If we want to decode something other than CODA files, we just use a different
concrete THaEvData class, e.g. OtherRawDataFormatDecoder, which extracts
prescale factors in the appropriate way and offers them via the generic
THaEvData interface. The decoder class is set up by the user via
THaInterface::SetDecoder, so knowledge of this one class type is sufficient
to decode the input data properly. If file-format-specific decoding
is now moved into separate event type handler classes, this logic will fail.
Obviously, this design will have to be revisited at a later time.
  • Loading branch information
hansenjo committed Nov 28, 2017
1 parent ab2a893 commit d5ea96c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
83 changes: 80 additions & 3 deletions hana_decode/CodaDecoder.C
Expand Up @@ -10,6 +10,7 @@
#include "CodaDecoder.h"
#include "THaCrateMap.h"
#include "THaBenchmark.h"
#include "THaUsrstrutils.h"
#include "TError.h"
#include <iostream>

Expand All @@ -33,6 +34,8 @@ CodaDecoder::CodaDecoder()
first_decode=kFALSE;
fMultiBlockMode=kFALSE;
fBlockIsDone=kFALSE;
for(int i=0; i<MAX_PSFACT; ++i)
psfact[i] = -1;
}

//_____________________________________________________________________________
Expand All @@ -43,12 +46,17 @@ CodaDecoder::~CodaDecoder()
}

//_____________________________________________________________________________
Int_t CodaDecoder::GetPrescaleFactor(Int_t /*trigger_type*/) const
Int_t CodaDecoder::GetPrescaleFactor(Int_t trigger_type) const
{
// To get the prescale factors for trigger number "trigger_type"
// (valid types are 1,2,3...)
// if (fgPsFact) return fgPsFact->GetPrescaleFactor(trigger_type);
//FIXME: TODO
if ( (trigger_type > 0) && (trigger_type <= MAX_PSFACT)) {
return psfact[trigger_type - 1];
}
if (fDebug > 0) {
Warning( "CodaDecoder::GetPrescaleFactor", "Requested prescale factor for "
"undefined trigger type %d", trigger_type );
}
return 0;
}

Expand Down Expand Up @@ -110,6 +118,10 @@ Int_t CodaDecoder::LoadEvent(const UInt_t* evbuffer)
run_num = evbuffer[3];
run_type = evbuffer[4];
evt_time = fRunTime;
} else if( event_type == PRESCALE_EVTYPE || event_type == TS_PRESCALE_EVTYPE ) {
ret = prescale_decode(evbuffer);
if( ret != HED_OK )
return ret;
}
if (event_type <= MAX_PHYS_EVTYPE) {
event_num = evbuffer[4];
Expand Down Expand Up @@ -626,7 +638,72 @@ void CodaDecoder::SetRunTime( ULong64_t tloc )
fNeedInit = true; // force re-init
}

//_____________________________________________________________________________
Int_t CodaDecoder::prescale_decode(const UInt_t* evbuffer)
{
// Decodes prescale factors from either
// TS_PRESCALE_EVTYPE(default) = PS factors
// read from Trig. Super. registors (since 11/03)
// - or -
// PRESCALE_EVTYPE = PS factors from traditional
// "prescale.dat" file.

assert( evbuffer );
assert( event_type == TS_PRESCALE_EVTYPE ||
event_type == PRESCALE_EVTYPE );
const Int_t HEAD_OFF1 = 2;
const Int_t HEAD_OFF2 = 4;
static const char* const pstr[] = { "ps1", "ps2", "ps3", "ps4",
"ps5", "ps6", "ps7", "ps8",
"ps9", "ps10", "ps11", "ps12" };
// TS registers -->
if( event_type == TS_PRESCALE_EVTYPE) {
// this is more authoritative
for (Int_t j = 0; j < 8; j++) {
Int_t k = j + HEAD_OFF1;
Int_t ps = 0;
if (k < event_length) {
ps = evbuffer[k];
if (psfact[j]!=0 && ps != psfact[j]) {
Warning("prescale_decode","Mismatch in prescale factor: "
"Trig %d oldps %d TS_PRESCALE %d. Setting to TS_PRESCALE",
j+1,psfact[j],ps);
}
}
psfact[j]=ps;
if (fDebug > 1)
cout << "%% TS psfact "<<dec<<j<<" "<<psfact[j]<<endl;
}
}
// "prescale.dat" -->
else if( event_type == PRESCALE_EVTYPE ) {
if( event_length <= HEAD_OFF2 )
return HED_ERR; //oops, event too short?
THaUsrstrutils sut;
sut.string_from_evbuffer(evbuffer+HEAD_OFF2, event_length-HEAD_OFF2);
for(Int_t trig=0; trig<MAX_PSFACT; trig++) {
Int_t ps = sut.getint(pstr[trig]);
Int_t psmax = 65536; // 2^16 for trig > 3
if (trig < 4) psmax = 16777216; // 2^24 for 1st 4 trigs
if (trig > 7) ps = 1; // cannot prescale trig 9-12
ps = ps % psmax;
if (psfact[trig]==-1) // not read before
psfact[trig] = ps;
else if (ps != psfact[trig]) {
Warning("prescale_decode","Mismatch in prescale factor: "
"Trig %d oldps %d prescale.dat %d, Keeping old value",
trig+1,psfact[trig],ps);
}
if (fDebug > 1)
cout << "** psfact[ "<<trig+1<< " ] = "<<psfact[trig]<<endl;
}
}
// Ok in any case
return HED_OK;
}

//_____________________________________________________________________________

} // namespace Decoder

ClassImp(Decoder::CodaDecoder)
2 changes: 2 additions & 0 deletions hana_decode/CodaDecoder.h
Expand Up @@ -45,12 +45,14 @@ class CodaDecoder : public THaEvData {
Bool_t buffmode,synchmiss,synchextra;

Int_t *fbfound;
Int_t psfact[MAX_PSFACT];

void CompareRocs();
void ChkFbSlot( Int_t roc, const UInt_t* evbuffer, Int_t ipt, Int_t istop );
void ChkFbSlots();

int init_slotdata(const THaCrateMap *map);
Int_t prescale_decode(const UInt_t* evbuffer);
void dump(const UInt_t* evbuffer) const;

ClassDef(CodaDecoder,0) // Decoder for CODA event buffer
Expand Down
12 changes: 8 additions & 4 deletions src/THaRun.C
Expand Up @@ -191,7 +191,8 @@ Int_t THaRun::ReadInitInfo()
if( status == THaEvData::HED_ERR ||
status == THaEvData::HED_FATAL ) {
Error( here, "Error decoding event %u", nev );
return READ_ERROR;
status = READ_ERROR;
break;
}
Warning( here, "Skipping event %u due to warnings", nev );
status = READ_OK;
Expand All @@ -205,13 +206,16 @@ Int_t THaRun::ReadInitInfo()
cout << "Prestart at " << nev << endl;
else if( st == 2 )
cout << "Prescales at " << nev << endl;

else if ( st < 0 ) {
status = READ_ERROR;
break;
}
}//end while
delete evdata;

if( status != READ_OK && status != READ_EOF ) {
Error( here, "Error %d reading CODA file %s. Check file permissions.",
status, GetFilename());
Error( here, "Error %d reading CODA file %s. Check file type & "
"permissions.", status, GetFilename());
return status;
}

Expand Down
11 changes: 9 additions & 2 deletions src/THaRunBase.C
Expand Up @@ -119,8 +119,15 @@ Int_t THaRunBase::Update( const THaEvData* evdata )
}
// Prescale factors
if( evdata->IsPrescaleEvent() ) {
for(int i=0; i<fParam->GetPrescales().GetSize(); i++)
fParam->Prescales()[i] = evdata->GetPrescaleFactor(i+1);
for(int i=0; i<fParam->GetPrescales().GetSize(); i++) {
Int_t psfact = evdata->GetPrescaleFactor(i+1);
if( psfact == -1 ) {
Error( "THaRunBase", "Failed to decode prescale factor for trigger %d. "
"Check raw data file for format errors.", i );
return -2;
}
fParam->Prescales()[i] = psfact;
}
fDataSet |= kPrescales;
fDataRead |= kPrescales;
ret = 2;
Expand Down

0 comments on commit d5ea96c

Please sign in to comment.