ROOT logo
//*-- AUTHOR : Pavel Tlusty
//*-- Created : 15/06/2010
//*-- based on hstart2trb2unpacker

//_HADES_CLASS_DESCRIPTION
/////////////////////////////////////////////////////////////
//
//  HTofTrbUnpacker
//
//  Class for unpacking TRB data and filling
//    the Tof Raw category
//  Base subevent decoding is performed by decode function
//  see: htrbbaseunpacker.h (/base/datasource/ )
//
/////////////////////////////////////////////////////////////

using namespace std;
#include "htoftrb2unpacker.h"
#include "tofdef.h"
#include "htofdetector.h"
#include "htoftrb2lookup.h"
#include "htrbnetaddressmapping.h"
#include "htrb2correction.h"
#include "htofraw.h"
#include "htofrawsim.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 (HTofTrb2Unpacker)

HTofTrb2Unpacker::HTofTrb2Unpacker (UInt_t id):HTrb2Unpacker (id)
{
  // constructor
  pRawCat = NULL;
  timeRef = kTRUE;
  timeRefRemove = kFALSE;
  timeShift = 7200.;
}

Bool_t HTofTrb2Unpacker::init (void)
{

//  cout<<"====tof init ...0"<<endl;
  // creates the raw category and gets the pointer to the TRB lookup table
  pRawCat = gHades->getCurrentEvent ()->getCategory (catTofRaw);
  if (!pRawCat) {

      if(gHades->getEmbeddingMode()==0) {
	  pRawCat=gHades->getSetup()->getDetector("Tof")->buildCategory(catTofRaw);
      } else {
	  pRawCat=((HTofDetector*)(gHades->getSetup()->getDetector("Tof")))->buildMatrixCategory("HTofRawSim",0.5F);
      }
      if (!pRawCat) return kFALSE;

      gHades->getCurrentEvent ()->addCategory (catTofRaw, pRawCat, "Tof");
  }
  loc.set(3,0,0,0);

  lookup = (HTofTrb2Lookup *) (gHades->getRuntimeDb ()->getContainer ("TofTrb2Lookup"));
  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 HTofTrb2Unpacker::finalize(void)
{

  if(!quietMode && debugFlag > 0 ){
    Info("finalize","No control histograms implemented");
  }
  return kTRUE;
}

Int_t HTofTrb2Unpacker::execute (void)
{
   HTofRaw *pRaw = 0;    // pointer to Raw category
   Int_t nEvt = 0;        // Evt Seq. Nb.


   if (gHades->isCalibration ()) {
      //calibration event
      return 1;
   }

   if (gHades->getCurrentEvent()->getHeader()->getId() == 0xe) {
      //scaler event
      return 1;
   }

   // decode subevent data
   if (pSubEvt) {        // pSubEvt - make sure that there is something for decoding
      nEvt = gHades->getCurrentEvent ()->getHeader ()->getEventSeqNumber ();

    // decodes the subevent and fill arrays, see: htrb2unpacker.h
    // decode() return values: 
    //    100  - end of subevent, stop decoding by purpose
    //      0  - error, decoding failed
    //      1  - data  decode     
    // decode all subsubevents until subevent is finished - even if there is an error 
    Int_t DecodeOutput = 1; 
    while( 1 ) { 
      DecodeOutput = decode();
      // go to next TRB
      uStartPosition = uStartPosition + uSubBlockSize + 1; //start TRB decoding at the correct start position
      if (DecodeOutput==0) { //stop decoding of current TRB and go to next
	Error("decode","subsubevent decoding failed!!! Evt Nr : %i SubEvtId: %x",nEvt,subEvtId);
	continue;
      }
      if (DecodeOutput==100) { //End of subevebt is reached
        uStartPosition=0;
	if ( debugFlag > 0 ) Info("decode","DecodeOutput : %i",DecodeOutput);
	break;
      }

    // if(!decode()) return(-1); <--- for old data structure

//		cout << "***** TOF: trbDataExtension *****"<<endl;
//		cout << trbDataVer <<": "<< trbExtensionSize <<endl;


      //for (Int_t i = 0; i < 128; i++) {
      //  if(trbLeadingTime[i][0]>0) printf("%d %.0f %d\n",i,trbLeadingTime[i][0],trbLeadingMult[i]);
      //}

      //Ref Time Subtraction
      //cout <<"timeRef "<<timeRef<<endl;
      if(timeRef)  correctRefTimeCh(32);
    

      // get the part of the lookup table which belongs to the subevent ID
      // in order to check which TRBchannel belongs to the TofDetector

      HTofTrb2LookupBoard *board = lookup->getBoard (uTrbNetAdress);
      if (!board) {
         Warning ("execute", "TrbNetAdress: %x (%i) unpacked but TRB Board not in lookup table",uTrbNetAdress,uTrbNetAdress);
         return 1;
      }

      //fill RAW category for Tof detector
      for (Int_t i=0; i<board->getSize(); i++) {

	if (trbLeadingMult[i] < 1 ) continue;        //Leading Time data for this channel exist        

	if ( debugFlag > 0 ) cout <<"timeRefRemove "<<timeRefRemove<<" timeShift "<<timeShift<<endl;

        if(timeRefRemove) {  //remove reference times
          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]);

         // fill tof raw category
         HTofTrb2LookupChan *chan = board->getChannel (i);


         loc[0] = (Int_t)chan->getSector();
         loc[1] = (Int_t)chan->getModule();
         loc[2] = (Int_t)chan->getCell();
         Char_t side   = (Char_t)chan->getSide();

	 if ( debugFlag > 0 ) printf("chan %d sector %d module %d cell %d side %d\n",i,loc[0],loc[1],loc[2],side);

         if(loc[0]>=0) {

            pRaw=(HTofRaw *)pRawCat->getObject(loc);
            if (!pRaw){
              pRaw=(HTofRaw *)pRawCat->getSlot(loc);
	      if (pRaw) {

		  if(gHades->getEmbeddingMode()==0) {
		  pRaw=new (pRaw) HTofRaw;
	          }else{
		    pRaw=new (pRaw) HTofRawSim;
	          }
                  pRaw->setSector((Char_t)loc[0]);
                  pRaw->setModule((Char_t)loc[1]);
                  pRaw->setCell((Char_t)loc[2]);
              }
              else {
                Error("execute()",
                "Can't get slot\n");
                return -1;
              }
            }
            else {
              #if DEBUG_LEVEL>4
              Info("execute()",
              "Slot already exists!\n");
              #endif
            }


            // fill only one time info !!!! 
            // todo: convert invalid time from unpacker -> invalid time in tof class ... all negative are invalid?
            Int_t m;
            m=trbLeadingMult[i];
            if(m>10) m=10;// no more than 10 Times stored
            
	    if(side=='r') {
                pRaw->setRightTime(trbLeadingTime[i][0]);
                pRaw->setRightCharge(trbADC[i][0]);
                }
	    else {
                pRaw->setLeftTime(trbLeadingTime[i][0]);
                pRaw->setLeftCharge(trbADC[i][0]);
                } 

	 } //sector>=0

      }// for(Int_t i=0; i<128; i++)
    } // while loop
   }//if(pSubEvt)

   return 1;
}

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