]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFDDLRawData.cxx
TOF raw data: preliminary implementation and style changes
[u/mrichter/AliRoot.git] / TOF / AliTOFDDLRawData.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
16 /*
17 Revision 0.01  2004/6/11 A.De Caro, S.B.Sellitto, R.Silvestri
18         First implementation: global methods RawDataTOF
19                                              GetDigits
20                                              (Un)PackWord
21 */
22 //
23 // This class contains the methods to create the Raw Data files
24 // for the TOF detector starting from the Digits.
25 // In this preliminary implementation, we defined the structure
26 // of the ALICE-TOF raw data starting from the current format
27 // for the TOF digits and the TOF raw data.
28 //
29
30 #include <stdlib.h>
31 #include <Riostream.h>
32 #include <TClonesArray.h>
33 #include <TTree.h>
34 #include <TMath.h>
35 #include "AliTOF.h"
36 #include "AliTOFGeometry.h"
37 #include "AliTOFdigit.h"
38 #include "AliTOFDDLRawData.h"
39 #include "AliRawDataHeader.h"
40
41 ClassImp(AliTOFDDLRawData)
42
43 //----------------------------------------------------------------------------------------
44 AliTOFDDLRawData::AliTOFDDLRawData()
45 {
46   //Default constructor
47   fIndex=-1;
48   fVerbose=0;
49 }
50
51 //----------------------------------------------------------------------------------------
52
53 AliTOFDDLRawData::AliTOFDDLRawData(const AliTOFDDLRawData &source) : 
54     TObject(source){
55   //Copy Constructor
56   this->fIndex=source.fIndex;
57   this->fVerbose=source.fVerbose;
58   return;
59 }
60
61 //---------------------------------------------------------------------------------------
62
63 AliTOFDDLRawData& AliTOFDDLRawData::operator=(const AliTOFDDLRawData &source){
64   //Assigment operator
65   this->fIndex=source.fIndex;
66   this->fVerbose=source.fVerbose;
67   return *this;
68 }
69
70 //---------------------------------------------------------------------------------------
71
72 void AliTOFDDLRawData::GetDigits(TClonesArray *TOFdigits,Int_t nDDL,UInt_t *buf)
73 {
74   //This method packs the TOF digits in a proper 32 bits structure
75   Int_t iDDL=(Int_t)((nDDL/4.-(Int_t)(nDDL/4.))*4);
76   Int_t iSector=(Int_t)(nDDL/4.);
77   Int_t iTRM=0;
78   Int_t iTDC=0;
79   Int_t iCH=-1;
80   Int_t sector; 
81   Int_t plate;
82   Int_t strip;
83   Int_t padx;
84   Int_t padz;
85   Int_t totCharge;
86   Int_t timeOfFlight;
87   Int_t error=0;
88   Int_t eureka;
89   UInt_t word;
90   UInt_t baseWord;
91   Int_t ndigits = TOFdigits->GetEntries();
92   AliTOFdigit *digs;
93   ofstream ftxt;
94   if(!ndigits) 
95     {
96       Error("GetDigits", "No found TOF digits\n");      
97       return;
98     }
99
100   if (fVerbose==2) ftxt.open("TOFdigits.txt",ios::app);
101   for (Int_t digit=0;digit<ndigits;digit++) {
102     digs = (AliTOFdigit*)TOFdigits->UncheckedAt(digit);
103     sector=digs->GetSector(); // Sector Number (0-17)
104     plate=digs->GetPlate();   // Plate Number (0-4)
105     strip=digs->GetStrip();   // Strip Number (0-14/18/19)
106     padx=digs->GetPadx();     // Pad Number in x direction (0-47)
107     padz=digs->GetPadz();     // Pad Number in z direction (0-1)
108     eureka=digs->GetTotPad(); // Global Pad Number inside a Sector
109     totCharge = (Int_t)digs->GetAdc();
110     timeOfFlight = (Int_t)digs->GetTdc();
111     /*
112     Int_t istriPlate=0;
113     switch (plate)
114       {
115       case 0:
116         break;
117       case 1:
118         istriPlate = AliTOFGeometry::NStripC();
119         break;
120       case 2:
121         istriPlate = AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB();
122         break;
123       case 3:
124         istriPlate = AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB()+AliTOFGeometry::NStripA();
125         break;
126       case 4:
127         istriPlate = AliTOFGeometry::NStripC()+2*AliTOFGeometry::NStripB()+AliTOFGeometry::NStripA();
128         break;
129       }
130
131     eureka=2*padx+padz+AliTOFGeometry::NpadXStrip()*(strip+istriPlate);
132     
133     if (eureka!=digs->GetTotPad()) printf(" eureka = %d AND digs->GetTotPad() = %d",eureka,digs->GetTotPad());
134     */
135     if (sector!=iSector || (Int_t)((Float_t)eureka/AliTOF::NPadXTRM()/AliTOF::NTRM())!=iDDL) continue;
136     
137     if (fVerbose==2) ftxt <<" Sector: "<<sector<<" plate: "<<plate<<" strip "<<strip<<" padx "<<padx<<" padz "<<padz<<" eureka "<<eureka<<endl;
138     
139     iTRM = (Int_t)((Float_t)eureka/AliTOF::NPadXTRM() - AliTOF::NTRM()*iDDL);
140     
141     iTDC = (Int_t)(AliTOF::NTdc()* 
142                    (
143                     (Float_t)eureka/AliTOF::NPadXTRM() -
144                     (Int_t)((Float_t)eureka/AliTOF::NPadXTRM())
145                     )
146                    );
147     
148     iCH  = (Int_t)(AliTOF::NCh() * 
149                    (
150                     (Float_t)eureka/AliTOF::NPadXTRM()*AliTOF::NTdc() - (Int_t)((Float_t)eureka/AliTOF::NPadXTRM()*AliTOF::NTdc()) -
151                     (Int_t)((Float_t)eureka/AliTOF::NPadXTRM()*AliTOF::NTdc() - (Int_t)((Float_t)eureka/AliTOF::NPadXTRM()*AliTOF::NTdc()))
152                     )
153                    );
154     
155     if (fVerbose==2) ftxt << "DDL: "<<iDDL<<" Sector: "<<sector<<" TRM: "<<iTRM<<" TDC: "<<iTDC<<" Channel: "<<iCH<<" totCharge: "<<totCharge<<" tof: "<<timeOfFlight<<endl;
156     
157     baseWord=0;
158     
159     word=iTRM;
160     PackWord(baseWord,word, 0, 3); // TRM ID
161     word=iTDC;
162     PackWord(baseWord,word, 4, 8); // TDC ID
163     word=iCH;
164     PackWord(baseWord,word, 9,11); // CH ID
165
166     // temporary control
167     if (totCharge<0) word=TMath::Abs(totCharge);
168     else word=totCharge;
169     PackWord(baseWord,word,12,31); // Charge (TOT)
170     
171     fIndex++;
172     buf[fIndex]=baseWord;
173     
174     baseWord=0;
175     
176     word=error;
177     PackWord(baseWord,word,0, 7); // Error flag
178     word=timeOfFlight;
179     PackWord(baseWord,word,8,31); // time-of-flight
180     
181     fIndex++;
182     buf[fIndex]=baseWord;
183     word=0;
184     baseWord=0;
185     
186   }//end for
187   
188   if (fVerbose==2) ftxt.close();
189
190   return;
191
192 }//end GetDigits
193
194 //-------------------------------------------------------------------------------------
195
196 void AliTOFDDLRawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit)
197 {
198   //This method packs a word into the Baseword buffer starting form the "StartBit" 
199   //and tacking StopBit-StartBit+1 bits
200   UInt_t dummyWord,offSet;
201   Int_t  length;
202   UInt_t sum;
203   //The BaseWord is being filled with 1 from StartBit to StopBit
204   length=StopBit-StartBit+1;
205   sum=(UInt_t)TMath::Power(2,length)-1;
206
207   if(Word > sum){
208     Error("PackWord", "Word to be filled is not within desired length\n"
209           "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
210     return;
211   }
212   offSet=sum;
213   offSet<<=StartBit;
214   BaseWord=BaseWord|offSet;
215
216   //The Word to be filled is shifted to the position StartBit
217   //and the remaining  Left and Right bits are filled with 1
218   sum=(UInt_t)TMath::Power(2,StartBit)-1;
219   dummyWord=0xFFFFFFFF<<length;
220   dummyWord +=Word;
221   dummyWord<<=StartBit;
222   dummyWord+=sum;
223   BaseWord=BaseWord&dummyWord;
224
225   return;
226 }
227
228 //------------------------------------------------------------------------------------------------
229
230 void AliTOFDDLRawData::UnpackWord(UInt_t PackedWord, Int_t StartBit, Int_t StopBit, UInt_t &Word)
231 {
232   //This method unpacks a words of StopBit-StartBit+1 bits starting from "StopBits"  
233   UInt_t offSet;
234   Int_t length;
235   length=StopBit-StartBit+1;
236   offSet=(UInt_t)TMath::Power(2,length)-1;
237   offSet<<=StartBit;
238   Word=PackedWord&offSet;
239   Word>>=StartBit;
240   return;
241 }
242
243 //---------------------------------------------------------------------------------------
244
245 Int_t AliTOFDDLRawData::RawDataTOF(TBranch* branch){
246   //This method creates the Raw data files for TOF detector
247   const Int_t kSize=5000; //2*AliTOFGeometry::NpadXSector() 
248                           //max number of digits per DDL file times 2
249   UInt_t buf[kSize];
250   UInt_t baseWord;
251   UInt_t word;
252
253   fIndex=-1;
254
255   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
256   char fileName[15];
257   ofstream outfile;         // logical name of the output file 
258   AliRawDataHeader header;
259
260   //loop over TOF DDL files
261   for(Int_t i=0;i<72;i++){
262     sprintf(fileName,"TOF_%d.ddl",i); //The name of the output file
263 #ifndef __DECCXX
264     outfile.open(fileName,ios::binary);
265 #else
266     outfile.open(fileName);
267 #endif
268     //write Dummy DATA HEADER
269     UInt_t dataHeaderPosition=outfile.tellp();
270     outfile.write((char*)(&header),sizeof(header));
271
272     baseWord=0;
273     word=i;
274     PackWord(baseWord,word,0, 31); // Number of DDL file
275
276     fIndex++;
277     buf[fIndex]=baseWord;
278
279     branch->GetEvent();
280
281     //For each DDL file, buf contains the array of data words in Binary format
282     //fIndex gives the number of 32 bits words in the buffer for each DDL file
283     GetDigits(digits,i,buf);
284     outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
285
286     for(Int_t ii=0;ii<(fIndex+1);ii++) buf[ii]=0;
287     fIndex=-1;
288     
289     //Write REAL DATA HEADER
290     UInt_t currentFilePosition=outfile.tellp();
291     outfile.seekp(dataHeaderPosition);
292     header.fSize=currentFilePosition-dataHeaderPosition;
293     header.SetAttribute(0);  // valid data
294     outfile.write((char*)(&header),sizeof(header));
295     outfile.close();
296
297   }//end for
298   
299   return 0;  
300 }
301
302 //-----------------------------------------------------------------------------------------
303 /*
304 void AliTOFDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t EventCnt,UInt_t &BaseWord)
305 {
306   //This method writes a chip header 
307   BaseWord=0;
308   PackWord(BaseWord,ChipAddr,0,3);
309   PackWord(BaseWord,EventCnt,4,10);
310   PackWord(BaseWord,0x7,11,13);
311   PackWord(BaseWord,0x1,14,15);
312   return;
313 }//end WriteChipHeader
314 */
315 //----------------------------------------------------------------------------------------
316 /*
317 void AliTOFDDLRawData::ReadChipHeader(Int_t &ChipAddr,Int_t &EventCnt,UInt_t BaseWord)
318 {
319   //This method reads a chip header
320   UInt_t temp=0;
321   UnpackWord(BaseWord,0,3,temp);
322   ChipAddr=(Int_t)temp;
323   UnpackWord(BaseWord,4,10,temp);
324   EventCnt=(Int_t)temp;
325   if(fVerbose)
326     Info("ReadChipHeader", "Chip:&d Half Stave module:%d",ChipAddr,EventCnt);
327   return;
328 }//end ReadChipHeader
329 */
330 //----------------------------------------------------------------------------------------
331 /*
332 void  AliTOFDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord)
333 {
334   //This method writes a chip trailer
335   //pixel fill word
336   if((ChipHitCount%2)!=0){
337     PackWord(BaseWord,0xFEDC,0,15);
338   }
339   PackWord(BaseWord,ChipHitCount,16,28);
340   PackWord(BaseWord,0x0,30,31);
341   fIndex++;
342   buf[fIndex]=BaseWord;
343   BaseWord=0;
344   return;
345 }//end WriteChipTrailer
346 */
347 //------------------------------------------------------------------------------------------
348 /*
349 void  AliTOFDDLRawData::ReadChipTrailer(Int_t &ChipHitCount,UInt_t BaseWord)
350 {
351   //This method reads a chip trailer
352   UInt_t temp=0;
353   UnpackWord(BaseWord,16,28,temp);
354   ChipHitCount=(Int_t)temp;
355   return;
356 }//end ReadChipTrailer
357 */
358 //------------------------------------------------------------------------------------------