ROOT logo
//*-- AUTHOR : Ilse Koenig, Tomasz Wojcik, Wolfgang Koenig
//*-- Created : 17/01/2005

//_HADES_CLASS_DESCRIPTION 
/////////////////////////////////////////////////////////////
//
//  HWallRaw
//
//  Class for the raw data class of the Forward Wall
//  (see TOF unpacker for subevent information)
//
/////////////////////////////////////////////////////////////

using namespace std;
#include "hwallunpacker.h"
#include "walldef.h"
#include "hwalllookup.h"
#include "hwallraw.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>

ClassImp(HWallUnpacker)

HWallUnpacker::HWallUnpacker(UInt_t id) {
  // constructor
  subEvtId=id;
  pRawCat=NULL;
  lookup=0;
}

Bool_t HWallUnpacker::init(void) {
  // creates the raw category and gets the pointer to the lookup table
  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=(HWallLookup*)(gHades->getRuntimeDb()->getContainer("WallLookup"));
  return kTRUE;
}


Int_t HWallUnpacker::execute(void) {
// This function fills the Forward Wall raw data structure.
// see TOF unpacker for subevent information
  HWallRaw* pRaw=0;
  Int_t nCrate=-1, nChannel=0, nCell=-1;
  Int_t nEvt        = 0;    //Evt Id
  UInt_t nSlot      = 0;
  UInt_t uWordCount = 0;    //auxiliary
  UInt_t uBlockSize = 0;    //Number of data words in one block
  UInt_t uBoardType = 0;    // 1 - Struck, 0 - TDC
  UInt_t *uTmp      = NULL; //auxiliary
  Char_t dcType='U';
  if (gHades->isCalibration()){
    //calibration event
    //nothing to do for forward wall
    return 1;
  }
  if (pSubEvt) {
    UInt_t* data = pSubEvt->getData();
    UInt_t* end  = pSubEvt->getEnd();
    nEvt = gHades->getCurrentEvent()->getHeader()->getEventSeqNumber();
    //Loop over all data in this subevt
    while ( ++data<end && *data!=0x0 ) {
      #if DEBUG_LEVEL>4
         printf("Data word: %p\n",*data);
      #endif
      //Scan for TIP-block header ( TYPE 0 )
      //TIP-block appears only in the new data structure
      if ( ((*data >>24) & 0x6)==0 ) {
        uBlockSize = (*data) & 0x0000003ff;
        uBoardType = (*data >> 12 ) & 0x1;
        //latch or scaler
        if (uBoardType==1) {
          data+=uBlockSize;
          continue;
        }
        //next Should (can) be tdc header
        else if (uBoardType==0) {
          //Do a normal TDC scanning
          #if DEBUG_LEVEL>2
             printf("TIP-block 0x%08x Size %i Board  %i\n",
                    *data,uBlockSize,uBoardType);
          #endif
          continue;
        } else {
          #if DEBUG_LEVEL>2
             Warning("execute","Uknown BoardType=%i",uBoardType);
          #endif
        }
      }
      //Scan for TDC header
      else if ( ((*data>>24) & 0x6)==2 ){
        //Found valid TDC header
        uBlockSize = ( (*data) >> 8   ) & 0xff;
        nCrate     = ( (*data) >> 16  ) & 0xff;
        nSlot      = ( (*data) >> 27  ) & 0x1f;
        //Check if also trailer exists
        if ((*(data + uBlockSize+1) >>24 & 0x6)!=4) {
          Warning("execute","Header found but no trailer!!!");
          continue;
        }
        //Check trigger tag in the trailer
        if ((*(data+uBlockSize+1)&0xff)!=((UInt_t)pSubEvt->getTrigNr()&0xFF)) {
          Error("execute","Trigger tag mismatch!");
          exit(EXIT_FAILURE);
        }
        //Scan all data words
        #if DEBUG_LEVEL>4
           printf("TDC header 0x%08x.Size: %i.Crate: %i.Slot: %i.\n",
                  *data,uBlockSize,nCrate,nSlot);
        #endif
        uTmp=data;
        HWallLookupSlot* dc=lookup->getSlot(nCrate-1,nSlot-1);
        if (dc && (dcType=dc->getType())!='U') {
          uWordCount=0;
          while( ++data && data<(uTmp + uBlockSize+1) && *data!=0x0 ) {
            uWordCount++;
            if (nSlot!=((*data>>27)&0x1f)) {
              Warning("execute",
                      "Slot (%d) different that in header(%d)",
                      nSlot,(*data>>27)&0x1f);
              continue;
            }
            nChannel = (*(data) >> 16 ) & 0x003f;
            HWallLookupChan* dcs=dc->getChannel(nChannel);
            if (dcs) {
              nCell=dcs->getCell();
              loc[0]=nCell;
              pRaw=(HWallRaw *)pRawCat->getObject(loc);
              if (!pRaw) {
                pRaw=(HWallRaw *)pRawCat->getSlot(loc);
                if (pRaw) {
                  pRaw=new (pRaw) HWallRaw;
                  pRaw->setCell(nCell);
                } else {
                  Error("execute()","Can't get slot\n");
                  return -1;
                }
              } else {
                #if DEBUG_LEVEL>4
                   Info("execute()","Slot already exists!\n");
                #endif
              }
              Float_t val = (*data & 0xfff);
              if (dcType=='T') pRaw->setTime(val);
              else pRaw->setCharge(val);
            } else Info("execute()","TDC channel: %i not found",nChannel);
          } // end while
          if (uWordCount!=uBlockSize) {
            //Corrupted block
            Warning("execute()",
                    "Found %i but expecting %i words!!!",
                    uWordCount,uBlockSize);
            continue;
          }
        } else {
          //Skip this block
          data+=uBlockSize+1;
          #if DEBUG_LEVEL>4
             Info("execute()",
                  "Can not get TDC for crate %i and slot %i",
                  nCrate-1,nSlot-1);
          #endif
        }
      } else if ( ((*data>>24) & 0x6)==6 ) {
        //invalid data word
        // skip this filler word
        #if DEBUG_LEVEL>2
           Warning("execute()",
                   "Evt: %i, Invalid Data Word! Type: %d,Skipping 0x%08x",
                   nEvt,((*data>>24) & 0x6),*data);
          #if DEBUG_LEVEL>4
             pSubEvt->dumpIt();
          #endif
        #endif
      } else if ( ((*data>>24) & 0x6)==0 ) {
        Warning("execute()",
                "Evt: %i, Data follow, but no header",nEvt);
      } else {
        Warning("execute()",
                "Evt: %i, Unknow raw data type %i",
                nEvt,((*data>>24) & 0x6));
      }
    } //end of data
  }
  return 1;
}






 hwallunpacker.cc:1
 hwallunpacker.cc:2
 hwallunpacker.cc:3
 hwallunpacker.cc:4
 hwallunpacker.cc:5
 hwallunpacker.cc:6
 hwallunpacker.cc:7
 hwallunpacker.cc:8
 hwallunpacker.cc:9
 hwallunpacker.cc:10
 hwallunpacker.cc:11
 hwallunpacker.cc:12
 hwallunpacker.cc:13
 hwallunpacker.cc:14
 hwallunpacker.cc:15
 hwallunpacker.cc:16
 hwallunpacker.cc:17
 hwallunpacker.cc:18
 hwallunpacker.cc:19
 hwallunpacker.cc:20
 hwallunpacker.cc:21
 hwallunpacker.cc:22
 hwallunpacker.cc:23
 hwallunpacker.cc:24
 hwallunpacker.cc:25
 hwallunpacker.cc:26
 hwallunpacker.cc:27
 hwallunpacker.cc:28
 hwallunpacker.cc:29
 hwallunpacker.cc:30
 hwallunpacker.cc:31
 hwallunpacker.cc:32
 hwallunpacker.cc:33
 hwallunpacker.cc:34
 hwallunpacker.cc:35
 hwallunpacker.cc:36
 hwallunpacker.cc:37
 hwallunpacker.cc:38
 hwallunpacker.cc:39
 hwallunpacker.cc:40
 hwallunpacker.cc:41
 hwallunpacker.cc:42
 hwallunpacker.cc:43
 hwallunpacker.cc:44
 hwallunpacker.cc:45
 hwallunpacker.cc:46
 hwallunpacker.cc:47
 hwallunpacker.cc:48
 hwallunpacker.cc:49
 hwallunpacker.cc:50
 hwallunpacker.cc:51
 hwallunpacker.cc:52
 hwallunpacker.cc:53
 hwallunpacker.cc:54
 hwallunpacker.cc:55
 hwallunpacker.cc:56
 hwallunpacker.cc:57
 hwallunpacker.cc:58
 hwallunpacker.cc:59
 hwallunpacker.cc:60
 hwallunpacker.cc:61
 hwallunpacker.cc:62
 hwallunpacker.cc:63
 hwallunpacker.cc:64
 hwallunpacker.cc:65
 hwallunpacker.cc:66
 hwallunpacker.cc:67
 hwallunpacker.cc:68
 hwallunpacker.cc:69
 hwallunpacker.cc:70
 hwallunpacker.cc:71
 hwallunpacker.cc:72
 hwallunpacker.cc:73
 hwallunpacker.cc:74
 hwallunpacker.cc:75
 hwallunpacker.cc:76
 hwallunpacker.cc:77
 hwallunpacker.cc:78
 hwallunpacker.cc:79
 hwallunpacker.cc:80
 hwallunpacker.cc:81
 hwallunpacker.cc:82
 hwallunpacker.cc:83
 hwallunpacker.cc:84
 hwallunpacker.cc:85
 hwallunpacker.cc:86
 hwallunpacker.cc:87
 hwallunpacker.cc:88
 hwallunpacker.cc:89
 hwallunpacker.cc:90
 hwallunpacker.cc:91
 hwallunpacker.cc:92
 hwallunpacker.cc:93
 hwallunpacker.cc:94
 hwallunpacker.cc:95
 hwallunpacker.cc:96
 hwallunpacker.cc:97
 hwallunpacker.cc:98
 hwallunpacker.cc:99
 hwallunpacker.cc:100
 hwallunpacker.cc:101
 hwallunpacker.cc:102
 hwallunpacker.cc:103
 hwallunpacker.cc:104
 hwallunpacker.cc:105
 hwallunpacker.cc:106
 hwallunpacker.cc:107
 hwallunpacker.cc:108
 hwallunpacker.cc:109
 hwallunpacker.cc:110
 hwallunpacker.cc:111
 hwallunpacker.cc:112
 hwallunpacker.cc:113
 hwallunpacker.cc:114
 hwallunpacker.cc:115
 hwallunpacker.cc:116
 hwallunpacker.cc:117
 hwallunpacker.cc:118
 hwallunpacker.cc:119
 hwallunpacker.cc:120
 hwallunpacker.cc:121
 hwallunpacker.cc:122
 hwallunpacker.cc:123
 hwallunpacker.cc:124
 hwallunpacker.cc:125
 hwallunpacker.cc:126
 hwallunpacker.cc:127
 hwallunpacker.cc:128
 hwallunpacker.cc:129
 hwallunpacker.cc:130
 hwallunpacker.cc:131
 hwallunpacker.cc:132
 hwallunpacker.cc:133
 hwallunpacker.cc:134
 hwallunpacker.cc:135
 hwallunpacker.cc:136
 hwallunpacker.cc:137
 hwallunpacker.cc:138
 hwallunpacker.cc:139
 hwallunpacker.cc:140
 hwallunpacker.cc:141
 hwallunpacker.cc:142
 hwallunpacker.cc:143
 hwallunpacker.cc:144
 hwallunpacker.cc:145
 hwallunpacker.cc:146
 hwallunpacker.cc:147
 hwallunpacker.cc:148
 hwallunpacker.cc:149
 hwallunpacker.cc:150
 hwallunpacker.cc:151
 hwallunpacker.cc:152
 hwallunpacker.cc:153
 hwallunpacker.cc:154
 hwallunpacker.cc:155
 hwallunpacker.cc:156
 hwallunpacker.cc:157
 hwallunpacker.cc:158
 hwallunpacker.cc:159
 hwallunpacker.cc:160
 hwallunpacker.cc:161
 hwallunpacker.cc:162
 hwallunpacker.cc:163
 hwallunpacker.cc:164
 hwallunpacker.cc:165
 hwallunpacker.cc:166
 hwallunpacker.cc:167
 hwallunpacker.cc:168
 hwallunpacker.cc:169
 hwallunpacker.cc:170
 hwallunpacker.cc:171
 hwallunpacker.cc:172
 hwallunpacker.cc:173
 hwallunpacker.cc:174
 hwallunpacker.cc:175
 hwallunpacker.cc:176
 hwallunpacker.cc:177
 hwallunpacker.cc:178
 hwallunpacker.cc:179
 hwallunpacker.cc:180
 hwallunpacker.cc:181
 hwallunpacker.cc:182
 hwallunpacker.cc:183
 hwallunpacker.cc:184
 hwallunpacker.cc:185
 hwallunpacker.cc:186
 hwallunpacker.cc:187
 hwallunpacker.cc:188
 hwallunpacker.cc:189
 hwallunpacker.cc:190
 hwallunpacker.cc:191
 hwallunpacker.cc:192
 hwallunpacker.cc:193
 hwallunpacker.cc:194
 hwallunpacker.cc:195
 hwallunpacker.cc:196
 hwallunpacker.cc:197
 hwallunpacker.cc:198
 hwallunpacker.cc:199
 hwallunpacker.cc:200
 hwallunpacker.cc:201
 hwallunpacker.cc:202
 hwallunpacker.cc:203
 hwallunpacker.cc:204
 hwallunpacker.cc:205
 hwallunpacker.cc:206
 hwallunpacker.cc:207