]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSspdTestBeam.cxx
Coding Conventions & Cleanup
[u/mrichter/AliRoot.git] / ITS / AliITSspdTestBeam.cxx
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 /*
17   $Id$
18 */
19
20 // A set of classes/routines that can read the SPD test beam data of 2002
21 // and create AliITSdigits. The posibility to use these routines to do the
22 // same for later and other detectors has yet to be demonstrated. At present
23 // there remains a bug in that the TreeE of event headders isn't created
24 // properly. See the macro AliITSspdTestBeam2Digits.C. The geometry from
25 // the class AliITSvSPD002 must be read in, one way or the other, so that
26 // the Geometry transoformation class AliITSgeom will prpoerly be inilized.
27
28
29 //#include <Riostream.h>
30 #include <TArrayI.h>
31
32 #include "AliITSspdTestBeam.h"
33
34 ClassImp(AliITSspdTestBeam)
35
36 //----------------------------------------------------------------------
37 AliITSspdTestBeam::AliITSspdTestBeam(){
38     // Default Constructor for the Task AliITSspdTestBeam.
39     // Inputs:
40     //    none.
41     // Outputs:
42     //    none.
43     // Return:
44     //     A default constructed AliITSspdTestBeam class
45
46     fRH           = 0;
47     fRT           = 0;
48     fNBrst        = 0;
49     fBrstSize     = 0;
50     fBrst         = 0;
51     fNData        = 0;
52     fData         = 0;
53     fHData        = 0;
54     fTData        = 0;
55     SetTerminationWord();
56     fNEvents      = 0;
57     fBuffSize     = 0;
58     fBuff         = 0;
59     fITS          = 0;
60     fNfiles       = 0;
61     fMaxFiles     = 0;
62     fFiles        = 0;
63     fNeventsStart = 0;
64     fNeventsEnd   = 0;
65 }
66 //----------------------------------------------------------------------
67 AliITSspdTestBeam::AliITSspdTestBeam(const Char_t *filename,const Char_t *opt,
68                                      AliITS *its){
69     // Standard Constructor for the Task AliITSspdTestBeam.
70     // Inputs:
71     //    const Char_t *filename   File where to read in the SPD test beam data
72     //    const Char_t *opt        Option, 2002 => 2002 test beam data.
73     // Outputs:
74     //    none.
75     // Return:
76     //     A default constructed AliITSspdTestBeam class
77
78     fRH           = 0;
79     fRT           = 0;
80     fNBrst        = 0;
81     fBrstSize     = 0;
82     fBrst         = 0;
83     fNData        = 0;
84     fData         = 0;
85     fHData        = 0;
86     fTData        = 0;
87     SetTerminationWord();
88     fNEvents      = 0;
89     fBuffSize     = 0;
90     fBuff         = 0;
91     fITS          = 0;
92     fNfiles       = 0;
93     fMaxFiles     = 0;
94     fFiles        = 0;
95     fNeventsStart = 0;
96     fNeventsEnd   = 0;
97     //
98     fITS          = its;
99     fNfiles       = 0;
100     OpenInputFile(filename,0,-1);
101     if(strcmp(opt,"2002")) cout << "2002 assumed" << endl;
102 }
103
104 //----------------------------------------------------------------------
105 AliITSspdTestBeam::~AliITSspdTestBeam(){
106     // Destructor. Frees up any memory allocated or closes any files opened.
107     // Inputs:
108     //    none.
109     // Outputs:
110     //    none.
111     // Return:
112     //     nothing.
113     Int_t i,np;
114
115     np = GetNumberOfPilots();
116     fRH = 0;  // Just a Pointer into fBuff.
117     fRT = 0;  // Just a Pointer into fBuff.
118     if(fBrst){delete[] fBrst; fBrst = 0;}
119     if(fNData)for(i=0;i<np;i++){
120         if(fNData[i]) delete[] fNData[i];
121     } // end if
122     if(fNData) {delete[] fNData; fNData = 0;}
123     if(fData)for(i=0;i<np;i++){
124         if(fData[i]) delete[] fData[i];
125     } // end if
126     if(fData) delete[] fData;
127     fData = 0;
128     if(fHData)for(i=0;i<np;i++){
129         if(fHData[i]) delete[] fHData[i];
130     } // end if
131     if(fHData) delete[] fHData;
132     fHData = 0;
133     if(fTData)for(i=0;i<np;i++){
134         if(fTData[i]) delete[] fTData[i];
135     } // end if
136     if(fTData) delete[] fTData;
137     fTData = 0;
138     for(i=0;i<fMaxFiles;i++){
139         if(fFiles[i]!=0) delete fFiles[i];
140     } // end for i
141     if(fBrstSize)for(i=0;i<np;i++){
142         if(fBrstSize[i]) delete[] fBrstSize[i];
143     } // end if
144     if(fBrstSize) {delete[] fBrstSize; fBrstSize = 0;}
145     delete[] fBuff;
146     fITS = 0; //delete fITS;
147     delete[] fFiles;
148     delete[] fNeventsStart;
149     delete[] fNeventsEnd;
150 }
151 //----------------------------------------------------------------------
152 Int_t AliITSspdTestBeam::OpenInputFile(const Char_t *filename,Int_t start,Int_t end){
153     // Opens input file for reading spd test beam data.
154     // Inputs:
155     //    const Char_t *filename    file name to read data from
156     // Outputs:
157     //    none.
158     // Return:
159     //    An error number. 0=success, -1=failure.
160     Int_t stat = 0,i;
161
162     if(fMaxFiles==0) {
163         fMaxFiles     = 5;
164         fFiles        = new ifstream*[fMaxFiles];
165         fNeventsStart = new Int_t[fMaxFiles];
166         fNeventsEnd   = new Int_t[fMaxFiles];
167     } // end if
168     if(fNfiles==fMaxFiles){// Need to expand array of open files.
169         ifstream **tmp = new ifstream*[fMaxFiles];
170         TArrayI st(fMaxFiles);
171         TArrayI  en(fMaxFiles);
172         for(i=0;i<fMaxFiles;i++) { // copy pointers into tmp
173             tmp[i]    = fFiles[i];
174             fFiles[i] = 0;
175             st[i]     = fNeventsStart[i];
176             en[i]     = fNeventsEnd[i];
177         } // end for i
178         delete fFiles;
179         fMaxFiles    += 5;  // expand by 5.
180         fFiles        = new ifstream*[fMaxFiles];
181         fNeventsStart = new Int_t[fMaxFiles];
182         fNeventsEnd   = new Int_t[fMaxFiles];
183         for(i=0;i<fMaxFiles;i++) { // copy pointers back into fFiles
184             fFiles[i]        = 0;  // and zero rest.
185             fNeventsStart[i] = 0;
186             fNeventsEnd[i]   = 0;
187             if(i<fNfiles) {
188                 fFiles[i]        = tmp[i];
189                 tmp[i]           = 0;
190                 fNeventsStart[i] = st[i];
191                 fNeventsEnd[i]   = en[i];
192             } // end if i<fNfiles
193         } // end for i
194         // the array of pointers tmp is deleted automatically.
195         delete [] tmp;
196     } // end if
197     // Open file
198 #ifndef __DECCXX
199     fFiles[fNfiles] = new ifstream(filename,ios::in|ios::binary);
200 #else
201     fFiles[fNfiles] = new ifstream(filename,ios::in);
202 #endif
203     if(fFiles[fNfiles]==0){// file open error
204         cout << "Error opening input file " << filename << endl;
205         stat = -1;
206         return stat;
207     } // end if
208     fNeventsStart[fNfiles] = start;
209     fNeventsEnd[fNfiles]   = end;
210     fNfiles++;
211     return stat;
212 }
213 //----------------------------------------------------------------------
214 Int_t AliITSspdTestBeam::Read(Int_t i){
215     // Read in one buffer's worth of the file.
216     // Inputs:
217     //    Int_t i  Which file from the array of files to be read in.
218     // Outputs:
219     //    none.
220     // Return:
221     //    size of file.
222     Int_t filesize=0;
223
224     fFiles[i]->seekg(0,ios::end);
225     filesize  = fFiles[i]->tellg();
226     fFiles[i]->seekg(0,ios::beg);
227     if(fBuff) delete[] fBuff;
228     fBuffSize = filesize;
229     fBuff     = new UChar_t[fBuffSize];
230     fFiles[i]->read((Char_t*)fBuff,fBuffSize);
231     fFiles[i] ->close();
232     return filesize;
233 }
234 //----------------------------------------------------------------------
235 Int_t AliITSspdTestBeam::Decode(){
236     // Decode the fBuff read in.
237     // Inputs:
238     //    none.
239     // Outputs:
240     //    none.
241     // Return:
242     //    sets up the array fData fRH, and fRT
243     Int_t stat=0;
244     UInt_t *tr;
245     union {
246         UInt_t  *wd;
247         UChar_t *bt;
248     }u; // end union
249     Int_t size;
250     Int_t *ivnt,iburst,ip,np,i,j;
251     AliITSspdTestBeamData  *d;
252     AliITSspdTestBeamBurst *b;
253
254     np = GetNumberOfPilots();
255     ivnt = new Int_t[np];
256     for(i=0;i<np;i++) ivnt[i] = 0;
257     fRH  = (AliITSspdTestBeamHeader*) &(fBuff[0]); // Sets up the Run Header.
258     fRT  = (AliITSspdTestBeamTail*)&(fBuff[fBuffSize-fRT->SizeOf()]);
259     // Check termination
260     tr   = (UInt_t*) &(fBuff[fBuffSize-(fRT->SizeOf())-sizeof(UInt_t)]);
261     if(!(*tr==fTermination)){
262         cout << "Error Termination word not found at "<<tr<<endl;
263         exit(-1);
264     } // end if
265     if(fNData)for(i=0;i<np;i++){
266         if(fNData[i]) delete[] fNData[i];
267     } // end if
268     if(fNData) {delete[] fNData; fNData = 0;}
269     if(fData)for(i=0;i<np;i++){
270         if(fData[i]) delete[] fData[i];
271     } // end if
272     if(fData) {delete[] fData; fData = 0;}
273     if(fHData)for(i=0;i<np;i++){
274         if(fHData[i]) delete[] fHData[i];
275     } // end if
276     if(fHData) {delete[] fHData; fHData = 0;}
277     if(fTData)for(i=0;i<np;i++){
278         if(fTData[i]) delete[] fTData[i];
279     } // end if
280     if(fTData) {delete[] fTData; fTData = 0;}
281     if(fBrstSize)for(i=0;i<np;i++){
282         if(fBrstSize[i]) delete[] fBrstSize[i];
283     } // end if
284     if(fBrstSize) {delete[] fBrstSize; fBrstSize = 0;}
285     fNEvents  = fRH->GetNumberOfEvents();
286     fNBrst    = fNEvents/fRH->GetBurstSize();
287     fBrst     = new AliITSspdTestBeamBurst*[fNBrst];
288     fBrstSize = new Int_t*[np];
289     fNData    = new Int_t*[np];
290     fData     = new AliITSspdTestBeamData**[np];
291     fHData    = new AliITSspdTestBeamData**[np];
292     fTData    = new AliITSspdTestBeamData**[np];
293     for(i=0;i<np;i++){
294         fBrstSize[i] = new Int_t[fNBrst];
295         fNData[i]    = new Int_t[fNEvents];
296         fData[i]     = new AliITSspdTestBeamData*[fNEvents];
297         fHData[i]    = new AliITSspdTestBeamData*[fNEvents];
298         fTData[i]    = new AliITSspdTestBeamData*[fNEvents];
299         for(j=0;j<fNEvents;j++){
300             fNData[i][j] = 0;
301             fData[i][j]  = 0;
302             fHData[i][j] = 0;
303             fTData[i][j] = 0;
304         } // end for j
305     } // end for i
306     if(fBrst){delete[] fBrst; fBrst = 0;}
307     size      = fRH->SizeOf();
308     u.bt      = &fBuff[size];
309     //
310     for(iburst=0;(*(u.wd) != fTermination)&&(u.wd<tr);iburst++){ // loop over Bursts
311         b   = (AliITSspdTestBeamBurst *) u.wd;
312         fBrst[iburst] = b;
313         u.bt += b->SizeOf(); // increment wd byte wise
314         for(ip=0;ip<np;ip++){  // loop over pilots
315             // Get size of data stored for this pilot.
316             fBrstSize[ip][iburst] = (UInt_t) u.wd;
317             u.bt += sizeof(UInt_t); // increment wd byte wise
318             for(i=0;i<fBrstSize[ip][iburst];i++){ // loop over data
319                 d = (AliITSspdTestBeamData *) u.wd;
320                 switch (d->Mode()){
321                 case AliITSTestBeamData::kData :
322                     fNData[ip][ivnt[ip]]++;
323                     // set pointer to first data member
324                     if(fData[ip][ivnt[ip]] == 0 ) fData[ip][ivnt[ip]] = d;
325                     break;
326                 case AliITSTestBeamData::kHead :
327                     fNData[ip][ivnt[ip]]   = 0;
328                     fData[ip][ivnt[ip]]    = 0;
329                     fHData[ip][ivnt[ip]++] = d;
330                     break;
331                 case AliITSTestBeamData::kTail  : 
332                 case AliITSTestBeamData::kAbort :
333                     fTData[ip][ivnt[ip]] = d;
334                     break;
335                 default:
336                     cout << "Unknown Data Type: wd="<<hex<<*(u.wd)<<dec<<endl; 
337                     break;
338                 } // end switch
339                 u.bt += d->SizeOf(); // increment wd byte wise
340             } // end for i (next data word).
341         } // end for loop over pilots (ip).
342     } // end for loop over bursts
343     delete[] ivnt;
344     return stat;
345 }
346 //============================================================================
347 void AliITSspdTestBeamHeader::Print(ostream *os)const{
348     // print out the header information
349     // Inputs:
350     //    ostream *os  Pointer to the output stream.
351     // Outputs:
352     //    none.
353     // Return:
354     //    none.
355 /*
356 #if defined __GNUC__
357 #if __GNUC__ > 2
358     ios::fmtflags fmt;
359 #else
360     Int_t fmt;
361 #endif
362 #else
363 #if defined __ICC || defined __ECC
364     ios::fmtflags fmt;
365 #else
366     Int_t fmt;
367 #endif
368 #endif
369 */
370     *os<<"Version: "<<fUnion.fHead.fVersion<<" Written: "<<fUnion.fHead.fDate;
371     *os<<" " << fUnion.fHead.fTime << endl;
372     *os<<"Buffer Size [0], [1], [2]: " << fUnion.fHead.fBuffSize[0] << ",";
373     *os<<fUnion.fHead.fBuffSize[1]<<"," <<fUnion.fHead.fBuffSize[2] << endl;
374     *os<<"Test Pulse: " << fUnion.fHead.fTestPulse << endl;
375     *os<<"Trigger Mode General, [0], [1], [2]: " << fUnion.fHead.fTriggerMode;
376     *os<<","<<fUnion.fHead.fTrigger[0]<<","<<fUnion.fHead.fTrigger[1]<< ",";
377     *os<<fUnion.fHead.fTrigger[2] << endl;
378     *os<<"Number of Events: " << fUnion.fHead.fNEvents << " Burst Size: ";
379     *os<<fUnion.fHead.fBurstSize << endl;
380     return;
381 }
382 //----------------------------------------------------------------------
383 ostream &operator<<(ostream &os,AliITSspdTestBeamHeader &p){
384     // Standard output operator. See Print
385     // Inputs:
386     //    ostream                 &os  the output stream.
387     //    AliITSspdTestBeamHeader &p   the data to be printed out.
388     // Outputs:
389     //    none.
390     // Return:
391     //    ostream &os pointing now to the end of the present stream.
392
393     p.Print(&os);
394     return os;
395 }
396 //============================================================================
397 void AliITSspdTestBeamTail::Print(ostream *os)const{
398     // print out the Tail information
399     // Inputs:
400     //    ostream *os  Pointer to the output stream.
401     // Outputs:
402     //    none.
403     // Return:
404     //    none.
405 /*
406 #if defined __GNUC__
407 #if __GNUC__ > 2
408     ios::fmtflags fmt;
409 #else
410     Int_t fmt;
411 #endif
412 #else
413 #if defined __ICC || defined __ECC
414     ios::fmtflags fmt;
415 #else
416     Int_t fmt;
417 #endif
418 #endif
419 */
420     *os << "Number of Events: "<< fUnion.fTail.fEvents << " Written: "
421         << fUnion.fTail.fDate;
422     *os << " " << fUnion.fTail.fTime << endl;
423     *os <<"Termination Flag: " << fUnion.fTail.fTermMode << endl; 
424     return;
425 }
426 //----------------------------------------------------------------------
427 ostream &operator<<(ostream &os,AliITSspdTestBeamTail &p){
428     // Standard output operator. See Print
429     // Inputs:
430     //    ostream                 &os  the output stream.
431     //    AliITSspdTestBeamHeader &p   the data to be printed out.
432     // Outputs:
433     //    none.
434     // Return:
435     //    ostream &os pointing now to the end of the present stream.
436
437     p.Print(&os);
438     return os;
439 }
440 //============================================================================
441 void AliITSspdTestBeamBurst::Print(ostream *os)const{
442     // print out the Burst information
443     // Inputs:
444     //    ostream *os  Pointer to the output stream.
445     // Outputs:
446     //    none.
447     // Return:
448     //    none.
449 /*
450 #if defined __GNUC__
451 #if __GNUC__ > 2
452     ios::fmtflags fmt;
453 #else
454     Int_t fmt;
455 #endif
456 #else
457 #if defined __ICC || defined __ECC
458     ios::fmtflags fmt;
459 #else
460     Int_t fmt;
461 #endif
462 #endif
463 */
464     *os << "Burst Number: "<< fUnion.fBrst.fNumber << " Transfers: " 
465         << fUnion.fBrst.fTransfers << endl; 
466     return;
467 }
468 //----------------------------------------------------------------------
469 ostream &operator<<(ostream &os,AliITSspdTestBeamBurst &p){
470     // Standard output operator. See Print
471     // Inputs:
472     //    ostream                 &os  the output stream.
473     //    AliITSspdTestBeamHeader &p   the data to be printed out.
474     // Outputs:
475     //    none.
476     // Return:
477     //    ostream &os pointing now to the end of the present stream.
478
479     p.Print(&os);
480     return os;
481 }
482 //======================================================================
483 void AliITSspdTestBeamData::Print(ostream *os)const{
484     // print out the the Test Beam Data information
485     // Inputs:
486     //    ostream *os  Pointer to the output stream.
487     // Outputs:
488     //    none.
489     // Return:
490     //    none.
491 /*
492 #if defined __GNUC__
493 #if __GNUC__ > 2
494     ios::fmtflags fmt;
495 #else
496     Int_t fmt;
497 #endif
498 #else
499 #if defined __ICC || defined __ECC
500     ios::fmtflags fmt;
501 #else
502     Int_t fmt;
503 #endif
504 #endif
505 */
506     *os << "Word=" << hex << fUnion.fBuf[0] << hex << fUnion.fBuf[1] 
507                    << hex << fUnion.fBuf[2] << hex << fUnion.fBuf[3] << dec;
508     switch (this->Mode()){
509     case AliITSTestBeamData::kData :
510         *os << " kData chip=" << setw(3) << fUnion.fDataD.fChip; 
511         *os << " Row="        << setw(3) << fUnion.fDataD.fRow;
512         *os << " Column="     << setw(3) << fUnion.fDataD.fColm;
513         break;
514     case AliITSTestBeamData::kHead :
515         *os << " kHead Event Sync =" << fUnion.fDataH.fEventSync;
516         break;
517     case AliITSTestBeamData::kTail  :
518         *os << " kTail Transmitted word count =" << fUnion.fDataT.fTrans;
519         break;
520     case AliITSTestBeamData::kAbort :
521         *os << " kAbort Transmitted word count =" << fUnion.fDataA.fTrans;
522         break;
523     default:
524         *os << " Unknown Data Type"; 
525         break;
526     } // end switch
527     *os << endl;
528     return;
529 }
530 //----------------------------------------------------------------------
531 ostream &operator<<(ostream &os,AliITSspdTestBeamData &p){
532     // Standard output operator. See Print
533     // Inputs:
534     //    ostream                 &os  the output stream.
535     //    AliITSspdTestBeamHeader &p   the data to be printed out.
536     // Outputs:
537     //    none.
538     // Return:
539     //    ostream &os pointing now to the end of the present stream.
540
541     p.Print(&os);
542     return os;
543 }