ROOT logo
// File: showerunpacker.cc
//
// Author: Leszek Kidon
// Last update: 25/5/2000
// Last update: 6/7/2010
//
using namespace std;
#include "hades.h"
#include "hevent.h"
#include "hspectrometer.h"
#include "hdetector.h"
#include "hcategory.h"
#include "hldsubevt.h"
#include "hdebug.h"
#include "heventheader.h"
#include "hshowerunpacker.h"
#include "hshowerrawhist.h"
#include "hshowerraw.h"

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

HShowerUnpacker::HShowerUnpacker(Int_t nSubId) {
  m_nSubId = nSubId;
  pRawCat = NULL;
  fEventNr = 0;
  m_loc.set(4,0,0,0,0);
  m_zeroLoc.set(0);
}

HShowerUnpacker::~HShowerUnpacker(void) {
}

Bool_t HShowerUnpacker::init(void) {
  pRawCat=gHades->getCurrentEvent()->getCategory(catShowerRaw);
  if (!pRawCat) {         
    pRawCat=gHades->getSetup()->getDetector("Shower")
                             ->buildCategory(catShowerRaw);
    if (!pRawCat) {   
      return kFALSE;  
    } else {
      gHades->getCurrentEvent()->addCategory(catShowerRaw,pRawCat,"Shower");
    }         
  }

  return kTRUE;
}

Bool_t HShowerUnpacker::reinit(void) {
  return kTRUE;
}

Int_t HShowerUnpacker::fillData(class HLocation &loc, Int_t nCharge, Int_t nEvtId) {
  static long i = 0;
  HShowerRaw *pCell = NULL;
  // check the location of the data, should not be out of the detector !!!
  Long_t nEvt=gHades->getCurrentEvent()->getHeader()->getEventSeqNumber();
  if(loc[0]<0 || loc[0]>5 || loc[1]<0 || loc[1]>2 || loc[2]<0 || loc[2]>31 || loc[3]<0 || loc[3]>31){
      Error("fillData"," EventSeqNumber: %ld  ShowerUnpack: data out of range: sec: %d  mod: %d  row: %d  col: %d  data: %d  error nr: %ld\n",
	    nEvt, loc[0], loc[1], loc[2], loc[3], nCharge, i++);
    return 0;
  }

//cout << "ShowerUnpacker fill data ******************* " << endl;

  pCell = (HShowerRaw*) pRawCat->getNewSlot(m_zeroLoc);
  if (pCell != NULL) {
      pCell = new(pCell) HShowerRaw;

      pCell->setCharge(nCharge);
      pCell->setSector(loc[0]);
      pCell->setModule(loc[1]);
      pCell->setRow(loc[2]);
      pCell->setCol(loc[3]);
      pCell->setEventType(nEvtId&0xFF);
      pCell->setEventNr(nEvt);

  }
  else
  {
      Error("HShowerUnpacker::execute","Not enough memory");
      return 0;
  }
  return 1;
}

