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