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 ---
71 #include <netinet/in.h>
73 // --- AliRoot header files ---
74 #include "AliPHOSDigit.h"
75 #include "AliPHOSConTableDB.h"
76 #include "AliPHOSBeamTestEvent.h"
77 #include "AliPHOSRaw2Digits.h"
78 #include "AliPHOSv1.h"
79 #include "../EVGEN/AliGenBox.h"
82 ClassImp(AliPHOSRaw2Digits)
85 //____________________________________________________________________________
86 AliPHOSRaw2Digits::AliPHOSRaw2Digits():TTask()
94 fDebug = kFALSE; // Debug flag
96 fIsInitialized = kFALSE ;
104 fMaxPerFile = 20000 ;
108 //____________________________________________________________________________
109 AliPHOSRaw2Digits::AliPHOSRaw2Digits(const char * filename,Bool_t toSplit):TTask("Default","")
113 TString outname("") ;
115 outname = "galice.root" ;
119 outname.ReplaceAll(".fz",".root") ;
120 outname.ReplaceAll(".gz","") ;
129 fDebug = kFALSE; // Debug flag
130 fIsInitialized = kFALSE ;
138 fMaxPerFile = 20000 ;
143 //____________________________________________________________________________
144 AliPHOSRaw2Digits::~AliPHOSRaw2Digits()
147 fPHOSHeader->Delete() ;
154 //____________________________________________________________________________
155 void AliPHOSRaw2Digits::Exec(Option_t * option){
156 //This is steering method performing all the conversion
158 if(!fIsInitialized) //need initialization
159 if(!Init()) //failed to initialize
165 //____________________________________________________________________________
166 Bool_t AliPHOSRaw2Digits::Init(void){
167 //Makes initialization of contaniers
172 //Make container for digits
173 fDigits = new TClonesArray("AliPHOSDigit",1000) ;
174 fPHOSHeader = new AliPHOSBeamTestEvent() ;
175 fIsInitialized = kTRUE ;
176 return StartRootFiles() ;
179 //____________________________________________________________________________
180 Bool_t AliPHOSRaw2Digits::StartRootFiles(void ){
181 //Create PHOS geometry, sets magnetic field to zero,
182 //create Generator - to store target position,
183 //opens out file, creates TreeE
185 //create gAlice if nececcary
187 new AliRun("gAlice","The ALICE Off-line Simulation Framework") ;
190 if(!gAlice->GetModule("PHOS"))
191 new AliPHOSv1("PHOS","GPS2") ;
194 gAlice->SetField(0,2);
196 //Set positin of the virtex
197 AliGenerator * gener = gAlice->Generator() ;
199 gener = new AliGenBox(1);
200 Float_t ox = fTarget[1];
201 Float_t oy = fTarget[2]+460.;
202 Float_t oz = fTarget[0];
203 gener->SetOrigin(ox, oy, oz);
206 Int_t nRootFile = (fEvent+1)/fMaxPerFile ;
209 sprintf(dname,"%d",nRootFile) ;
210 if(gSystem->AccessPathName(dname)) //strange return: 0 if exists
211 if(gSystem->MakeDirectory(dname)!=0)
212 Fatal("StartRootFiles","Can not make directory %s \n",dname) ;
214 if(!gSystem->ChangeDirectory(dname))
215 Fatal("StartRootFiles","Can not cd to %s\n",dname) ;
218 // Create the output file
219 TString outname("") ;
220 if(strstr(GetTitle(),"root")){
226 outname.ReplaceAll(".fz",".root") ;
229 fHeaderFile = new TFile(outname,"recreate");
230 fHeaderFile->SetCompressionLevel(2);
232 // Create the Root Trees
233 gAlice->MakeTree("E") ;
236 Int_t splitlevel = 0 ;
237 Int_t bufferSize = 32000 ;
238 TBranch * headerBranch = gAlice->TreeE()->Branch("AliPHOSBeamTestEvent",
239 "AliPHOSBeamTestEvent",
240 &fPHOSHeader,bufferSize,splitlevel);
241 headerBranch->SetName("AliPHOSBeamTestEvent") ;
244 fDigitsFile = new TFile("PHOS.Digits.root","recreate") ;
245 fDigitsFile->SetCompressionLevel(2) ;
249 //____________________________________________________________________________
250 Bool_t AliPHOSRaw2Digits::CloseRootFiles(void ){
251 //cleans everething to start next root file
253 printf("writing gAlice \n") ;
255 gAlice->Write(0,TObject::kOverwrite);
256 gAlice->TreeE()->Write(0,TObject::kOverwrite);
262 fHeaderFile->Close() ;
268 fDigitsFile->Close() ;
273 Int_t nRootFile = (fEvent-1)/fMaxPerFile ;
275 if(!gSystem->ChangeDirectory("../")){
276 Fatal("CloseRootFile","Can not return to initial dir \n") ;
282 //____________________________________________________________________________
283 Bool_t AliPHOSRaw2Digits::ProcessRawFile(){
285 //Method opens zebra file and reads successively bytes from it,
286 //filling corresponding fields in header and digits.
290 //First of all, open file and check if it is a zebra file
293 sprintf(command,"zcat %s",fInName.Data());
294 FILE *dataFile = popen(command, "r");
295 if (dataFile == NULL) {
296 Warning("ProcessRawFile", " Cannot open file %s\n", fInName.Data() ) ;
297 perror(fInName.Data()) ;
302 // Check if byte ordering is little-endian
303 UInt_t w = 0x12345678;
304 Int_t swapo = memcmp(&w, "\x78\x56\x34\x12", sizeof(UInt_t)) == 0;
306 Info("ProcessRawFile", "swapo=%f\n", swapo ) ;
311 // Read physical record control words
312 UInt_t nb = 8*sizeof(UInt_t);
313 Int_t n = fread(recBuf, nb, 1, dataFile);
314 if(static_cast<UInt_t>(n) != 1) {
316 perror(fInName.Data());
318 Error("ProcessRawFile", "Could not read physical record control words" ) ;
324 Info("ProcessRawFile", "recbuf[0] = %d\n", recBuf[0] );
326 // Check if it is a ZEBRA file and if the words are swapped
328 if (recBuf[0] != fMK1) {
329 Swab4(recBuf, &w, 1);
331 Error("ProcessRawFile", "Does not look like a ZEBRA file\n" ) ;
341 message = " w = %f\n" ;
342 message += " swapi = %f\n" ;
343 Info("ProcessRawFile", message.Data(), w, swapi ) ;
346 // Get number of words in physical record
349 Swab4(&recBuf[4],&nwphr,1);
352 nwphr*=2; // 1998 -- Now we have 2 records 150 words each
355 //start loop over data
356 // Read rest of record
357 nb = (nwphr-8)*sizeof(UInt_t);
358 n = fread(&recBuf[8], nb, 1, dataFile) ;
359 if (static_cast<UInt_t>(n) != 1) {
361 perror(fInName.Data());
366 nb = nwphr *sizeof(UInt_t);
368 UInt_t userVector[16] ;
371 UShort_t scanning[32] ;
374 UShort_t tdc2228[32];
376 //read untill the end of file
382 if((fEvent%fMaxPerFile == 0) && fEvent ){
386 gAlice->SetEvent(fEvent%fMaxPerFile) ;
389 fPHOSHeader->SetBeamEnergy(fBeamEnergy) ;
393 userVector[i]=*(recBuf+21+i);
395 Swab4(userVector, userVector, 16);
396 fPHOSHeader->SetUserVector(userVector) ;
399 // ZEBRA event header
401 zheader[i]=*(recBuf+47+i);
403 Swab4(zheader, zheader, 12);
404 fPHOSHeader->SetHeader(zheader) ;
408 Swab4(recBuf, recBuf, nwphr);
410 /* Physical record control words */
411 UInt_t * recptr = recBuf; //Pointer to current position
414 Error("ProcessRawFile", "Cannot handle fast blocks" ) ;
420 // Logical record control words
421 UInt_t lrtyp = recptr[1];
423 Error("ProcessRawFile", "Can not handle logical record type %d", lrtyp ) ;
429 if (recptr[0] != fCKW) {
430 Error("ProcessRawFile", "Bad check word" ) ;
435 UInt_t nwuh = recptr[9];
439 UInt_t nd = recptr[8]; /* Number of data words */
443 UInt_t evtno = recptr[2]; /* Event number */
446 Info("ProcessRawFile", "evtno= %d", evtno);
448 UInt_t nh = recptr[4]; /* Number of header words in data bank */
451 // Unswap data from VME
453 Swab4(recptr, recptr, nd-nh-3);
455 // Give buffer to monitor program
456 // UInt_t esize = nd-nh-3;
458 // Swab2(recptr, recptr, esize);
459 // Two byte data are correct after this operation.
460 //But we're in trouble if the data array contains 4 byte data!
462 // From now on deal with VME data (MSB first, or network byte order).
465 // Fill the event with data from ADCs
466 UChar_t *byteptr=(UChar_t*)recptr;
468 // Trigger bit register
469 pattern=net2host(*(UShort_t*)byteptr);
470 fPHOSHeader->SetPattern(pattern) ;
471 byteptr+=sizeof(UShort_t);
473 // Either peak ADCs, 10 modulesX8=80 channels,
474 //or Kurchatov 64+2 channel ADC
475 //(the rest of the channels padded with 0xffff)
477 Int_t peak = static_cast<Int_t>(net2host(*(UShort_t*)byteptr));
479 Int_t absID = fctdb->Raw2AbsId(i) ;
481 new((*fDigits)[i])AliPHOSDigit(-1,absID,peak,0.,i) ;
483 if(peak>(UShort_t)1000)
484 Info("ProcessRawFile", "event= %d peak[%d] = %f", fEvent, i, peak);
486 byteptr+=sizeof(UShort_t);
489 // Scanning ADCs, 4 modulesX8=32 channels
491 scanning[i]=net2host(*(UShort_t*)byteptr);
492 byteptr+=sizeof(UShort_t);
494 fPHOSHeader->SetScanning(scanning) ;
496 // Charge ADCs, 1 moduleX12=12 channels
498 charge[i]=net2host(*(UShort_t*)byteptr);
499 byteptr+=sizeof(UShort_t);
501 fPHOSHeader->SetCharge(charge) ;
503 // Scalers, 1 moduleX12=12 (4 byte) channels
505 scaler[i]=net2host(*(UInt_t*)byteptr);
506 byteptr+=sizeof(UInt_t);
508 fPHOSHeader->SetScaler(scaler) ;
510 // LeCroy TDC 2228A, 4 moduleX8=32 channels
512 tdc2228[i]=net2host(*(UShort_t*)byteptr);
513 byteptr+=sizeof(UShort_t);
515 fPHOSHeader->SetTDC(tdc2228) ;
519 Info("ProcessRawFile", "event= %d written", fEvent) ;
522 UInt_t nb = nwphr *sizeof(UInt_t);
523 n = fread( recBuf, nb,1,dataFile);
529 if (static_cast<UInt_t>(n) != 1) {
532 return kTRUE ; //all read
541 //____________________________________________________________________________
542 void AliPHOSRaw2Digits::Swab4(void *from, void *to, size_t nwords){
543 // The function swaps 4 bytes: byte#3<-->byte#0, byte#2<-->byte#1
544 register char *pf=static_cast<char*>(from) ;
545 register char *pt=static_cast<char*>(to) ;
547 while (nwords-- > 0 ) {
559 //____________________________________________________________________________
560 void AliPHOSRaw2Digits::Swab2(void *from, void *to, size_t nwords)
561 { //The function swaps 2x2 bytes: byte#0<-->byte#1, byte#2<-->byte#3
562 register char *pf=static_cast<char*>(from) ;
563 register char *pt=static_cast<char*>(to);
565 while (nwords-- > 0 ) {
577 //____________________________________________________________________________
578 void AliPHOSRaw2Digits::WriteDigits(void){
579 //In this method we create TreeD, write digits and Raw2Digits to it
580 // and write Header to TreeE. Finally we write TreeD to root file
584 fDigits->Expand(fDigits->GetEntriesFast()) ;
585 for(Int_t i=0;i<fDigits->GetEntriesFast(); i++)
586 static_cast<AliPHOSDigit*>(fDigits->At(i))->SetIndexInList(i) ;
589 sprintf(hname,"TreeD%d",fEvent%fMaxPerFile);
590 TTree * treeD = new TTree(hname,"Digits");
591 //treeD->Write(0,TObject::kOverwrite);
593 // -- create Digits branch
594 Int_t bufferSize = 32000 ;
595 TBranch * digitsBranch = treeD->Branch("PHOS",&fDigits,bufferSize);
596 digitsBranch->SetTitle("Default");
598 // -- Create Digitizer branch
599 Int_t splitlevel = 0 ;
600 const AliPHOSRaw2Digits * d = this ;
601 TBranch * digitizerBranch = treeD->Branch("AliPHOSRaw2Digits",
602 "AliPHOSRaw2Digits", &d,bufferSize,splitlevel);
603 digitizerBranch->SetTitle("Default");
607 digitsBranch->Fill() ;
608 digitizerBranch->Fill() ;
609 treeD->Write(0,TObject::kOverwrite);
615 gAlice->TreeE()->Fill();
617 //____________________________________________________________________________
618 void AliPHOSRaw2Digits::Print(Option_t * option)const{
621 printf("----------AliPHOSRaw2Digits---------- \n") ;
622 printf("Current input File: %s\n",fInName.Data()) ;
623 printf("Current output File: %s\n", GetTitle());
624 printf("Events processes in the last file %d\n",fEvent) ;
625 printf("Input file status\n") ;
627 case 0: printf("`Have not processed yet'\n") ;
629 case 1: printf("`Processed normally'\n") ;
631 case -1: printf("`File not found'\n") ;
633 case -2: printf("`Error in reading'\n") ;
635 case -3: printf("'Interupted'\n") ;
638 printf("Connection table: " );
640 printf("%s %s \n",fctdb->GetName(), fctdb->GetTitle() ) ;
642 printf(" no DB \n" );