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 to calculate calibration parameters from beam tests etc.
25 // First pass - one should calcuate pedestals, in the second pass - gains.
27 // To calculate pedestals we scan pedestals events and fill histos for each
28 // channel. Then in each histo we find maximum and fit it with Gaussian in the
29 // visinity of the maximum. Extracted mean and width of distribution are put into
30 // resulting histogram which cheks results for 'reasonability'.
32 // To evaluate gains: scans beam events and calculate gain in the cristall with
33 // maximal energy deposition, assuming, that 90% of energy deposited in it.
34 // For each channel separate histogramm is filled. When scan is finished,
35 // these histograms are fitted with Gaussian and finds mean and width, which
36 // are put in final histogram. Finaly gains are checked for deviation from mean.
38 // Finally fills database.
41 // AliPHOSCalibrator * c = new AliPHOSCalibrator("path/galice.root") ;
42 // c->AddRun("path2/galice.root") ;
43 // c->ScanPedestals();
44 // c->CalculatePedestals();
45 // c->WritePedestals();
47 // c->CalculateGains() ;
50 //*-- Author : D.Peressounko (RRC KI)
51 //////////////////////////////////////////////////////////////////////////////
53 // --- ROOT system ---
56 #include "TObjString.h"
58 #include "TClonesArray.h"
60 // --- Standard library ---
62 // --- AliRoot header files ---
64 #include "AliPHOSCalibrManager.h"
65 #include "AliPHOSCalibrationData.h"
66 #include "AliPHOSCalibrator.h"
67 #include "AliPHOSConTableDB.h"
68 #include "AliRawReaderDate.h"
69 #include "AliPHOSRawStream.h"
70 #include "AliPHOSDigit.h"
75 #define PHYSICS_EVENT 7
78 ClassImp(AliPHOSCalibrator)
81 //____________________________________________________________________________
82 AliPHOSCalibrator::AliPHOSCalibrator():TTask("AliPHOSCalibrator","Default")
84 //Default constuctor for root. Normally should not be used
93 fConTableDB = "Beamtest2002" ;
94 fConTableDBFile = "ConTableDB.root" ;
96 //____________________________________________________________________________
97 AliPHOSCalibrator::AliPHOSCalibrator(const char* file, const char* title):
98 TTask("AliPHOSCalibrator",title)
100 //Constructor which should normally be used.
101 //file: path/galice.root - header file
102 //title: branch name of PHOS reconstruction (e.g. "Default")
105 fRunList = new TList() ;
106 fRunList->SetOwner() ;
107 fRunList->Add(new TObjString(file)) ;
114 fAcceptCorr = 10 ; //Maximal deviation from mean, considered as normal
116 fGainAcceptCorr = 5 ; //Factor for gain deviation
122 fConTableDB = "Beamtest2002" ;
123 fConTableDBFile = "ConTableDB.root" ;
126 //____________________________________________________________________________
127 AliPHOSCalibrator::~AliPHOSCalibrator()
137 delete fhPedestalsWid ;
143 //____________________________________________________________________________
144 void AliPHOSCalibrator::AddRun(const char * filename)
146 //Adds one more run to list of runs, which will be scanned in ScanXXX methods
148 TObjString * fn = new TObjString(filename) ;
150 fRunList=new TList() ;
151 fRunList->SetOwner() ;
156 TIter next(fRunList) ;
158 while((r=(TObjString *)(next()))){
159 if(fn->String().CompareTo(r->String())==0){
160 AliError(Form("Run already in list: %s",filename)) ;
168 //____________________________________________________________________________
169 void AliPHOSCalibrator::Exec(Option_t * option)
171 // reads parameters and does the calibration
172 ScanPedestals(option);
173 CalculatePedestals();
179 //____________________________________________________________________________
180 void AliPHOSCalibrator::Init(void)
182 // intializes everything
184 //check if ConTableDB already read
186 SetConTableDB(fConTableDBFile) ;
189 fNch = fctdb->GetNchanels() ;
190 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
191 fhPedestalsWid= new TH1F("hPedestalsWid","Pedestals width",fNch,0.,fNch) ;
192 fhGains = new TH1F("hGains","Gains ",fNch,0.,fNch) ;
193 fhGainsWid = new TH1F("hGainsWid","Gains width",fNch,0.,fNch) ;
195 //____________________________________________________________________________
196 void AliPHOSCalibrator::SetConTableDB(const char * file,const char * name)
198 //Reads Connection Table database with name "name" from file "file"
200 if(file==0 || name == 0){
201 AliError(Form("Please, specify file with database and its title")) ;
204 if(fctdb && strcmp(fctdb->GetTitle(),name)==0) //already read
213 TFile * v = gROOT->GetFile(fConTableDBFile) ;
215 v = TFile::Open(fConTableDBFile) ;
217 AliError(Form("Can not open file with Connection Table DB: %s",fConTableDBFile.Data())) ;
220 fctdb = new AliPHOSConTableDB(*(dynamic_cast<AliPHOSConTableDB *>(v->Get("AliPHOSConTableDB")))) ;
224 //____________________________________________________________________________
225 void AliPHOSCalibrator::PlotPedestal(Int_t chanel)
227 //Plot histogram for a given channel, filled in Scan method
228 if(fPedHistos && fPedHistos->GetEntriesFast()){
229 static_cast<TH1F*>(fPedHistos->At(chanel))->Draw() ;
232 AliInfo(Form("Histograms not created yet! \n")) ;
235 //____________________________________________________________________________
236 void AliPHOSCalibrator::PlotPedestals(void)
238 // draws pedestals distribution
239 fhPedestals->Draw() ;
241 //____________________________________________________________________________
242 void AliPHOSCalibrator::PlotGain(Int_t chanel)
244 //Plot histogram for a given channel, filled in Scan method
245 if(fGainHistos && fGainHistos->GetEntriesFast()){
246 static_cast<TH1F*>(fGainHistos->At(chanel))->Draw() ;
249 AliInfo(Form("Histograms not created yet! \n")) ;
252 //____________________________________________________________________________
253 void AliPHOSCalibrator::PlotGains(void)
255 // draws gains distribution
258 //____________________________________________________________________________
259 void AliPHOSCalibrator::ScanPedestals(Option_t * option )
261 //scan all files in list fRunList and fill pedestal hisgrams
262 //option: "clear" - clear pedestal histograms filled up to now
263 // "deb" - plot file name currently processed
268 if(fPedHistos && strstr(option,"clear"))
269 fPedHistos->Delete() ;
271 fPedHistos = new TObjArray(fNch) ;
273 //Create histos for each channel, fills them and extracts mean values.
274 //First - prepare histos
276 for(ich=0;ich<fNch ;ich++){
277 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
281 TString name("Pedestal for channel ") ;
283 fPedHistos->AddAt(new TH1F(n,name,fNChan,0,fNChan),ich) ;
287 TIter next(fRunList) ;
289 while((file = static_cast<TObjString *>(next()))){
290 if(strstr(option,"deb"))
291 printf("Processing file %s \n ",file->String().Data()) ;
294 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
295 AliPHOSRawStream *rawStream = new AliPHOSRawStream(rawReader) ;
296 rawStream->SetConTableDB(fctdb) ;
297 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
299 //Scan all event in file
300 while(rawReader->NextEvent()){
301 //Is it PHYSICAL event
302 if(rawReader->GetType() == PHYSICS_EVENT){
304 if(rawStream->ReadDigits(digits)){
305 if(rawStream->IsPEDevent()){
306 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
307 AliPHOSDigit * digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
308 ich = fctdb->AbsId2Raw(digit->GetId());
310 Float_t amp = digit->GetAmp() ;
311 TH1F * hh = dynamic_cast<TH1F*>(fPedHistos->At(ich)) ;
319 if(strstr(option,"deb"))
320 AliInfo(Form(" found %d events \n ",nevents)) ;
326 //____________________________________________________________________________
327 void AliPHOSCalibrator::CalculatePedestals()
329 //Fit histograms, filled in ScanPedestals method with Gaussian
330 //find mean and width, check deviation from mean for each channel.
332 if(!fPedHistos || !fPedHistos->At(0)){
333 AliError(Form("You should run ScanPedestals first!")) ;
337 //Now fit results with Gauss
338 TF1 * gs = new TF1("gs","gaus",0.,10000.) ;
340 for(ich=0;ich<fNch ;ich++){
341 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
342 Int_t max = h->GetMaximumBin() ;
343 Axis_t xmin = max/2. ;
344 Axis_t xmax = max*3/2 ;
345 gs->SetRange(xmin,xmax) ;
347 par[0] = h->GetBinContent(max) ;
350 gs->SetParameters(par[0],par[1],par[2]) ;
352 gs->GetParameters(par) ;
353 fhPedestals->SetBinContent(ich,par[1]) ;
354 fhPedestals->SetBinError(ich,par[2]) ;
355 fhPedestalsWid->Fill(ich,par[2]) ;
359 //now check reasonability of results
360 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
361 fhPedestals->Fit("p0","Q") ;
363 p0->GetParameters(&meanPed);
364 for(ich=0;ich<fNch ;ich++){
365 Float_t ped = fhPedestals->GetBinContent(ich) ;
366 if(ped < 0 || ped > meanPed+fAcceptCorr){
367 TString out("Pedestal of channel ") ;
371 out+= "it is too far from mean " ;
373 AliError(Form("PHOSCalibrator %s",out.Data())) ;
379 //____________________________________________________________________________
380 void AliPHOSCalibrator::ScanGains(Option_t * option)
382 //Scan all runs, listed in fRunList and fill histograms for all channels
383 //options: "clear" - clean histograms, filled up to now
384 // "deb" - print current file name
385 // "narrow" - scan only narrow beam events
389 if(fGainHistos && strstr(option,"clear"))
390 fGainHistos->Delete() ;
392 if(strstr(option,"deball"))
393 AliInfo(Form("creating array for %d channels \n",fNch)) ;
394 fGainHistos = new TObjArray(fNch) ;
397 //Create histos for each channel, fills them and extracts mean values.
398 //First - prepare histos
400 if(!fGainHistos->GetEntriesFast()){
402 for(ich=0;ich<fNch ;ich++){
405 TString name("Gains for channel ") ;
407 fGainHistos->AddAt(new TH1F(n,name,fNGainBins,0,fGainMax),ich) ;
408 // static_cast<TH1F*>(fGainHistos->At(ich))->Sumw2() ;
412 TIter next(fRunList) ;
414 while((file = static_cast<TObjString *>(next()))){
416 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
417 AliPHOSRawStream *rawStream = new AliPHOSRawStream(rawReader) ;
418 rawStream->SetConTableDB(fctdb) ;
420 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
422 //Scan all event in file
423 while(rawReader->NextEvent()){
424 //Is it PHYSICAL event
425 if(rawReader->GetType() == PHYSICS_EVENT){
426 if(rawStream->ReadDigits(digits)){
428 if(rawStream->IsNELevent() || rawStream->IsWELevent()){
430 AliPHOSDigit * digit ;
433 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
434 digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
435 if(digit->GetAmp() > max){
437 max = digit->GetAmp() ;
440 digit = static_cast<AliPHOSDigit *>(digits->At(imax) ) ;
441 Int_t ich = fctdb->AbsId2Raw(digit->GetId());
443 Float_t pedestal = fhPedestals->GetBinContent(ich) ;
444 const Float_t kshowerInCrystall = 0.9 ;
445 Float_t gain = fBeamEnergy*kshowerInCrystall/
446 (digit->GetAmp() - pedestal) ;
447 static_cast<TH1F*>(fGainHistos->At(ich))->Fill(gain) ;
456 if(strstr(option,"deb"))
457 AliInfo(Form(" found %d events \n",nevents)) ;
460 //____________________________________________________________________________
461 void AliPHOSCalibrator::CalculateGains(void)
465 if(!fGainHistos || !fGainHistos->GetEntriesFast()){
466 AliError(Form("You should run ScanGains first!")) ;
470 //Fit results with Landau
471 TF1 * gs = new TF1("gs","landau",0.,10000.) ;
473 for(ich=0;ich<fNch ;ich++){
474 TH1F * h = static_cast<TH1F *>(fGainHistos->At(ich)) ;
475 Int_t bmax = h->GetMaximumBin() ;
476 Axis_t center = h->GetBinCenter(bmax) ;
477 Axis_t xmin = center - 0.01 ;
478 Axis_t xmax = center + 0.02 ;
479 gs->SetRange(xmin,xmax) ;
481 par[0] = h->GetBinContent(bmax) ;
484 gs->SetParameters(par[0],par[1],par[2]) ;
486 gs->GetParameters(par) ;
487 fhGains->SetBinContent(ich,par[1]) ;
488 fhGains->SetBinError(ich,par[2]) ;
489 fhGainsWid->Fill(ich,par[2]) ;
493 //now check reasonability of results
494 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
495 fhGains->Fit("p0","Q") ;
497 p0->GetParameters(&meanGain);
498 for(ich=0;ich<fNch ;ich++){
499 Float_t gain = fhGains->GetBinContent(ich) ;
500 if(gain < meanGain/fGainAcceptCorr || gain > meanGain*fGainAcceptCorr){
501 TString out("Gain of channel ") ;
505 out+= "it is too far from mean " ;
507 AliError(Form("PHOSCalibrator %s",out.Data())) ;
513 //____________________________________________________________________________
514 void AliPHOSCalibrator::ReadFromASCII(const char * filename){
515 // We read pedestals and gains from *.dat file with following format:
516 // 0 0 0 0 37.09 1972. // next nmodrows*nmodcols*ncryrows*ncrycols lines
517 // 0 0 0 1 28.53 2072. // contains <RR CC r c ped peak>
518 // 0 0 0 2 30.93 1938. //
519 // where module is an array of 8*8 crystals and RR and CC are module raw and column position
520 FILE * file = fopen(filename, "r");
522 Error("ReadFromASCII", "could not open file %s", filename);
525 if(!fctdb || !fhPedestals || !fhGains){
533 Int_t modRaw,modCol,raw,col;
536 while(fscanf(file,"%d %d %d %d %f %f",&modRaw,&modCol,&raw,&col,&ped,&pik)==6){
537 //Calculate plain crystal position:
538 Int_t rawPosition = (modRaw*8+raw)*fctdb->GetNColumns()+modCol*8+col ;
539 fhPedestals->SetBinContent(rawPosition,ped) ;
541 fhGains->SetBinContent(rawPosition,1./pik);
543 fhGains->SetBinContent(rawPosition,0.);
546 if(nread != fctdb->GetNColumns()*fctdb->GetNRaws()){
547 Error("ReadFromASCII","Read %d parameters instead of %d\n",nread,fctdb->GetNColumns()*fctdb->GetNRaws()) ;
551 //_____________________________________________________________________________
552 void AliPHOSCalibrator::WritePedestals(const char * version)
554 //Write calculated data to file using AliPHOSCalibrManager
555 //version and validitirange (begin-end) will be used to identify data
558 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
562 AliPHOSCalibrationData ped("Pedestals",version);
563 for(Int_t i=0; i<fNch;i++){
564 Int_t absid=fctdb->Raw2AbsId(i) ;
565 ped.SetData(absid,fhPedestals->GetBinContent(i)) ;
566 ped.SetDataCheck(absid,fhPedestalsWid->GetBinContent(i)) ;
569 // //evaluate validity range
571 // TIter next(fRunList) ;
572 // Int_t ibegin=99999;
574 // TObjString * file ;
575 // while((file=((TObjString*)next()))){
576 // TString s = file->GetString() ;
577 // TString ss = s(s.Last('_'),s.Last('.'));
579 // if(sscanf(ss.Data(),"%d",&tmp)){
586 // ped.SetValidityRange(ibegin,iend) ;
589 // ped.SetValidityRange(begin,end) ;
591 //check, may be Manager instance already configured?
592 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
594 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
595 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
597 cmngr->WriteData(ped) ;
599 //_____________________________________________________________________________
600 void AliPHOSCalibrator::ReadPedestals(const char * version)
602 //Read data from file using AliPHOSCalibrManager
603 //version and range will be used to choose proper data
605 AliPHOSCalibrationData ped("Pedestals",version);
606 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
608 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
609 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
611 cmngr->GetParameters(ped) ;
612 Int_t npeds=ped.NChannels() ;
613 fNch = fctdb->GetNchanels() ;
616 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
617 for(Int_t i=0;i<npeds;i++){
618 Int_t raw =fctdb->AbsId2Raw(i) ;
620 fhPedestals->SetBinContent(raw-1,ped.Data(i)) ;
621 fhPedestals->SetBinError(raw-1,ped.DataCheck(i)) ;
625 //_____________________________________________________________________________
626 void AliPHOSCalibrator::ReadGains(const char * version)
628 //Read data from file using AliPHOSCalibrManager
629 //version and range will be used to choose proper data
631 AliPHOSCalibrationData gains("Gains",version);
632 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
634 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
635 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
637 cmngr->GetParameters(gains) ;
638 Int_t npeds=gains.NChannels() ;
639 fNch = fctdb->GetNchanels() ;
642 fhGains = new TH1F("hGainss","Gains mean",fNch,0.,fNch) ;
643 for(Int_t i=0;i<npeds;i++){
644 Int_t raw =fctdb->AbsId2Raw(i) ;
646 fhGains->SetBinContent(raw-1,gains.Data(i)) ;
647 fhGains->SetBinError(raw-1,gains.DataCheck(i)) ;
651 //_____________________________________________________________________________
652 void AliPHOSCalibrator::WriteGains(const char * version)
654 //Write gains through AliPHOSCalibrManager
655 //version and validity range(begin-end) are used to identify data
658 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
662 AliPHOSCalibrationData gains("Gains",version);
663 for(Int_t i=0; i<fNch;i++){
664 Int_t absid=fctdb->Raw2AbsId(i) ;
665 gains.SetData(absid,fhGains->GetBinContent(i)) ;
666 gains.SetDataCheck(absid,fhGainsWid->GetBinContent(i)) ;
669 // TIter next(fRunList) ;
670 // Int_t ibegin=99999;
672 // TObjString * file ;
673 // while((file=((TObjString*)next()))){
674 // TString s = file->GetString() ;
675 // TSubString ss = s(s.Last('_'),s.Last('.'));
677 // if(sscanf(ss.Data(),"%d",&tmp)){
684 // gains.SetValidityRange(ibegin,iend) ;
687 // gains.SetValidityRange(begin,end) ;
688 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
690 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
691 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
693 cmngr->WriteData(gains) ;
695 //_____________________________________________________________________________
696 void AliPHOSCalibrator::Print(const Option_t *)const
699 AliInfo(Form("--------------PHOS Calibrator-----------------\n")) ;
700 printf("Files to handle:\n") ;
701 TIter next(fRunList) ;
703 while((r=(TObjString *)(next())))
704 printf(" %s\n",r->GetName()) ;
706 printf("Name of ConTableDB:.....................%s\n",fConTableDB.Data()) ;
707 printf("File of ConTableDB:.....................%s\n",fConTableDBFile.Data() ) ;
708 printf("Maximal deviation from mean Gain (factor):.%f\n",fGainAcceptCorr) ;
709 printf("Maximal deviation of Pedestal from mean:...%f\n",fAcceptCorr) ;
710 printf("Range used in Gain histos:..............%f\n",fGainMax) ;
711 printf("Number of bins in Gain histos:..........%d\n",fNGainBins) ;
712 printf("Number of channels to calibrate:........%d\n",fNch) ;
713 printf("Number of bins in pedestal histos:......%d\n",fNChan) ;
714 printf("--------------------------------------------------\n") ;