(Marian)
[u/mrichter/AliRoot.git] / TPC / AliTPCDDLRawData.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2003, 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 /* $Id$ */
16
17
18 //This class conteins all the methods to create raw data 
19 //as par a given DDL.
20 //It produces DDL with both compressed and uncompressed format.
21 //For compression we use the optimized table wich needs 
22 //to be provided.
23
24 #include <TObjArray.h>
25 #include <TString.h>
26 #include <TSystem.h>
27 #include <Riostream.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include "AliAltroBuffer.h"
31 #include "AliTPCAltroMapping.h"
32 #include "AliTPCDDLRawData.h"
33 #include "AliRawDataHeader.h"
34 #include "AliDAQ.h"
35
36 ClassImp(AliTPCDDLRawData)
37 ////////////////////////////////////////////////////////////////////////////////////////
38
39 AliTPCDDLRawData::AliTPCDDLRawData(const AliTPCDDLRawData &source):
40   TObject(source)
41 {
42   // Copy Constructor
43   fVerbose=source.fVerbose;
44   return;
45 }
46
47 AliTPCDDLRawData& AliTPCDDLRawData::operator=(const AliTPCDDLRawData &source){
48   //Assigment operator
49   fVerbose=source.fVerbose;
50   return *this;
51 }
52
53
54 ////////////////////////////////////////////////////////////////////////////
55 void AliTPCDDLRawData::RawData(const char* inputFileName){
56   //Raw data generation
57   //Number of DDL=2*36+4*36=216
58   //2 DDL for each inner sector
59   //4 DDL for each outer sector
60   ifstream f;
61 #ifndef __DECCXX
62   f.open(inputFileName,ios::binary);
63 #else
64   f.open(inputFileName);
65 #endif
66   if(!f){Error("RawData", "File doesn't exist !!");return;}
67   struct DataPad{
68     Int_t Sec;
69     Int_t SubSec;
70     Int_t Row;
71     Int_t Pad;
72     Int_t Dig;
73     Int_t Time;
74   };
75   DataPad data;
76
77   //AliAltroBuffer is used in write mode to generate raw data file
78   char  filename[15];
79   Int_t ddlNumber=0;
80   AliAltroBuffer *buffer=NULL;
81   Int_t pSecNumber=-1;  //Previous Sector number
82   Int_t pRowNumber=-1;  //Previous Row number  
83   Int_t pPadNumber=-1;  //Previous Pad number
84   Int_t pTimeBin=-1;    //Previous Time-Bin
85   Int_t pSubSector=-1;  //Previous Sub Sector
86   Int_t bunchLength=0;
87   Int_t nwords=0;
88   UInt_t numPackets=0;
89
90   TString path = gSystem->Getenv("ALICE_ROOT");
91   path += "/TPC/mapping/Patch";
92   TString path2;
93   AliTPCAltroMapping *mapping[6];
94   for(Int_t i = 0; i < 6; i++) {
95     path2 = path;
96     path2 += i;
97     path2 += ".data";
98     mapping[i] = new AliTPCAltroMapping(path2.Data());
99   }
100
101
102   while (f.read((char*)(&data),sizeof(data))){
103     if (pPadNumber==-1){
104       pSecNumber=data.Sec;
105       pRowNumber=data.Row;
106       pPadNumber=data.Pad;
107       pTimeBin=data.Time;
108       pSubSector=data.SubSec;
109
110       if(data.Sec<36)
111         ddlNumber=data.Sec*2+data.SubSec;
112       else
113         ddlNumber=72+(data.Sec-36)*4+data.SubSec;
114       strcpy(filename,AliDAQ::DdlFileName("TPC",ddlNumber));
115       Int_t patchIndex = data.SubSec;
116       if(data.Sec>=36) patchIndex += 2;
117       buffer=new AliAltroBuffer(filename,mapping[patchIndex]);
118       //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
119       buffer->WriteDataHeader(kTRUE,kFALSE);//Dummy;
120       bunchLength=1;
121       buffer->FillBuffer(data.Dig);
122       nwords++;
123     }//end if
124     else{
125       if ( (data.Time==(pTimeBin+1)) &&
126            (pPadNumber==data.Pad) &&
127            (pRowNumber==data.Row) &&
128            (pSecNumber==data.Sec)){
129         bunchLength++;
130       }//end if
131       else{
132         buffer->FillBuffer(pTimeBin);
133         buffer->FillBuffer(bunchLength+2);
134         nwords+=2;
135         if ((pPadNumber!=data.Pad)||(pRowNumber!=data.Row)||(pSecNumber!=data.Sec)){
136           //Trailer is formatted and inserted!!
137           buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
138           numPackets++;
139           nwords=0;
140
141           if(pSubSector!=data.SubSec){
142             //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
143             buffer->Flush();
144             buffer->WriteDataHeader(kFALSE,kFALSE);
145             //cout<<"Data header for DDL:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
146             delete buffer;
147
148             if(data.Sec<36)
149               ddlNumber=data.Sec*2+data.SubSec;
150             else
151               ddlNumber=72+(data.Sec-36)*4+data.SubSec;
152             strcpy(filename,AliDAQ::DdlFileName("TPC",ddlNumber));
153             Int_t patchIndex = data.SubSec;
154             if(data.Sec>=36) patchIndex += 2;
155             buffer=new AliAltroBuffer(filename,mapping[patchIndex]);
156             buffer->WriteDataHeader(kTRUE,kFALSE);//Dummy;
157             pSubSector=data.SubSec;
158           }//end if
159         }//end if
160         
161         bunchLength=1;
162         pPadNumber=data.Pad;
163         pRowNumber=data.Row;
164         pSecNumber=data.Sec;
165       }//end else
166       pTimeBin=data.Time;
167       buffer->FillBuffer(data.Dig);
168       nwords++;
169     }//end else
170   }//end while
171   if (buffer) {
172     buffer->FillBuffer(pTimeBin);
173     buffer->FillBuffer(bunchLength+2);
174     nwords+=2;
175     buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
176     //write the  D.H.
177     buffer->Flush();
178     buffer->WriteDataHeader(kFALSE,kFALSE);
179     //cout<<"Data header for D D L:"<<pSecNumber<<" Sub-sec:"<<pSubSector<<endl;
180     delete buffer;
181   }
182
183   for(Int_t i = 0; i < 6; i++) delete mapping[i];
184
185   f.close();
186   return;
187 }