1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //_________________________________________________________________________
19 // Class designed to convert raw data to aliroot format. We assume, that
20 // prototype is situated in the center of 3 PHOS module and convert prototype
21 // outpur to AliPHOSDigits. In addition, we fill branch of TreeE with
22 // AliPHOSBeamTestEvent, contaning description of event(triggers etc).
23 // Note, that one byte per channel in raw data is transvormed to class
24 // AliPHOSDigit, so finale zise increase ~ 100 times. So output can be split
25 // into peases of reasonable size: each file can not contain more than
26 // fMaxPerFile: if there are still events in raw file, then new directory
27 // is created and header+digits files are written to it.
30 // AliPHOSRaw2Digits * r = new AliPHOSRaw2Digits("path/input.file") ;
31 // //note, that it can be gzipped file!
32 // //Set position of the target in the given run.
33 // //Z axis along beam direction, from target to prototype (0-surface of prototype)
34 // //X axis along columns of prototype (0-center of prototype)
35 // //Y axis along raws of prototype (0-center of prototype)
36 // Double_t pos[3]={0,0,-452.} ;
37 // r->SetTargetPosition(pos) ;
38 // //Read/create connection Table:
39 // TFile f("ConTableDB.root") ;
40 // AliPHOSConTableDB * cdb = f.Get("AliPHOSConTableDB") ;
42 // r->SetConTableDB(cdb) ;
45 // As a result files galice.root and PHOS.Digits.root should be produced in
46 // current dir, and, possibly, dirs 1,2,3... each with galice.root and PHOS.Digits.root,
47 // where the rest of data are written.
49 /*-- Author: Maxim Volkov (RRC KI)
50 Dmitri Peressounko (RRC KI & SUBATECH)
51 Yuri Kharlov (IHEP & SUBATECH) */
53 //////////////////////////////////////////////////////////////////////////////
55 // --- ROOT system ---
56 #include "TClonesArray.h"
62 // --- Standard library ---
66 // --- AliRoot header files ---
67 #include "AliPHOSDigit.h"
68 #include "AliPHOSConTableDB.h"
69 #include "AliPHOSBeamTestEvent.h"
70 #include "AliPHOSRaw2Digits.h"
73 ClassImp(AliPHOSRaw2Digits)
76 //____________________________________________________________________________
77 AliPHOSRaw2Digits::AliPHOSRaw2Digits():TTask()
79 //As one can easily see, this is constructor.
86 fDebug = kFALSE; // Debug flag
87 fIsInitialized = kFALSE ;
95 fMaxPerFile = 20000 ; //Maximal number of events in root file.
99 //____________________________________________________________________________
100 AliPHOSRaw2Digits::AliPHOSRaw2Digits(const char * filename):TTask("Default","")
102 //this constructor should be normally used. Parameters: imput file
104 TString outname("") ;
108 outname.ReplaceAll(".fz",".root") ;
109 outname.ReplaceAll(".gz","") ;
118 fDebug = kFALSE; // Debug flag
119 fIsInitialized = kFALSE ;
127 fMaxPerFile = 20000 ;
131 //____________________________________________________________________________
132 AliPHOSRaw2Digits::AliPHOSRaw2Digits(AliPHOSRaw2Digits & r2d):TTask(r2d.GetName(), r2d.GetTitle())
135 fInName=r2d.fInName ;
142 fDebug = kFALSE; // Debug flag
143 fIsInitialized = kFALSE ;
144 fTarget[0] = r2d.fTarget[0] ;
145 fTarget[1] = r2d.fTarget[1] ;
146 fTarget[2] = r2d.fTarget[2] ;
147 fDigits = r2d.fDigits ;
148 fPHOSHeader = r2d.fPHOSHeader ;
149 fHeaderFile = new TFile( (r2d.fHeaderFile)->GetName(), "new" ) ;
150 fDigitsFile = new TFile( (r2d.fDigitsFile)->GetName(), "new" ) ;
151 fMaxPerFile = r2d.fMaxPerFile ;
152 fEvent = r2d.fEvent ;
153 fctdb = new AliPHOSConTableDB( *(r2d.fctdb) ) ;
156 //____________________________________________________________________________
157 AliPHOSRaw2Digits::~AliPHOSRaw2Digits()
161 fPHOSHeader->Delete() ;
168 //____________________________________________________________________________
169 void AliPHOSRaw2Digits::Exec(){
170 //This is steering method performing all the conversion
172 if(!fIsInitialized) //need initialization
173 if(!Init()) //failed to initialize
179 //____________________________________________________________________________
180 Bool_t AliPHOSRaw2Digits::Init(void){
181 //Makes initialization of contaniers
186 //Make container for digits
187 fDigits = new TClonesArray("AliPHOSDigit",1000) ;
188 fPHOSHeader = new AliPHOSBeamTestEvent() ;
189 fIsInitialized = kTRUE ;
190 return StartRootFiles() ;
193 //____________________________________________________________________________
194 Bool_t AliPHOSRaw2Digits::StartRootFiles(void ) const {
195 // //Create PHOS geometry, sets magnetic field to zero,
196 // //create Generator - to store target position,
197 // //opens out file, creates TreeE
199 // //create gAlice if nececcary
201 // new AliRun("gAlice","The ALICE Off-line Simulation Framework") ;
204 // if(!gAlice->GetModule("PHOS"))
205 // new AliPHOSv1("PHOS","GPS2") ;
207 // //Set Magnetic field
208 // gAlice->SetField(0,2);
210 // //Set positin of the virtex
211 // AliGenerator * gener = gAlice->Generator() ;
213 // gener = new AliGenBox(1);
214 // Float_t ox = fTarget[1];
215 // Float_t oy = fTarget[2]+460.;
216 // Float_t oz = fTarget[0];
217 // gener->SetOrigin(ox, oy, oz);
220 // Int_t nRootFile = (fEvent+1)/fMaxPerFile ;
223 // sprintf(dname,"%d",nRootFile) ;
224 // if(gSystem->AccessPathName(dname)) //strange return: 0 if exists
225 // if(gSystem->MakeDirectory(dname)!=0)
226 // Fatal("StartRootFiles","Can not make directory %s \n",dname) ;
228 // if(!gSystem->ChangeDirectory(dname))
229 // Fatal("StartRootFiles","Can not cd to %s\n",dname) ;
232 // // Create the output file
233 // TString outname("") ;
234 // if(strstr(GetTitle(),"root")){
235 // outname=GetTitle();
238 // outname = fInName ;
239 // outname.ToLower() ;
240 // outname.ReplaceAll(".fz",".root") ;
243 // fHeaderFile = new TFile(outname,"recreate");
244 // fHeaderFile->SetCompressionLevel(2);
246 // // Create the Root Trees
248 // gime->MakeTree("E") ;
251 // Int_t splitlevel = 0 ;
252 // Int_t bufferSize = 32000 ;
253 // TBranch * headerBranch = gAlice->TreeE()->Branch("AliPHOSBeamTestEvent",
254 // "AliPHOSBeamTestEvent",
255 // &fPHOSHeader,bufferSize,splitlevel);
256 // headerBranch->SetName("AliPHOSBeamTestEvent") ;
259 // // fDigitsFile = new TFile("PHOS.Digits.root","recreate") ;
260 // // fDigitsFile->SetCompressionLevel(2) ;
264 //____________________________________________________________________________
265 Bool_t AliPHOSRaw2Digits::CloseRootFiles(void ){
266 //cleans everething to start next root file
268 printf("writing gAlice \n") ;
270 gAlice->Write(0,TObject::kOverwrite);
271 gAlice->TreeE()->Write(0,TObject::kOverwrite);
277 fHeaderFile->Close() ;
283 fDigitsFile->Close() ;
288 Int_t nRootFile = (fEvent-1)/fMaxPerFile ;
290 if(!gSystem->ChangeDirectory("../")){
291 Fatal("CloseRootFile","Can not return to initial dir \n") ;
297 //____________________________________________________________________________
298 Bool_t AliPHOSRaw2Digits::ProcessRawFile(){
300 //Method opens zebra file and reads successively bytes from it,
301 //filling corresponding fields in header and digits.
305 //First of all, open file and check if it is a zebra file
308 sprintf(command,"zcat %s",fInName.Data());
309 FILE *dataFile = popen(command, "r");
310 if (dataFile == NULL) {
311 Warning("ProcessRawFile", " Cannot open file %s\n", fInName.Data() ) ;
312 perror(fInName.Data()) ;
317 // Check if byte ordering is little-endian
318 UInt_t w = 0x12345678;
319 Int_t swapo = memcmp(&w, "\x78\x56\x34\x12", sizeof(UInt_t)) == 0;
321 Info("ProcessRawFile", "swapo=%f\n", swapo ) ;
326 // Read physical record control words
327 UInt_t nb = 8*sizeof(UInt_t);
328 Int_t n = fread(recBuf, nb, 1, dataFile);
329 if(static_cast<UInt_t>(n) != 1) {
331 perror(fInName.Data());
333 Error("ProcessRawFile", "Could not read physical record control words" ) ;
339 Info("ProcessRawFile", "recbuf[0] = %d\n", recBuf[0] );
341 // Check if it is a ZEBRA file and if the words are swapped
343 if (recBuf[0] != fMK1) {
344 Swab4(recBuf, &w, 1);
346 Error("ProcessRawFile", "Does not look like a ZEBRA file\n" ) ;
356 message = " w = %f\n" ;
357 message += " swapi = %f\n" ;
358 Info("ProcessRawFile", message.Data(), w, swapi ) ;
361 // Get number of words in physical record
364 Swab4(&recBuf[4],&nwphr,1);
367 nwphr*=2; // 1998 -- Now we have 2 records 150 words each
370 //start loop over data
371 // Read rest of record
372 nb = (nwphr-8)*sizeof(UInt_t);
373 n = fread(&recBuf[8], nb, 1, dataFile) ;
374 if (static_cast<UInt_t>(n) != 1) {
376 perror(fInName.Data());
381 nb = nwphr *sizeof(UInt_t);
383 UInt_t userVector[16] ;
386 UShort_t scanning[32] ;
389 UShort_t tdc2228[32];
391 //read untill the end of file
397 if((fEvent%fMaxPerFile == 0) && fEvent ){
401 gAlice->SetEvent(fEvent%fMaxPerFile) ;
404 fPHOSHeader->SetBeamEnergy(fBeamEnergy) ;
408 userVector[i]=*(recBuf+21+i);
410 Swab4(userVector, userVector, 16);
411 fPHOSHeader->SetUserVector(userVector) ;
414 // ZEBRA event header
416 zheader[i]=*(recBuf+47+i);
418 Swab4(zheader, zheader, 12);
419 fPHOSHeader->SetHeader(zheader) ;
423 Swab4(recBuf, recBuf, nwphr);
425 /* Physical record control words */
426 UInt_t * recptr = recBuf; //Pointer to current position
429 Error("ProcessRawFile", "Cannot handle fast blocks" ) ;
435 // Logical record control words
436 UInt_t lrtyp = recptr[1];
438 Error("ProcessRawFile", "Can not handle logical record type %d", lrtyp ) ;
444 if (recptr[0] != fCKW) {
445 Error("ProcessRawFile", "Bad check word" ) ;
450 UInt_t nwuh = recptr[9];
454 UInt_t nd = recptr[8]; /* Number of data words */
458 UInt_t evtno = recptr[2]; /* Event number */
461 Info("ProcessRawFile", "evtno= %d", evtno);
463 UInt_t nh = recptr[4]; /* Number of header words in data bank */
466 // Unswap data from VME
468 Swab4(recptr, recptr, nd-nh-3);
470 // Give buffer to monitor program
471 // UInt_t esize = nd-nh-3;
473 // Swab2(recptr, recptr, esize);
474 // Two byte data are correct after this operation.
475 //But we're in trouble if the data array contains 4 byte data!
477 // From now on deal with VME data (MSB first, or network byte order).
480 // Fill the event with data from ADCs
481 UChar_t *byteptr=(UChar_t*)recptr;
483 // Trigger bit register
484 pattern=net2host(*(UShort_t*)byteptr);
485 fPHOSHeader->SetPattern(pattern) ;
486 byteptr+=sizeof(UShort_t);
488 // Either peak ADCs, 10 modulesX8=80 channels,
489 //or Kurchatov 64+2 channel ADC
490 //(the rest of the channels padded with 0xffff)
492 Int_t peak = static_cast<Int_t>(net2host(*(UShort_t*)byteptr));
494 Int_t absID = fctdb->Raw2AbsId(i) ;
496 new((*fDigits)[i])AliPHOSDigit(-1,absID,peak,0.,i) ;
498 if(peak>(UShort_t)1000)
499 Info("ProcessRawFile", "event= %d peak[%d] = %f", fEvent, i, peak);
501 byteptr+=sizeof(UShort_t);
504 // Scanning ADCs, 4 modulesX8=32 channels
506 scanning[i]=net2host(*(UShort_t*)byteptr);
507 byteptr+=sizeof(UShort_t);
509 fPHOSHeader->SetScanning(scanning) ;
511 // Charge ADCs, 1 moduleX12=12 channels
513 charge[i]=net2host(*(UShort_t*)byteptr);
514 byteptr+=sizeof(UShort_t);
516 fPHOSHeader->SetCharge(charge) ;
518 // Scalers, 1 moduleX12=12 (4 byte) channels
520 scaler[i]=net2host(*(UInt_t*)byteptr);
521 byteptr+=sizeof(UInt_t);
523 fPHOSHeader->SetScaler(scaler) ;
525 // LeCroy TDC 2228A, 4 moduleX8=32 channels
527 tdc2228[i]=net2host(*(UShort_t*)byteptr);
528 byteptr+=sizeof(UShort_t);
530 fPHOSHeader->SetTDC(tdc2228) ;
534 Info("ProcessRawFile", "event= %d written", fEvent) ;
537 UInt_t nb = nwphr *sizeof(UInt_t);
538 n = fread( recBuf, nb,1,dataFile);
544 if (static_cast<UInt_t>(n) != 1) {
547 return kTRUE ; //all read
557 //____________________________________________________________________________
558 void AliPHOSRaw2Digits::Swab4(void *from, void *to, size_t nwords)const
560 // The function swaps 4 bytes: byte#3<-->byte#0, byte#2<-->byte#1
561 register char *pf=static_cast<char*>(from) ;
562 register char *pt=static_cast<char*>(to) ;
564 while (nwords-- > 0 ) {
576 //____________________________________________________________________________
577 void AliPHOSRaw2Digits::Swab2(void *from, void *to, size_t nwords)const
579 //The function swaps 2x2 bytes: byte#0<-->byte#1, byte#2<-->byte#3
580 register char *pf=static_cast<char*>(from) ;
581 register char *pt=static_cast<char*>(to);
583 while (nwords-- > 0 ) {
595 //____________________________________________________________________________
596 void AliPHOSRaw2Digits::WriteDigits(void){
597 //In this method we create TreeD, write digits and Raw2Digits to it
598 // and write Header to TreeE. Finally we write TreeD to root file
602 fDigits->Expand(fDigits->GetEntriesFast()) ;
603 for(Int_t i=0;i<fDigits->GetEntriesFast(); i++)
604 static_cast<AliPHOSDigit*>(fDigits->At(i))->SetIndexInList(i) ;
607 sprintf(hname,"TreeD%d",fEvent%fMaxPerFile);
608 TTree * treeD = new TTree(hname,"Digits");
609 //treeD->Write(0,TObject::kOverwrite);
611 // -- create Digits branch
612 Int_t bufferSize = 32000 ;
613 TBranch * digitsBranch = treeD->Branch("PHOS",&fDigits,bufferSize);
614 digitsBranch->SetTitle("Default");
616 // -- Create Digitizer branch
617 Int_t splitlevel = 0 ;
618 const AliPHOSRaw2Digits * d = this ;
619 TBranch * digitizerBranch = treeD->Branch("AliPHOSRaw2Digits",
620 "AliPHOSRaw2Digits", &d,bufferSize,splitlevel);
621 digitizerBranch->SetTitle("Default");
625 digitsBranch->Fill() ;
626 digitizerBranch->Fill() ;
627 treeD->Write(0,TObject::kOverwrite);
633 gAlice->TreeE()->Fill();
635 //____________________________________________________________________________
636 void AliPHOSRaw2Digits::Print()const{
637 //prints current configuration and status.
639 printf("----------AliPHOSRaw2Digits---------- \n") ;
640 printf("Current input File: %s\n",fInName.Data()) ;
641 printf("Current output File: %s\n", GetTitle());
642 printf("Events processes in the last file %d\n",fEvent) ;
643 printf("Input file status\n") ;
645 case 0: printf("`Have not processed yet'\n") ;
647 case 1: printf("`Processed normally'\n") ;
649 case -1: printf("`File not found'\n") ;
651 case -2: printf("`Error in reading'\n") ;
653 case -3: printf("'Interupted'\n") ;
656 printf("Connection table: " );
658 printf("%s %s \n",fctdb->GetName(), fctdb->GetTitle() ) ;
660 printf(" no DB \n" );