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 /* History of cvs commits:
23 //_________________________________________________________________________
24 // Class designed to convert raw data to aliroot format. We assume, that
25 // prototype is situated in the center of 3 PHOS module and convert prototype
26 // outpur to AliPHOSDigits. In addition, we fill branch of TreeE with
27 // AliPHOSBeamTestEvent, contaning description of event(triggers etc).
28 // Note, that one byte per channel in raw data is transvormed to class
29 // AliPHOSDigit, so finale zise increase ~ 100 times. So output can be split
30 // into peases of reasonable size: each file can not contain more than
31 // fMaxPerFile: if there are still events in raw file, then new directory
32 // is created and header+digits files are written to it.
35 // AliPHOSRaw2Digits * r = new AliPHOSRaw2Digits("path/input.file") ;
36 // //note, that it can be gzipped file!
37 // //Set position of the target in the given run.
38 // //Z axis along beam direction, from target to prototype (0-surface of prototype)
39 // //X axis along columns of prototype (0-center of prototype)
40 // //Y axis along raws of prototype (0-center of prototype)
41 // Double_t pos[3]={0,0,-452.} ;
42 // r->SetTargetPosition(pos) ;
43 // //Read/create connection Table:
44 // TFile f("ConTableDB.root") ;
45 // AliPHOSConTableDB * cdb = f.Get("AliPHOSConTableDB") ;
47 // r->SetConTableDB(cdb) ;
50 // As a result files galice.root and PHOS.Digits.root should be produced in
51 // current dir, and, possibly, dirs 1,2,3... each with galice.root and PHOS.Digits.root,
52 // where the rest of data are written.
54 /*-- Author: Maxim Volkov (RRC KI)
55 Dmitri Peressounko (RRC KI & SUBATECH)
56 Yuri Kharlov (IHEP & SUBATECH) */
58 //////////////////////////////////////////////////////////////////////////////
60 // --- ROOT system ---
61 #include "TClonesArray.h"
67 // --- Standard library ---
71 // --- AliRoot header files ---
72 #include "AliPHOSDigit.h"
73 #include "AliPHOSConTableDB.h"
74 #include "AliPHOSBeamTestEvent.h"
75 #include "AliPHOSRaw2Digits.h"
78 ClassImp(AliPHOSRaw2Digits)
81 //____________________________________________________________________________
82 AliPHOSRaw2Digits::AliPHOSRaw2Digits():TTask()
84 //As one can easily see, this is constructor.
91 fDebug = kFALSE; // Debug flag
92 fIsInitialized = kFALSE ;
100 fMaxPerFile = 20000 ; //Maximal number of events in root file.
104 //____________________________________________________________________________
105 AliPHOSRaw2Digits::AliPHOSRaw2Digits(const char * filename):TTask("Default","")
107 //this constructor should be normally used. Parameters: imput file
109 TString outname("") ;
113 outname.ReplaceAll(".fz",".root") ;
114 outname.ReplaceAll(".gz","") ;
123 fDebug = kFALSE; // Debug flag
124 fIsInitialized = kFALSE ;
132 fMaxPerFile = 20000 ;
136 //____________________________________________________________________________
137 AliPHOSRaw2Digits::AliPHOSRaw2Digits(AliPHOSRaw2Digits & r2d):TTask(r2d.GetName(), r2d.GetTitle())
140 fInName=r2d.fInName ;
147 fDebug = kFALSE; // Debug flag
148 fIsInitialized = kFALSE ;
149 fTarget[0] = r2d.fTarget[0] ;
150 fTarget[1] = r2d.fTarget[1] ;
151 fTarget[2] = r2d.fTarget[2] ;
152 fDigits = r2d.fDigits ;
153 fPHOSHeader = r2d.fPHOSHeader ;
154 fHeaderFile = new TFile( (r2d.fHeaderFile)->GetName(), "new" ) ;
155 fDigitsFile = new TFile( (r2d.fDigitsFile)->GetName(), "new" ) ;
156 fMaxPerFile = r2d.fMaxPerFile ;
157 fEvent = r2d.fEvent ;
158 fctdb = new AliPHOSConTableDB( *(r2d.fctdb) ) ;
161 //____________________________________________________________________________
162 AliPHOSRaw2Digits::~AliPHOSRaw2Digits()
166 fPHOSHeader->Delete() ;
173 //____________________________________________________________________________
174 void AliPHOSRaw2Digits::Exec(const Option_t *){
175 //This is steering method performing all the conversion
177 if(!fIsInitialized) //need initialization
178 if(!Init()) //failed to initialize
184 //____________________________________________________________________________
185 Bool_t AliPHOSRaw2Digits::Init(void){
186 //Makes initialization of contaniers
191 //Make container for digits
192 fDigits = new TClonesArray("AliPHOSDigit",1000) ;
193 fPHOSHeader = new AliPHOSBeamTestEvent() ;
194 fIsInitialized = kTRUE ;
195 return StartRootFiles() ;
198 //____________________________________________________________________________
199 Bool_t AliPHOSRaw2Digits::StartRootFiles(void ) const {
200 // //Create PHOS geometry, sets magnetic field to zero,
201 // //create Generator - to store target position,
202 // //opens out file, creates TreeE
204 // //create gAlice if nececcary
206 // new AliRun("gAlice","The ALICE Off-line Simulation Framework") ;
209 // if(!gAlice->GetModule("PHOS"))
210 // new AliPHOSv1("PHOS","GPS2") ;
212 // //Set Magnetic field
213 // gAlice->SetField(0,2);
215 // //Set positin of the virtex
216 // AliGenerator * gener = gAlice->Generator() ;
218 // gener = new AliGenBox(1);
219 // Float_t ox = fTarget[1];
220 // Float_t oy = fTarget[2]+460.;
221 // Float_t oz = fTarget[0];
222 // gener->SetOrigin(ox, oy, oz);
225 // Int_t nRootFile = (fEvent+1)/fMaxPerFile ;
228 // sprintf(dname,"%d",nRootFile) ;
229 // if(gSystem->AccessPathName(dname)) //strange return: 0 if exists
230 // if(gSystem->MakeDirectory(dname)!=0)
231 // Fatal("StartRootFiles","Can not make directory %s \n",dname) ;
233 // if(!gSystem->ChangeDirectory(dname))
234 // Fatal("StartRootFiles","Can not cd to %s\n",dname) ;
237 // // Create the output file
238 // TString outname("") ;
239 // if(strstr(GetTitle(),"root")){
240 // outname=GetTitle();
243 // outname = fInName ;
244 // outname.ToLower() ;
245 // outname.ReplaceAll(".fz",".root") ;
248 // fHeaderFile = new TFile(outname,"recreate");
249 // fHeaderFile->SetCompressionLevel(2);
251 // // Create the Root Trees
253 // gime->MakeTree("E") ;
256 // Int_t splitlevel = 0 ;
257 // Int_t bufferSize = 32000 ;
258 // TBranch * headerBranch = gAlice->TreeE()->Branch("AliPHOSBeamTestEvent",
259 // "AliPHOSBeamTestEvent",
260 // &fPHOSHeader,bufferSize,splitlevel);
261 // headerBranch->SetName("AliPHOSBeamTestEvent") ;
264 // // fDigitsFile = new TFile("PHOS.Digits.root","recreate") ;
265 // // fDigitsFile->SetCompressionLevel(2) ;
269 //____________________________________________________________________________
270 Bool_t AliPHOSRaw2Digits::CloseRootFiles(void ){
271 //cleans everething to start next root file
273 printf("writing gAlice \n") ;
275 gAlice->Write(0,TObject::kOverwrite);
276 gAlice->TreeE()->Write(0,TObject::kOverwrite);
282 fHeaderFile->Close() ;
288 fDigitsFile->Close() ;
293 Int_t nRootFile = (fEvent-1)/fMaxPerFile ;
295 if(!gSystem->ChangeDirectory("../")){
296 Fatal("CloseRootFile","Can not return to initial dir \n") ;
302 //____________________________________________________________________________
303 Bool_t AliPHOSRaw2Digits::ProcessRawFile(){
305 //Method opens zebra file and reads successively bytes from it,
306 //filling corresponding fields in header and digits.
310 //First of all, open file and check if it is a zebra file
313 sprintf(command,"zcat %s",fInName.Data());
314 FILE *dataFile = popen(command, "r");
315 if (dataFile == NULL) {
316 Warning("ProcessRawFile", " Cannot open file %s\n", fInName.Data() ) ;
317 perror(fInName.Data()) ;
322 // Check if byte ordering is little-endian
323 UInt_t w = 0x12345678;
324 Int_t swapo = memcmp(&w, "\x78\x56\x34\x12", sizeof(UInt_t)) == 0;
326 Info("ProcessRawFile", "swapo=%f\n", swapo ) ;
331 // Read physical record control words
332 UInt_t nb = 8*sizeof(UInt_t);
333 Int_t n = fread(recBuf, nb, 1, dataFile);
334 if(static_cast<UInt_t>(n) != 1) {
336 perror(fInName.Data());
338 Error("ProcessRawFile", "Could not read physical record control words" ) ;
344 Info("ProcessRawFile", "recbuf[0] = %d\n", recBuf[0] );
346 // Check if it is a ZEBRA file and if the words are swapped
348 if (recBuf[0] != fMK1) {
349 Swab4(recBuf, &w, 1);
351 Error("ProcessRawFile", "Does not look like a ZEBRA file\n" ) ;
361 message = " w = %f\n" ;
362 message += " swapi = %f\n" ;
363 Info("ProcessRawFile", message.Data(), w, swapi ) ;
366 // Get number of words in physical record
369 Swab4(&recBuf[4],&nwphr,1);
372 nwphr*=2; // 1998 -- Now we have 2 records 150 words each
375 //start loop over data
376 // Read rest of record
377 nb = (nwphr-8)*sizeof(UInt_t);
378 n = fread(&recBuf[8], nb, 1, dataFile) ;
379 if (static_cast<UInt_t>(n) != 1) {
381 perror(fInName.Data());
386 nb = nwphr *sizeof(UInt_t);
388 UInt_t userVector[16] ;
391 UShort_t scanning[32] ;
394 UShort_t tdc2228[32];
396 //read untill the end of file
402 if((fEvent%fMaxPerFile == 0) && fEvent ){
406 gAlice->SetEvent(fEvent%fMaxPerFile) ;
409 fPHOSHeader->SetBeamEnergy(fBeamEnergy) ;
413 userVector[i]=*(recBuf+21+i);
415 Swab4(userVector, userVector, 16);
416 fPHOSHeader->SetUserVector(userVector) ;
419 // ZEBRA event header
421 zheader[i]=*(recBuf+47+i);
423 Swab4(zheader, zheader, 12);
424 fPHOSHeader->SetHeader(zheader) ;
428 Swab4(recBuf, recBuf, nwphr);
430 /* Physical record control words */
431 UInt_t * recptr = recBuf; //Pointer to current position
434 Error("ProcessRawFile", "Cannot handle fast blocks" ) ;
440 // Logical record control words
441 UInt_t lrtyp = recptr[1];
443 Error("ProcessRawFile", "Can not handle logical record type %d", lrtyp ) ;
449 if (recptr[0] != fCKW) {
450 Error("ProcessRawFile", "Bad check word" ) ;
455 UInt_t nwuh = recptr[9];
459 UInt_t nd = recptr[8]; /* Number of data words */
463 UInt_t evtno = recptr[2]; /* Event number */
466 Info("ProcessRawFile", "evtno= %d", evtno);
468 UInt_t nh = recptr[4]; /* Number of header words in data bank */
471 // Unswap data from VME
473 Swab4(recptr, recptr, nd-nh-3);
475 // Give buffer to monitor program
476 // UInt_t esize = nd-nh-3;
478 // Swab2(recptr, recptr, esize);
479 // Two byte data are correct after this operation.
480 //But we're in trouble if the data array contains 4 byte data!
482 // From now on deal with VME data (MSB first, or network byte order).
485 // Fill the event with data from ADCs
486 UChar_t *byteptr=(UChar_t*)recptr;
488 // Trigger bit register
489 pattern=net2host(*(UShort_t*)byteptr);
490 fPHOSHeader->SetPattern(pattern) ;
491 byteptr+=sizeof(UShort_t);
493 // Either peak ADCs, 10 modulesX8=80 channels,
494 //or Kurchatov 64+2 channel ADC
495 //(the rest of the channels padded with 0xffff)
497 Int_t peak = static_cast<Int_t>(net2host(*(UShort_t*)byteptr));
499 Int_t absID = fctdb->Raw2AbsId(i) ;
501 new((*fDigits)[i])AliPHOSDigit(-1,absID,peak,0.,i) ;
503 if(peak>(UShort_t)1000)
504 Info("ProcessRawFile", "event= %d peak[%d] = %f", fEvent, i, peak);
506 byteptr+=sizeof(UShort_t);
509 // Scanning ADCs, 4 modulesX8=32 channels
511 scanning[i]=net2host(*(UShort_t*)byteptr);
512 byteptr+=sizeof(UShort_t);
514 fPHOSHeader->SetScanning(scanning) ;
516 // Charge ADCs, 1 moduleX12=12 channels
518 charge[i]=net2host(*(UShort_t*)byteptr);
519 byteptr+=sizeof(UShort_t);
521 fPHOSHeader->SetCharge(charge) ;
523 // Scalers, 1 moduleX12=12 (4 byte) channels
525 scaler[i]=net2host(*(UInt_t*)byteptr);
526 byteptr+=sizeof(UInt_t);
528 fPHOSHeader->SetScaler(scaler) ;
530 // LeCroy TDC 2228A, 4 moduleX8=32 channels
532 tdc2228[i]=net2host(*(UShort_t*)byteptr);
533 byteptr+=sizeof(UShort_t);
535 fPHOSHeader->SetTDC(tdc2228) ;
539 Info("ProcessRawFile", "event= %d written", fEvent) ;
542 UInt_t nb = nwphr *sizeof(UInt_t);
543 n = fread( recBuf, nb,1,dataFile);
549 if (static_cast<UInt_t>(n) != 1) {
552 return kTRUE ; //all read
562 //____________________________________________________________________________
563 void AliPHOSRaw2Digits::Swab4(void *from, void *to, size_t nwords)const
565 // The function swaps 4 bytes: byte#3<-->byte#0, byte#2<-->byte#1
566 register char *pf=static_cast<char*>(from) ;
567 register char *pt=static_cast<char*>(to) ;
569 while (nwords-- > 0 ) {
581 //____________________________________________________________________________
582 void AliPHOSRaw2Digits::Swab2(void *from, void *to, size_t nwords)const
584 //The function swaps 2x2 bytes: byte#0<-->byte#1, byte#2<-->byte#3
585 register char *pf=static_cast<char*>(from) ;
586 register char *pt=static_cast<char*>(to);
588 while (nwords-- > 0 ) {
600 //____________________________________________________________________________
601 void AliPHOSRaw2Digits::WriteDigits(void){
602 //In this method we create TreeD, write digits and Raw2Digits to it
603 // and write Header to TreeE. Finally we write TreeD to root file
607 fDigits->Expand(fDigits->GetEntriesFast()) ;
608 for(Int_t i=0;i<fDigits->GetEntriesFast(); i++)
609 static_cast<AliPHOSDigit*>(fDigits->At(i))->SetIndexInList(i) ;
612 sprintf(hname,"TreeD%d",fEvent%fMaxPerFile);
613 TTree * treeD = new TTree(hname,"Digits");
614 //treeD->Write(0,TObject::kOverwrite);
616 // -- create Digits branch
617 Int_t bufferSize = 32000 ;
618 TBranch * digitsBranch = treeD->Branch("PHOS",&fDigits,bufferSize);
619 digitsBranch->SetTitle("Default");
621 // -- Create Digitizer branch
622 Int_t splitlevel = 0 ;
623 const AliPHOSRaw2Digits * d = this ;
624 TBranch * digitizerBranch = treeD->Branch("AliPHOSRaw2Digits",
625 "AliPHOSRaw2Digits", &d,bufferSize,splitlevel);
626 digitizerBranch->SetTitle("Default");
630 digitsBranch->Fill() ;
631 digitizerBranch->Fill() ;
632 treeD->Write(0,TObject::kOverwrite);
638 gAlice->TreeE()->Fill();
640 //____________________________________________________________________________
641 void AliPHOSRaw2Digits::Print(const Option_t *)const{
642 //prints current configuration and status.
644 printf("----------AliPHOSRaw2Digits---------- \n") ;
645 printf("Current input File: %s\n",fInName.Data()) ;
646 printf("Current output File: %s\n", GetTitle());
647 printf("Events processes in the last file %d\n",fEvent) ;
648 printf("Input file status\n") ;
650 case 0: printf("`Have not processed yet'\n") ;
652 case 1: printf("`Processed normally'\n") ;
654 case -1: printf("`File not found'\n") ;
656 case -2: printf("`Error in reading'\n") ;
658 case -3: printf("'Interupted'\n") ;
661 printf("Connection table: " );
663 printf("%s %s \n",fctdb->GetName(), fctdb->GetTitle() ) ;
665 printf(" no DB \n" );