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