LCOV - code coverage report
Current view: top level - RAW/RAWDatarec - AliRawReaderRoot.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 168 444 37.8 %
Date: 2016-06-14 17:26:59 Functions: 24 42 57.1 %

          Line data    Source code
       1             : /**************************************************************************
       2             :  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
       3             :  *                                                                        *
       4             :  * Author: The ALICE Off-line Project.                                    *
       5             :  * Contributors are mentioned in the code where appropriate.              *
       6             :  *                                                                        *
       7             :  * Permission to use, copy, modify and distribute this software and its   *
       8             :  * documentation strictly for non-commercial purposes is hereby granted   *
       9             :  * without fee, provided that the above copyright notice appears in all   *
      10             :  * copies and that both the copyright notice and this permission notice   *
      11             :  * appear in the supporting documentation. The authors make no claims     *
      12             :  * about the suitability of this software for any purpose. It is          *
      13             :  * provided "as is" without express or implied warranty.                  *
      14             :  **************************************************************************/
      15             : 
      16             : /* $Id$ */
      17             : 
      18             : ///////////////////////////////////////////////////////////////////////////////
      19             : ///
      20             : /// This is a class for reading raw data from a root file.
      21             : ///
      22             : /// The root file is expected to contain a tree of name "RAW" with
      23             : /// a branch of name "rawevent" which contains objects of type
      24             : /// AliRawVEvent.
      25             : /// 
      26             : /// The file name and the event number are arguments of the constructor
      27             : /// of AliRawReaderRoot.
      28             : ///
      29             : ///////////////////////////////////////////////////////////////////////////////
      30             : 
      31             : #include <TFile.h>
      32             : #include <TTree.h>
      33             : #include <TTreeIndex.h>
      34             : #include <TGrid.h>
      35             : #include "AliRawReaderRoot.h"
      36             : #include "AliRawVEvent.h"
      37             : #include "AliRawEventHeaderBase.h"
      38             : #include "AliRawVEquipment.h"
      39             : #include "AliRawEquipmentHeader.h"
      40             : #include "AliRawData.h"
      41             : 
      42             : 
      43         128 : ClassImp(AliRawReaderRoot)
      44             : Bool_t AliRawReaderRoot::fgUseOrder = kFALSE;
      45             : 
      46             : 
      47           0 : AliRawReaderRoot::AliRawReaderRoot() :
      48           0 :   fFile(NULL),
      49           0 :   fBranch(NULL),
      50           0 :   fEventIndex(-1),
      51           0 :   fEvent(NULL),
      52           0 :   fEventHeader(NULL),
      53           0 :   fSubEventIndex(0),
      54           0 :   fSubEvent(NULL),
      55           0 :   fEquipmentIndex(0),
      56           0 :   fEquipment(NULL),
      57           0 :   fRawData(NULL),
      58           0 :   fPosition(NULL),
      59           0 :   fEnd(NULL),
      60           0 :   fIndex(0x0)
      61           0 : {
      62             : // default constructor
      63             : 
      64           0 : }
      65             : 
      66           1 : AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
      67           1 :   fFile(NULL),
      68           1 :   fBranch(NULL),
      69           1 :   fEventIndex(eventNumber),
      70           1 :   fEvent(NULL),
      71           1 :   fEventHeader(NULL),
      72           1 :   fSubEventIndex(0),
      73           1 :   fSubEvent(NULL),
      74           1 :   fEquipmentIndex(0),
      75           1 :   fEquipment(NULL),
      76           1 :   fRawData(NULL),
      77           1 :   fPosition(NULL),
      78           1 :   fEnd(NULL),
      79           1 :   fIndex(0x0)
      80           5 : {
      81             : // create an object to read digits from the given input file for the
      82             : // event with the given number
      83             : 
      84           2 :   TDirectory* dir = gDirectory;
      85           1 :   TString flStr = fileName;
      86           2 :   if (flStr.BeginsWith("alien://") && !gGrid) TGrid::Connect("alien://");
      87           2 :   fFile = TFile::Open(fileName);
      88           1 :   dir->cd();
      89           3 :   if (!fFile || !fFile->IsOpen()) {
      90           0 :     Error("AliRawReaderRoot", "could not open file %s", fileName);
      91           0 :     fIsValid = kFALSE;
      92           0 :     return;
      93             :   }
      94           2 :   TTree* tree = (TTree*) fFile->Get("RAW");
      95           1 :   if (!tree) {
      96           0 :     Error("AliRawReaderRoot", "no raw data tree found");
      97           0 :     fIsValid = kFALSE;
      98           0 :     return;
      99             :   }
     100           2 :   fBranch = tree->GetBranch("rawevent");
     101           1 :   if (!fBranch) {
     102           0 :     Error("AliRawReaderRoot", "no raw data branch found");
     103           0 :     fIsValid = kFALSE;
     104           0 :     return;
     105             :   }
     106             : 
     107           1 :   fBranch->SetAddress(&fEvent);
     108           1 :   if (fEventIndex >= 0) {
     109           0 :     if (fBranch->GetEntry(fEventIndex) <= 0) {
     110           0 :       Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
     111           0 :       fIsValid = kFALSE;
     112           0 :       return;
     113             :     }
     114           0 :     fEventHeader = fEvent->GetHeader();
     115           0 :   }
     116           3 : }
     117             : 
     118           0 : AliRawReaderRoot::AliRawReaderRoot(AliRawVEvent* event, Int_t evId) :
     119           0 :   fFile(NULL),
     120           0 :   fBranch(NULL),
     121           0 :   fEventIndex(evId),
     122           0 :   fEvent(event),
     123           0 :   fEventHeader(event->GetHeader()),
     124           0 :   fSubEventIndex(0),
     125           0 :   fSubEvent(NULL),
     126           0 :   fEquipmentIndex(0),
     127           0 :   fEquipment(NULL),
     128           0 :   fRawData(NULL),
     129           0 :   fPosition(NULL),
     130           0 :   fEnd(NULL),
     131           0 :   fIndex(0x0)
     132           0 : {
     133             : // create an object to read digits from the given raw event
     134           0 :   if (!fEvent) fIsValid = kFALSE;
     135           0 : }
     136             : 
     137             : AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
     138           0 :   AliRawReader(rawReader),
     139           0 :   fFile(NULL),
     140           0 :   fBranch(NULL),
     141           0 :   fEventIndex(rawReader.fEventIndex),
     142           0 :   fEvent(NULL),
     143           0 :   fEventHeader(NULL),
     144           0 :   fSubEventIndex(rawReader.fSubEventIndex),
     145           0 :   fSubEvent(NULL),
     146           0 :   fEquipmentIndex(rawReader.fEquipmentIndex),
     147           0 :   fEquipment(NULL),
     148           0 :   fRawData(NULL),
     149           0 :   fPosition(NULL),
     150           0 :   fEnd(NULL),
     151           0 :   fIndex(0x0)
     152           0 : {
     153             : // copy constructor
     154             : 
     155           0 :   if (rawReader.fFile) {
     156           0 :     TDirectory* dir = gDirectory;    
     157           0 :     fFile = TFile::Open(rawReader.fFile->GetName());
     158           0 :     dir->cd();
     159           0 :     if (!fFile || !fFile->IsOpen()) {
     160           0 :       Error("AliRawReaderRoot", "could not open file %s", 
     161           0 :             rawReader.fFile->GetName());
     162           0 :       fIsValid = kFALSE;
     163           0 :       return;
     164             :     }
     165           0 :     TTree* tree = (TTree*) fFile->Get("RAW");
     166           0 :     if (!tree) {
     167           0 :       Error("AliRawReaderRoot", "no raw data tree found");
     168           0 :       fIsValid = kFALSE;
     169           0 :       return;
     170             :     }
     171           0 :     fBranch = tree->GetBranch("rawevent");
     172           0 :     if (!fBranch) {
     173           0 :       Error("AliRawReaderRoot", "no raw data branch found");
     174           0 :       fIsValid = kFALSE;
     175           0 :       return;
     176             :     }
     177             : 
     178           0 :     fBranch->SetAddress(&fEvent);
     179           0 :     if (fEventIndex >= 0) {
     180           0 :       if (fBranch->GetEntry(fEventIndex) <= 0) {
     181           0 :         Error("AliRawReaderRoot", "no event with number %d found", 
     182           0 :               fEventIndex);
     183           0 :         fIsValid = kFALSE;
     184           0 :         return;
     185             :       }
     186           0 :       fEventHeader = fEvent->GetHeader();
     187           0 :     }
     188           0 :   } else {
     189           0 :     fEvent = rawReader.fEvent;
     190           0 :     fEventHeader = rawReader.fEventHeader;
     191             :   }
     192             : 
     193           0 :   if (fSubEventIndex > 0) {
     194           0 :     fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
     195           0 :     fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
     196           0 :     fRawData = fEquipment->GetRawData();
     197           0 :     fCount = 0;
     198           0 :     fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() + 
     199           0 :       ((UChar_t*) rawReader.fHeader - 
     200           0 :        (UChar_t*) rawReader.fRawData->GetBuffer()));
     201             :     // Now check the version of the header
     202             :     UChar_t version = 2;
     203           0 :     if (fHeader) version = fHeader->GetVersion();
     204           0 :     if (version==3) {
     205           0 :       fHeader = NULL;
     206           0 :       fHeaderV3 = (AliRawDataHeaderV3*) ((UChar_t*) fRawData->GetBuffer() + 
     207           0 :       ((UChar_t*) rawReader.fHeaderV3 - 
     208           0 :        (UChar_t*) rawReader.fRawData->GetBuffer()));
     209           0 :     }
     210           0 :     fPosition = (UChar_t*) fRawData->GetBuffer() + 
     211           0 :       (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
     212           0 :     fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
     213           0 :   }
     214           0 : }
     215             : 
     216             : AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& 
     217             :                                                 rawReader)
     218             : {
     219             : // assignment operator
     220             : 
     221           0 :   this->~AliRawReaderRoot();
     222           0 :   new(this) AliRawReaderRoot(rawReader);
     223           0 :   return *this;
     224           0 : }
     225             : 
     226             : AliRawReaderRoot::~AliRawReaderRoot()
     227           6 : {
     228             : // delete objects and close root file
     229             : 
     230           1 :   if (fFile) {
     231           3 :     if (fEvent) delete fEvent;
     232           1 :     fFile->Close();
     233           2 :     delete fFile;
     234             :   }
     235           3 : }
     236             : 
     237             : const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
     238             : {
     239             :   // Get the even header
     240             :   // Return NULL in case of failure
     241           8 :   return fEventHeader;
     242             : }
     243             : 
     244             : UInt_t AliRawReaderRoot::GetType() const
     245             : {
     246             : // get the type from the event header
     247             : 
     248        1274 :   if (!fEventHeader) return 0;
     249         637 :   return fEventHeader->Get("Type");
     250         637 : }
     251             : 
     252             : UInt_t AliRawReaderRoot::GetRunNumber() const
     253             : {
     254             : // get the run number from the event header
     255             : 
     256         138 :   if (!fEventHeader) return 0;
     257          69 :   return fEventHeader->Get("RunNb");
     258          69 : }
     259             : 
     260             : const UInt_t* AliRawReaderRoot::GetEventId() const
     261             : {
     262             : // get the event id from the event header
     263             : 
     264        9880 :   if (!fEventHeader) return NULL;
     265        4940 :   return fEventHeader->GetP("Id");
     266        4940 : }
     267             : 
     268             : const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
     269             : {
     270             : // get the trigger pattern from the event header
     271             : 
     272          20 :   if (!fEventHeader) return NULL;
     273          10 :   return fEventHeader->GetP("TriggerPattern");
     274          10 : }
     275             : 
     276             : const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
     277             : {
     278             : // get the detector pattern from the event header
     279             : 
     280          18 :   if (!fEventHeader) return NULL;
     281           9 :   return fEventHeader->GetP("DetectorPattern");
     282           9 : }
     283             : 
     284             : const UInt_t* AliRawReaderRoot::GetAttributes() const
     285             : {
     286             : // get the type attributes from the event header
     287             : 
     288           8 :   if (!fEventHeader) return NULL;
     289           4 :   return fEventHeader->GetP("TypeAttribute");
     290           4 : }
     291             : 
     292             : const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
     293             : {
     294             : // get the type attributes from the sub event header
     295             : 
     296           0 :   if (!fSubEvent) return NULL;
     297           0 :   return fSubEvent->GetHeader()->GetP("TypeAttribute");
     298           0 : }
     299             : 
     300             : UInt_t AliRawReaderRoot::GetLDCId() const
     301             : {
     302             : // get the LDC Id from the event header
     303             : 
     304         168 :   if (!fEvent || !fSubEvent) return 0;
     305          56 :   return fSubEvent->GetHeader()->Get("LdcId");
     306          56 : }
     307             : 
     308             : UInt_t AliRawReaderRoot::GetGDCId() const
     309             : {
     310             : // get the GDC Id from the event header
     311             : 
     312           0 :   if (!fEventHeader) return 0;
     313           0 :   return fEventHeader->Get("GdcId");
     314           0 : }
     315             : 
     316             : UInt_t AliRawReaderRoot::GetTimestamp() const
     317             : {
     318         136 :   if (!fEventHeader) return 0;
     319          68 :   return fEventHeader->Get("Timestamp");
     320          68 : }
     321             : 
     322             : Int_t AliRawReaderRoot::GetEquipmentSize() const
     323             : {
     324             : // get the size of the equipment
     325             : 
     326          80 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
     327          20 :   return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
     328          20 : }
     329             : 
     330             : Int_t AliRawReaderRoot::GetEquipmentType() const
     331             : {
     332             : // get the type from the equipment header
     333             : 
     334           0 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
     335           0 :   return fEquipment->GetEquipmentHeader()->GetEquipmentType();
     336           0 : }
     337             : 
     338             : Int_t AliRawReaderRoot::GetEquipmentId() const
     339             : {
     340             : // get the ID from the equipment header
     341             : 
     342     3158960 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
     343      789740 :   return fEquipment->GetEquipmentHeader()->GetId();
     344      789740 : }
     345             : 
     346             : const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
     347             : {
     348             : // get the attributes from the equipment header
     349             : 
     350           0 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
     351           0 :   return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
     352           0 : }
     353             : 
     354             : Int_t AliRawReaderRoot::GetEquipmentElementSize() const
     355             : {
     356             : // get the basic element size from the equipment header
     357             : 
     358           0 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
     359           0 :   return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
     360           0 : }
     361             : 
     362             : Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
     363             : {
     364             : // get the size of the equipment header (28 bytes by default)
     365             : 
     366          80 :   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
     367          20 :   return fEquipment->GetEquipmentHeader()->HeaderSize();
     368          20 : }
     369             : 
     370             : // _________________________________________________________________________
     371             : void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
     372             :   // The method swaps the contents of the
     373             :   // raw-data event header
     374           0 :   UInt_t  intCount = (size+3)/sizeof(UInt_t);
     375             : 
     376           0 :   UInt_t* buf = (UInt_t*) inbuf;    // temporary integers buffer
     377           0 :   for (UInt_t i=0; i<intCount; i++, buf++) {
     378           0 :       UInt_t value = SwapWord(*buf);
     379           0 :       if (i==(intCount-1))
     380           0 :          memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
     381             :       else
     382           0 :          memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
     383           0 :   }
     384           0 : }
     385             : // _________________________________________________________________________
     386             : 
     387             : Bool_t AliRawReaderRoot::ReadHeader()
     388             : {
     389             : // read a data header at the current position
     390             : // returns kFALSE if the data header could not be read
     391             : 
     392        4560 :   fErrorCode = 0;
     393        2280 :   if (!fEvent) return kFALSE;
     394             : 
     395             :   do {
     396             :     // skip payload (if event was not selected)
     397      356735 :     if (fCount > 0) fPosition += fCount;
     398             : 
     399             :     // get the first or the next equipment if at the end of an equipment
     400      712446 :     if (!fEquipment || (fPosition >= fEnd)) {
     401             : 
     402             :       // get the first or the next sub event if at the end of a sub event
     403      712446 :       if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
     404             : 
     405             :         // check for end of event data
     406       97260 :         if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
     407       96580 :         fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
     408             : 
     409             :         // check the magic word of the sub event
     410       96580 :         if (!fSubEvent->GetHeader()->IsValid()) {
     411           0 :           Error("ReadHeader", "wrong magic number in sub event!");
     412           0 :           fSubEvent->GetHeader()->Dump();
     413           0 :           fErrorCode = kErrMagic;
     414           0 :           return kFALSE;
     415             :         }
     416             : 
     417       96580 :         fEquipmentIndex = 0;
     418       96580 :         fEquipment = NULL;
     419       96580 :         fRawData = NULL;
     420       96580 :       }
     421             : 
     422             :       // get the next equipment and raw data
     423      356395 :       fCount = 0;
     424      356395 :       if (fEquipmentIndex >= fSubEvent->GetNEquipments()) {
     425           0 :         fEquipment = NULL;
     426           0 :         continue;
     427             :       }
     428      356395 :       fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
     429      356395 :       if (!fEquipment) continue;
     430      356395 :       if (!IsSelected()) {
     431      354455 :         fPosition = fEnd;
     432      354455 :         continue;
     433             :       }
     434        1940 :       fRawData = fEquipment->GetRawData();
     435        1940 :       if (!fRawData) {
     436           0 :         fPosition = fEnd;
     437           0 :         continue;
     438             :       }
     439        1940 :       fPosition = (UChar_t*) fRawData->GetBuffer();
     440        1940 :       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
     441        1940 :     }
     442             : 
     443             :     // continue with the next equipment if no data left in the payload
     444        1940 :     if (fPosition >= fEnd) continue;
     445             : 
     446        1940 :     if (fRequireHeader) {
     447             :       // check that there are enough bytes left for the data header
     448        1936 :       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
     449           0 :         Error("ReadHeader", "could not read data header!");
     450           0 :         Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
     451           0 :         fEquipment->GetEquipmentHeader()->Dump();
     452           0 :         fCount = 0;
     453           0 :         fPosition = fEnd;
     454           0 :         fErrorCode = kErrNoDataHeader;
     455           0 :         continue;
     456             :       }
     457             : 
     458             :       // "read" the data header
     459        1936 :       fHeader = (AliRawDataHeader*) fPosition;
     460             :       // Now check the version of the header
     461             :       UChar_t version = 2;
     462        3872 :       if (fHeader) version=fHeader->GetVersion();
     463        1936 :       if (version==2) {
     464             :  #ifndef R__BYTESWAP
     465             :         SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
     466             :         fHeader=fHeaderSwapped;
     467             : #endif
     468           0 :         if ((fPosition + fHeader->fSize) != fEnd) {
     469           0 :           if (fHeader->fSize != 0xFFFFFFFF)
     470           0 :             Warning("ReadHeader",
     471             :                     "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
     472           0 :                     fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
     473           0 :           fHeader->fSize = fEnd - fPosition;
     474           0 :         }
     475           0 :         fPosition += sizeof(AliRawDataHeader);
     476           0 :         fHeaderV3 = 0;
     477        1936 :       } else if (version==3) {
     478        1936 :         fHeaderV3 = (AliRawDataHeaderV3*) fPosition;
     479             : #ifndef R__BYTESWAP
     480             :         SwapData((void*) fHeaderV3, (void*) fHeaderSwapped, sizeof(AliRawDataHeaderV3));
     481             :         fHeaderV3=fHeaderSwappedV3;
     482             : #endif
     483        1936 :         if ((fPosition + fHeaderV3->fSize) != fEnd) {
     484         841 :           if (fHeaderV3->fSize != 0xFFFFFFFF)
     485           0 :             Warning("ReadHeader",
     486             :                     "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
     487           0 :                     fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
     488         841 :           fHeaderV3->fSize = fEnd - fPosition;
     489         841 :         }
     490        1936 :         fPosition += sizeof(AliRawDataHeaderV3);
     491        1936 :         fHeader = 0;
     492        1936 :       }
     493        1936 :     }
     494             : 
     495        1940 :     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
     496           0 :       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
     497             : 
     498             :       // check consistency of data size in the header and in the sub event
     499           0 :       if (fPosition + fCount > fEnd) {  
     500           0 :         Error("ReadHeader", "size in data header exceeds event size!");
     501           0 :         Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
     502           0 :         fEquipment->GetEquipmentHeader()->Dump();
     503           0 :         fCount = 0;
     504           0 :         fPosition = fEnd;
     505           0 :         fErrorCode = kErrSize;
     506           0 :         continue;
     507             :       }
     508        3876 :     } else if (fHeaderV3 && (fHeaderV3->fSize != 0xFFFFFFFF)) {
     509        1936 :       fCount = fHeaderV3->fSize - sizeof(AliRawDataHeaderV3);
     510             : 
     511             :       // check consistency of data size in the header and in the sub event
     512        1936 :       if (fPosition + fCount > fEnd) {  
     513           0 :         Error("ReadHeader", "size in data header exceeds event size!");
     514           0 :         Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
     515           0 :         fEquipment->GetEquipmentHeader()->Dump();
     516           0 :         fCount = 0;
     517           0 :         fPosition = fEnd;
     518           0 :         fErrorCode = kErrSize;
     519           0 :         continue;
     520             :       }
     521             : 
     522             :     } else {
     523           4 :       fCount = fEnd - fPosition;
     524             :     }
     525             : 
     526      356395 :   } while (!IsSelected());
     527             : 
     528        1940 :   return kTRUE;
     529        2280 : }
     530             : 
     531             : Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
     532             : {
     533             : // reads the next payload at the current position
     534             : // returns kFALSE if the data could not be read
     535             : 
     536        2696 :   fErrorCode = 0;
     537        3712 :   while (fCount == 0) {
     538        1648 :     if (!ReadHeader()) return kFALSE;
     539             :   }
     540        1032 :   data = fPosition;
     541        1032 :   fPosition += fCount;  
     542        1032 :   fCount = 0;
     543        1032 :   return kTRUE;
     544        1348 : }
     545             : 
     546             : Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
     547             : {
     548             : // reads the next block of data at the current position
     549             : // returns kFALSE if the data could not be read
     550             : 
     551      148102 :   fErrorCode = 0;
     552       74051 :   if (fPosition + size > fEnd) {
     553           0 :     Error("ReadNext", "could not read data!");
     554           0 :     fErrorCode = kErrOutOfBounds;
     555           0 :     return kFALSE;
     556             :   }
     557       74051 :   memcpy(data, fPosition, size);
     558             : 
     559       74051 :   fPosition += size;
     560       74051 :   fCount -= size;
     561       74051 :   return kTRUE;
     562       74051 : }
     563             : 
     564             : 
     565             : Bool_t AliRawReaderRoot::Reset()
     566             : {
     567             : // reset the current position to the beginning of the event
     568             : 
     569        3284 :   fSubEventIndex = 0;
     570        1642 :   fSubEvent = NULL;
     571        1642 :   fEquipmentIndex = 0;
     572        1642 :   fEquipment = NULL;
     573        1642 :   fRawData = NULL;
     574        1642 :   fHeader = NULL;
     575        1642 :   fHeaderV3 = NULL;
     576             : 
     577        1642 :   fCount = 0;
     578        1642 :   fPosition = fEnd = NULL;
     579        1642 :   return kTRUE;
     580             : }
     581             : 
     582             : 
     583             : Bool_t AliRawReaderRoot::NextEvent()
     584             : {
     585             : // go to the next event in the root file
     586             : 
     587          12 :   if (!fBranch) return kFALSE;
     588             : 
     589             :   // check if it uses order or not
     590           6 :   if (fgUseOrder && !fIndex) MakeIndex();
     591             : 
     592             :   do {
     593          12 :     delete fEvent;
     594           6 :     fEvent = NULL;
     595           6 :     fEventHeader = NULL;
     596           6 :     fBranch->SetAddress(&fEvent);
     597           6 :     Int_t entryToGet = fEventIndex + 1;
     598           6 :     if (fgUseOrder 
     599           6 :         && fIndex 
     600           0 :         && entryToGet<fBranch->GetEntries()
     601           0 :         && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
     602           6 :     if (fBranch->GetEntry(entryToGet) <= 0)
     603           1 :       return kFALSE;
     604           5 :     fEventHeader = fEvent->GetHeader();
     605           5 :     fEventIndex++;
     606          10 :   } while (!IsEventSelected());
     607           5 :   fEventNumber++;
     608           5 :   return Reset();
     609           6 : }
     610             : 
     611             : Bool_t AliRawReaderRoot::RewindEvents()
     612             : {
     613             : // go back to the beginning of the root file
     614             : 
     615           2 :   if (!fBranch) return kFALSE;
     616             : 
     617           1 :   fEventIndex = -1;
     618           2 :   delete fEvent;
     619           1 :   fEvent = NULL;
     620           1 :   fEventHeader = NULL;
     621           1 :   fBranch->SetAddress(&fEvent);
     622           1 :   fEventNumber = -1;
     623           1 :   return Reset();
     624           1 : }
     625             : 
     626             : Bool_t  AliRawReaderRoot::GotoEvent(Int_t event)
     627             : {
     628             :   // go to a particular event
     629             :   // Uses the absolute event index inside the
     630             :   // raw-data file
     631             : 
     632           0 :   if (!fBranch) return kFALSE;
     633             : 
     634             :   // check if it uses order or not
     635           0 :   if (fgUseOrder && !fIndex) MakeIndex();
     636             : 
     637           0 :   delete fEvent;
     638           0 :   fEvent = NULL;
     639           0 :   fEventHeader = NULL;
     640           0 :   fBranch->SetAddress(&fEvent);
     641             :   Int_t entryToGet = event;
     642           0 :   if (fgUseOrder 
     643           0 :       && fIndex 
     644           0 :       && entryToGet<fBranch->GetEntries()
     645           0 :       && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
     646           0 :   if (fBranch->GetEntry(entryToGet) <= 0)
     647           0 :     return kFALSE;
     648           0 :   fEventHeader = fEvent->GetHeader();
     649           0 :   fEventIndex = event;
     650           0 :   fEventNumber++;
     651           0 :   return Reset();
     652           0 : }
     653             : 
     654             : Int_t AliRawReaderRoot::GetNumberOfEvents() const
     655             : {
     656             :   // Get the total number of events in
     657             :   // the raw-data tree
     658             : 
     659           0 :   if (!fBranch) return -1;
     660             : 
     661           0 :   return fBranch->GetEntries();
     662           0 : }
     663             : 
     664             : Int_t AliRawReaderRoot::CheckData() const
     665             : {
     666             : // check the consistency of the data
     667             : 
     668           0 :   if (!fEvent) return 0;
     669             : 
     670             :   AliRawVEvent* subEvent = NULL;
     671             :   Int_t subEventIndex = 0;
     672             :   AliRawVEquipment* equipment = NULL;
     673             :   Int_t equipmentIndex = 0;
     674             :   UChar_t* position = 0;
     675             :   UChar_t* end = 0;
     676             :   Int_t result = 0;
     677             : 
     678           0 :   while (kTRUE) {
     679             :     // get the first or the next sub event if at the end of an equipment
     680           0 :     if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
     681             : 
     682             :       // check for end of event data
     683           0 :       if (subEventIndex >= fEvent->GetNSubEvents()) return result;
     684           0 :       subEvent = fEvent->GetSubEvent(subEventIndex++);
     685             : 
     686             :       // check the magic word of the sub event
     687           0 :       if (fSubEvent && !fSubEvent->GetHeader()->IsValid()) {
     688           0 :         result |= kErrMagic;
     689           0 :         return result;
     690             :       }
     691             : 
     692             :       equipmentIndex = 0;
     693           0 :     }
     694             : 
     695             :     // get the next equipment and raw data
     696           0 :     if (equipmentIndex >= subEvent->GetNEquipments()) {
     697             :       equipment = NULL;
     698           0 :       continue;
     699             :     }
     700           0 :     equipment = subEvent->GetEquipment(equipmentIndex++);
     701           0 :     if (!equipment) continue;
     702           0 :     AliRawData* rawData = equipment->GetRawData();
     703           0 :     if (!rawData) continue;
     704           0 :     position = (UChar_t*) rawData->GetBuffer();
     705           0 :     end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
     706             : 
     707             :     // continue with the next sub event if no data left in the payload
     708           0 :     if (position >= end) continue;
     709             : 
     710           0 :     if (fRequireHeader) {
     711             :     // check that there are enough bytes left for the data header
     712           0 :       if (position + sizeof(AliRawDataHeader) > end) {
     713           0 :         result |= kErrNoDataHeader;
     714           0 :         continue;
     715             :       }
     716             : 
     717             :       // Here we have to check if we have header v2 or v3
     718             :       // check consistency of data size in the header and in the equipment
     719           0 :       AliRawDataHeader* header = (AliRawDataHeader*) position;
     720           0 :       UChar_t version = header->GetVersion();
     721           0 :       if (version==2) {
     722           0 :         if ((position + header->fSize) != end) {
     723           0 :           if (header->fSize != 0xFFFFFFFF)
     724           0 :             Warning("CheckData",
     725             :                     "Equipment %d : raw data size found in the header V2 is wrong (%d != %ld)! Using the equipment size instead !",
     726           0 :                     equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
     727           0 :           header->fSize = end - position;
     728           0 :           result |= kErrSize;
     729           0 :         }
     730             :       }
     731           0 :       else if (version==3) {
     732             : 
     733           0 :         AliRawDataHeaderV3 * headerV3 =  (AliRawDataHeaderV3*) position;
     734           0 :         if ((position + headerV3->fSize) != end) {
     735           0 :           if (headerV3->fSize != 0xFFFFFFFF)
     736           0 :             Warning("CheckData",
     737             :                     "Equipment %d : raw data size found in the header V3 is wrong (%d != %ld)! Using the equipment size instead !",
     738           0 :                     equipment->GetEquipmentHeader()->GetId(),headerV3->fSize, end - position);
     739           0 :           headerV3->fSize = end - position;
     740           0 :           result |= kErrSize;
     741           0 :         }
     742           0 :       }
     743             :       
     744           0 :     }
     745             :     position = end;
     746           0 :   };
     747             : 
     748             :   return result;
     749           0 : }
     750             : 
     751             : AliRawReader* AliRawReaderRoot::CloneSingleEvent() const
     752             : {
     753             :   // Clones the current event and
     754             :   // creates raw-reader for the cloned event
     755             :   // Can be used in order to make asynchronious
     756             :   // access to the current raw data within
     757             :   // several threads (online event display/reco)
     758             : 
     759           0 :   if (fEvent) {
     760             :     // Root formatted raw data
     761           0 :     AliRawVEvent *gdcRootEvent = (AliRawVEvent*)fEvent->Clone();
     762           0 :     for (Int_t ldcCounter=0; ldcCounter < gdcRootEvent->GetNSubEvents(); ldcCounter++) {
     763           0 :       AliRawVEvent *ldcRootEvent = gdcRootEvent->GetSubEvent(ldcCounter);
     764           0 :       AliRawVEvent *subEvent = fEvent->GetSubEvent(ldcCounter);
     765           0 :       for (Int_t eqCounter=0; eqCounter < ldcRootEvent->GetNEquipments(); eqCounter++) {
     766           0 :         AliRawVEquipment *equipment=ldcRootEvent->GetEquipment(eqCounter);
     767           0 :         AliRawVEquipment *eq = subEvent->GetEquipment(eqCounter);
     768           0 :         equipment->CloneRawData(eq->GetRawData());
     769             :       }
     770             :     }
     771             :     // Reset original event and newly
     772             :     // produced one
     773           0 :     gdcRootEvent->GetSubEvent(-1);
     774           0 :     fEvent->GetSubEvent(-1);
     775           0 :     return new AliRawReaderRoot(gdcRootEvent);
     776             :   }
     777           0 :   return NULL;
     778           0 : }
     779             : 
     780             : void AliRawReaderRoot::MakeIndex() {
     781             :   // Make index
     782           0 :   if (fBranch) {
     783           0 :     TTree * rawTree = fBranch->GetTree();
     784           0 :     if (rawTree) {
     785           0 :       rawTree->BuildIndex("-fEvtHdrs[0].fSize"); // Minus sign to get largest first
     786           0 :       TTreeIndex * treeInd = (TTreeIndex*)rawTree->GetTreeIndex();
     787           0 :       if (treeInd) fIndex = treeInd->GetIndex();
     788           0 :     }
     789           0 :   }
     790           0 : }

Generated by: LCOV version 1.11