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