Int_t HShowerUnpacker::execute() {
  SHeader dHeader;
  Int_t nEvtId;
  nEvtId = gHades->getCurrentEvent()->getHeader()->getId();
  fEventNr = gHades->getCurrentEvent()->getHeader()->getEventSeqNumber();

//cout << "ShowerUnpacker execute ******************* "<< fEventNr << " ( " << nEvtId << " ) " << endl;

  if (pSubEvt) {

//cout << "ShowerUnpacker execute pSubEvt "<< pSubEvt << endl;
    UInt_t dataLen = pSubEvt->getDataLen();
    UInt_t sumHeaderSize = 0;
    //fprintf(stderr, "datalen: %d\n", datalen);
    for (UInt_t i = 0; i < dataLen; i++) {

//cout << "ShowerUnpacker execute pSubEvt "<< pSubEvt->getDataLen() << endl;

       // decode subsub_event and its header
       // **************** DUMP ************************
       //dHeader.dump();
       // **************** DUMP ************************

       dHeader.getEvent(pSubEvt->getData()[i]);
       //fprintf(stderr, " %d:header:%x\n", i, pSubEvt->getData()[i]);
       if (0 == dHeader.getSize()) {
          // no data im subsubevent
          continue;
       }

       //cout << " ------------------- SUBEVt HEADER: SIZE " << hex << dHeader.getSize() << " SOURCE: " << hex << dHeader.getSource() << endl;

       // After decoding and checking the header jump to the next data word
       i++;
       fDataWord.sector = 0;

       fDataWord.sector = ( dHeader.getSource() & 0x00F0 ) >> 4;

         // Read the sub-sub event
	 UInt_t headerSize = dHeader.getSize();
	 sumHeaderSize += headerSize;
	 UInt_t sumAdcSize = 0;
         for (UInt_t j = 0; j < headerSize && i < dataLen; j++, i++) {
            // Check for debug word (skip)

            // check the source (shower - 32) and the endpoint number (1 or 2) - if not the case - skip
            if ( !(  ((dHeader.getSource() & 0xFF0F) == 0x3201) || ((dHeader.getSource() & 0xFF0F) == 0x3202) ||  ((dHeader.getSource() & 0xFFFF) == 0x5555) )  ) continue;

      //      if ( (i%4) == 0 ) cout << endl; 
      //    printf("0x%04x:0x%04x\t", pSubEvt->getData() + i, pSubEvt->getData()[i]);

            if ((dHeader.getSource() & 0xFFFF) != 0x5555)
	    {
	      if ((pSubEvt->getData()[i] & 0xFFFF) != 0x5555)
	      {
	               m_loc[0]=m_loc[1]=m_loc[2]=m_loc[3]=0;
		       fDataWord.module  = 0;
		       fDataWord.row = 0;
		       fDataWord.column     = 0;
		       fDataWord.charge     = 0;




            // Fill the data structure
                       fDataWord.module  = ((pSubEvt->getData()[i] & 0x30000000)  >> 28);   //0xF0000000 -> 0x30000000 - module masking
                       fDataWord.row = ((pSubEvt->getData()[i] & 0x01F00000) >> 20);        //0x0FF00000 -> 0x01F00000 - row masking
                       fDataWord.column     = ((pSubEvt->getData()[i] & 0x0001F000) >> 12); //0x000FF000 -> 0x0001F000 - col masking
                       fDataWord.charge     = (pSubEvt->getData()[i] & 0x000003FF);         //0x00000FFF -> 0x000003FF - charge masking


                        if ((pSubEvt->getData()[i] & 0xFFF) == 0xBAD) 
			{
			    /*fprintf(stderr, " %x:data:%x", k, pSubEvt->getData()[i]);
			    k = 0;*/
			    UInt_t adcSize = ((pSubEvt->getData()[i] & 0x7FF000) >> 12);
			    sumAdcSize += adcSize;

			}
			else
			{
                            m_loc[0]=fDataWord.sector;
                            m_loc[1]=fDataWord.module;
              	            m_loc[2]=fDataWord.row;
              	            m_loc[3]=fDataWord.column;
               	            fillData(m_loc, fDataWord.charge, nEvtId);
			}

	       }
	       else
	       {
//			i = i + ((pSubEvt->getData()[i] & 0xFFFF0000) >> 16);
	       }
	    }

         }
	 if (headerSize != sumAdcSize && sumAdcSize)
	 {
	     Error("execute", " EventSeqNumber: %d  Size from subevent: header = %d; size from adc = %d\n", fEventNr, headerSize, sumAdcSize);
	 }
	 // end of unpacking subsub event
	 //cout << endl;
         // rewind by one data word
         i--;

    } // end of unpacking sub event
    /*if (dataLen - sumHeaderSize != 11)
    {
        fprintf(stderr, "Data length = %d; Size from subevent headers = %d\n", dataLen, sumHeaderSize);
    }*/
  }
// printf("ii unpack = %d\n", ii);
//  dump();

  return 1;
}


Bool_t HShowerUnpacker::finalize() {
  return kTRUE;
}

Int_t HShowerUnpacker::dump() {
  if (pSubEvt) {
    UInt_t i = 0;
    cout << endl;
    while (i < pSubEvt->getDataLen()) {
       cout << "Dump must be implemented!" << endl;
       i++;
    }
    cout << endl;
  }

  return 1;
}





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