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:
21 * Revision 1.11 2006/01/13 16:59:39 kharlov
22 * Rename classes to read raw data 2004
24 * Revision 1.10 2005/05/28 14:19:04 schutz
25 * Compilation warnings fixed by T.P.
29 //_________________________________________________________________________
30 // Class to calculate calibration parameters from beam tests etc.
31 // First pass - one should calcuate pedestals, in the second pass - gains.
33 // To calculate pedestals we scan pedestals events and fill histos for each
34 // channel. Then in each histo we find maximum and fit it with Gaussian in the
35 // visinity of the maximum. Extracted mean and width of distribution are put into
36 // resulting histogram which cheks results for 'reasonability'.
38 // To evaluate gains: scans beam events and calculate gain in the cristall with
39 // maximal energy deposition, assuming, that 90% of energy deposited in it.
40 // For each channel separate histogramm is filled. When scan is finished,
41 // these histograms are fitted with Gaussian and finds mean and width, which
42 // are put in final histogram. Finaly gains are checked for deviation from mean.
44 // Finally fills database.
47 // AliPHOSCalibrator * c = new AliPHOSCalibrator("path/galice.root") ;
48 // c->AddRun("path2/galice.root") ;
49 // c->ScanPedestals();
50 // c->CalculatePedestals();
51 // c->WritePedestals();
53 // c->CalculateGains() ;
56 //*-- Author : D.Peressounko (RRC KI)
57 //////////////////////////////////////////////////////////////////////////////
59 // --- ROOT system ---
62 #include "TObjString.h"
64 #include "TClonesArray.h"
66 // --- Standard library ---
68 // --- AliRoot header files ---
70 #include "AliPHOSCalibrManager.h"
71 #include "AliPHOSCalibrationData.h"
72 #include "AliPHOSCalibrator.h"
73 #include "AliPHOSConTableDB.h"
74 #include "AliRawReaderDate.h"
75 #include "AliPHOSRawStream2004.h"
76 #include "AliPHOSDigit.h"
81 #define PHYSICS_EVENT 7
84 ClassImp(AliPHOSCalibrator)
87 //____________________________________________________________________________
88 AliPHOSCalibrator::AliPHOSCalibrator() :
89 TTask("AliPHOSCalibrator","Default"),
98 fConTableDB("Beamtest2002"),
99 fConTableDBFile("ConTableDB.root"),
108 //Default constuctor for root. Normally should not be used
111 //____________________________________________________________________________
112 AliPHOSCalibrator::AliPHOSCalibrator(const char* file, const char* title):
113 TTask("AliPHOSCalibrator",title),
122 fConTableDB("Beamtest2002"),
123 fConTableDBFile("ConTableDB.root"),
133 //Constructor which should normally be used.
134 //file: path/galice.root - header file
135 //title: branch name of PHOS reconstruction (e.g. "Default")
136 fRunList->SetOwner();
137 fRunList->Add(new TObjString(file));
140 //____________________________________________________________________________
141 AliPHOSCalibrator::AliPHOSCalibrator(const AliPHOSCalibrator & ctor) :
151 fConTableDB("Beamtest2002"),
152 fConTableDBFile("ConTableDB.root"),
161 // cpy ctor: no implementation yet
162 // requested by the Coding Convention
163 Fatal("cpy ctor", "not implemented") ;
166 //____________________________________________________________________________
167 AliPHOSCalibrator::~AliPHOSCalibrator()
177 delete fhPedestalsWid ;
183 //____________________________________________________________________________
184 void AliPHOSCalibrator::AddRun(const char * filename)
186 //Adds one more run to list of runs, which will be scanned in ScanXXX methods
188 TObjString * fn = new TObjString(filename) ;
190 fRunList=new TList() ;
191 fRunList->SetOwner() ;
196 TIter next(fRunList) ;
198 while((r=(TObjString *)(next()))){
199 if(fn->String().CompareTo(r->String())==0){
200 AliError(Form("Run already in list: %s",filename)) ;
208 //____________________________________________________________________________
209 void AliPHOSCalibrator::Exec(Option_t * option)
211 // reads parameters and does the calibration
212 ScanPedestals(option);
213 CalculatePedestals();
219 //____________________________________________________________________________
220 void AliPHOSCalibrator::Init(void)
222 // intializes everything
224 //check if ConTableDB already read
226 SetConTableDB(fConTableDBFile) ;
229 fNch = fctdb->GetNchanels() ;
230 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
231 fhPedestalsWid= new TH1F("hPedestalsWid","Pedestals width",fNch,0.,fNch) ;
232 fhGains = new TH1F("hGains","Gains ",fNch,0.,fNch) ;
233 fhGainsWid = new TH1F("hGainsWid","Gains width",fNch,0.,fNch) ;
235 //____________________________________________________________________________
236 void AliPHOSCalibrator::SetConTableDB(const char * file,const char * name)
238 //Reads Connection Table database with name "name" from file "file"
240 if(file==0 || name == 0){
241 AliError(Form("Please, specify file with database and its title")) ;
244 if(fctdb && strcmp(fctdb->GetTitle(),name)==0) //already read
253 TFile * v = gROOT->GetFile(fConTableDBFile) ;
255 v = TFile::Open(fConTableDBFile) ;
257 AliError(Form("Can not open file with Connection Table DB: %s",fConTableDBFile.Data())) ;
260 fctdb = new AliPHOSConTableDB(*(dynamic_cast<AliPHOSConTableDB *>(v->Get("AliPHOSConTableDB")))) ;
264 //____________________________________________________________________________
265 void AliPHOSCalibrator::PlotPedestal(Int_t chanel)
267 //Plot histogram for a given channel, filled in Scan method
268 if(fPedHistos && fPedHistos->GetEntriesFast()){
269 static_cast<TH1F*>(fPedHistos->At(chanel))->Draw() ;
272 AliInfo(Form("Histograms not created yet! \n")) ;
275 //____________________________________________________________________________
276 void AliPHOSCalibrator::PlotPedestals(void)
278 // draws pedestals distribution
279 fhPedestals->Draw() ;
281 //____________________________________________________________________________
282 void AliPHOSCalibrator::PlotGain(Int_t chanel)
284 //Plot histogram for a given channel, filled in Scan method
285 if(fGainHistos && fGainHistos->GetEntriesFast()){
286 static_cast<TH1F*>(fGainHistos->At(chanel))->Draw() ;
289 AliInfo(Form("Histograms not created yet! \n")) ;
292 //____________________________________________________________________________
293 void AliPHOSCalibrator::PlotGains(void)
295 // draws gains distribution
298 //____________________________________________________________________________
299 void AliPHOSCalibrator::ScanPedestals(Option_t * option )
301 //scan all files in list fRunList and fill pedestal hisgrams
302 //option: "clear" - clear pedestal histograms filled up to now
303 // "deb" - plot file name currently processed
308 if(fPedHistos && strstr(option,"clear"))
309 fPedHistos->Delete() ;
311 fPedHistos = new TObjArray(fNch) ;
313 //Create histos for each channel, fills them and extracts mean values.
314 //First - prepare histos
316 for(ich=0;ich<fNch ;ich++){
317 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
321 TString name("Pedestal for channel ") ;
323 fPedHistos->AddAt(new TH1F(n,name,fNChan,0,fNChan),ich) ;
327 TIter next(fRunList) ;
329 while((file = static_cast<TObjString *>(next()))){
330 if(strstr(option,"deb"))
331 printf("Processing file %s \n ",file->String().Data()) ;
334 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
335 AliPHOSRawStream2004 *rawStream = new AliPHOSRawStream2004(rawReader) ;
336 rawStream->SetConTableDB(fctdb) ;
337 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
339 //Scan all event in file
340 while(rawReader->NextEvent()){
341 //Is it PHYSICAL event
342 if(rawReader->GetType() == PHYSICS_EVENT){
344 if(rawStream->ReadDigits(digits)){
345 if(rawStream->IsPEDevent()){
346 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
347 AliPHOSDigit * digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
348 ich = fctdb->AbsId2Raw(digit->GetId());
350 Float_t amp = digit->GetAmp() ;
351 TH1F * hh = dynamic_cast<TH1F*>(fPedHistos->At(ich)) ;
359 if(strstr(option,"deb"))
360 AliInfo(Form(" found %d events \n ",nevents)) ;
366 //____________________________________________________________________________
367 void AliPHOSCalibrator::CalculatePedestals()
369 //Fit histograms, filled in ScanPedestals method with Gaussian
370 //find mean and width, check deviation from mean for each channel.
372 if(!fPedHistos || !fPedHistos->At(0)){
373 AliError(Form("You should run ScanPedestals first!")) ;
377 //Now fit results with Gauss
378 TF1 * gs = new TF1("gs","gaus",0.,10000.) ;
380 for(ich=0;ich<fNch ;ich++){
381 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
382 Int_t max = h->GetMaximumBin() ;
383 Axis_t xmin = max/2. ;
384 Axis_t xmax = max*3/2 ;
385 gs->SetRange(xmin,xmax) ;
387 par[0] = h->GetBinContent(max) ;
390 gs->SetParameters(par[0],par[1],par[2]) ;
392 gs->GetParameters(par) ;
393 fhPedestals->SetBinContent(ich,par[1]) ;
394 fhPedestals->SetBinError(ich,par[2]) ;
395 fhPedestalsWid->Fill(ich,par[2]) ;
399 //now check reasonability of results
400 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
401 fhPedestals->Fit("p0","Q") ;
403 p0->GetParameters(&meanPed);
404 for(ich=0;ich<fNch ;ich++){
405 Float_t ped = fhPedestals->GetBinContent(ich) ;
406 if(ped < 0 || ped > meanPed+fAcceptCorr){
407 TString out("Pedestal of channel ") ;
411 out+= "it is too far from mean " ;
413 AliError(Form("PHOSCalibrator %s",out.Data())) ;
419 //____________________________________________________________________________
420 void AliPHOSCalibrator::ScanGains(Option_t * option)
422 //Scan all runs, listed in fRunList and fill histograms for all channels
423 //options: "clear" - clean histograms, filled up to now
424 // "deb" - print current file name
425 // "narrow" - scan only narrow beam events
429 if(fGainHistos && strstr(option,"clear"))
430 fGainHistos->Delete() ;
432 if(strstr(option,"deball"))
433 AliInfo(Form("creating array for %d channels \n",fNch)) ;
434 fGainHistos = new TObjArray(fNch) ;
437 //Create histos for each channel, fills them and extracts mean values.
438 //First - prepare histos
440 if(!fGainHistos->GetEntriesFast()){
442 for(ich=0;ich<fNch ;ich++){
445 TString name("Gains for channel ") ;
447 fGainHistos->AddAt(new TH1F(n,name,fNGainBins,0,fGainMax),ich) ;
448 // static_cast<TH1F*>(fGainHistos->At(ich))->Sumw2() ;
452 TIter next(fRunList) ;
454 while((file = static_cast<TObjString *>(next()))){
456 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
457 AliPHOSRawStream2004 *rawStream = new AliPHOSRawStream2004(rawReader) ;
458 rawStream->SetConTableDB(fctdb) ;
460 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
462 //Scan all event in file
463 while(rawReader->NextEvent()){
464 //Is it PHYSICAL event
465 if(rawReader->GetType() == PHYSICS_EVENT){
466 if(rawStream->ReadDigits(digits)){
468 if(rawStream->IsNELevent() || rawStream->IsWELevent()){
470 AliPHOSDigit * digit ;
473 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
474 digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
475 if(digit->GetAmp() > max){
477 max = digit->GetAmp() ;
480 digit = static_cast<AliPHOSDigit *>(digits->At(imax) ) ;
481 Int_t ich = fctdb->AbsId2Raw(digit->GetId());
483 Float_t pedestal = fhPedestals->GetBinContent(ich) ;
484 const Float_t kshowerInCrystall = 0.9 ;
485 Float_t gain = fBeamEnergy*kshowerInCrystall/
486 (digit->GetAmp() - pedestal) ;
487 static_cast<TH1F*>(fGainHistos->At(ich))->Fill(gain) ;
496 if(strstr(option,"deb"))
497 AliInfo(Form(" found %d events \n",nevents)) ;
500 //____________________________________________________________________________
501 void AliPHOSCalibrator::CalculateGains(void)
505 if(!fGainHistos || !fGainHistos->GetEntriesFast()){
506 AliError(Form("You should run ScanGains first!")) ;
510 //Fit results with Landau
511 TF1 * gs = new TF1("gs","landau",0.,10000.) ;
513 for(ich=0;ich<fNch ;ich++){
514 TH1F * h = static_cast<TH1F *>(fGainHistos->At(ich)) ;
515 Int_t bmax = h->GetMaximumBin() ;
516 Axis_t center = h->GetBinCenter(bmax) ;
517 Axis_t xmin = center - 0.01 ;
518 Axis_t xmax = center + 0.02 ;
519 gs->SetRange(xmin,xmax) ;
521 par[0] = h->GetBinContent(bmax) ;
524 gs->SetParameters(par[0],par[1],par[2]) ;
526 gs->GetParameters(par) ;
527 fhGains->SetBinContent(ich,par[1]) ;
528 fhGains->SetBinError(ich,par[2]) ;
529 fhGainsWid->Fill(ich,par[2]) ;
533 //now check reasonability of results
534 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
535 fhGains->Fit("p0","Q") ;
537 p0->GetParameters(&meanGain);
538 for(ich=0;ich<fNch ;ich++){
539 Float_t gain = fhGains->GetBinContent(ich) ;
540 if(gain < meanGain/fGainAcceptCorr || gain > meanGain*fGainAcceptCorr){
541 TString out("Gain of channel ") ;
545 out+= "it is too far from mean " ;
547 AliError(Form("PHOSCalibrator %s",out.Data())) ;
553 //____________________________________________________________________________
554 void AliPHOSCalibrator::ReadFromASCII(const char * filename){
555 // We read pedestals and gains from *.dat file with following format:
556 // 0 0 0 0 37.09 1972. // next nmodrows*nmodcols*ncryrows*ncrycols lines
557 // 0 0 0 1 28.53 2072. // contains <RR CC r c ped peak>
558 // 0 0 0 2 30.93 1938. //
559 // where module is an array of 8*8 crystals and RR and CC are module raw and column position
560 FILE * file = fopen(filename, "r");
562 Error("ReadFromASCII", "could not open file %s", filename);
565 if(!fctdb || !fhPedestals || !fhGains){
573 Int_t modRaw,modCol,raw,col;
576 while(fscanf(file,"%d %d %d %d %f %f",&modRaw,&modCol,&raw,&col,&ped,&pik)==6){
577 //Calculate plain crystal position:
578 Int_t rawPosition = (modRaw*8+raw)*fctdb->GetNColumns()+modCol*8+col ;
579 fhPedestals->SetBinContent(rawPosition,ped) ;
581 fhGains->SetBinContent(rawPosition,1./pik);
583 fhGains->SetBinContent(rawPosition,0.);
586 if(nread != fctdb->GetNColumns()*fctdb->GetNRaws()){
587 Error("ReadFromASCII","Read %d parameters instead of %d\n",nread,fctdb->GetNColumns()*fctdb->GetNRaws()) ;
591 //_____________________________________________________________________________
592 void AliPHOSCalibrator::WritePedestals(const char * version)
594 //Write calculated data to file using AliPHOSCalibrManager
595 //version and validitirange (begin-end) will be used to identify data
598 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
602 AliPHOSCalibrationData ped("Pedestals",version);
603 for(Int_t i=0; i<fNch;i++){
604 Int_t absid=fctdb->Raw2AbsId(i) ;
605 ped.SetData(absid,fhPedestals->GetBinContent(i)) ;
606 ped.SetDataCheck(absid,fhPedestalsWid->GetBinContent(i)) ;
609 // //evaluate validity range
611 // TIter next(fRunList) ;
612 // Int_t ibegin=99999;
614 // TObjString * file ;
615 // while((file=((TObjString*)next()))){
616 // TString s = file->GetString() ;
617 // TString ss = s(s.Last('_'),s.Last('.'));
619 // if(sscanf(ss.Data(),"%d",&tmp)){
626 // ped.SetValidityRange(ibegin,iend) ;
629 // ped.SetValidityRange(begin,end) ;
631 //check, may be Manager instance already configured?
632 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
634 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
635 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
637 cmngr->WriteData(ped) ;
639 //_____________________________________________________________________________
640 void AliPHOSCalibrator::ReadPedestals(const char * version)
642 //Read data from file using AliPHOSCalibrManager
643 //version and range will be used to choose proper data
645 AliPHOSCalibrationData ped("Pedestals",version);
646 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
648 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
649 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
651 cmngr->GetParameters(ped) ;
652 Int_t npeds=ped.NChannels() ;
653 fNch = fctdb->GetNchanels() ;
656 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
657 for(Int_t i=0;i<npeds;i++){
658 Int_t raw =fctdb->AbsId2Raw(i) ;
660 fhPedestals->SetBinContent(raw-1,ped.Data(i)) ;
661 fhPedestals->SetBinError(raw-1,ped.DataCheck(i)) ;
665 //_____________________________________________________________________________
666 void AliPHOSCalibrator::ReadGains(const char * version)
668 //Read data from file using AliPHOSCalibrManager
669 //version and range will be used to choose proper data
671 AliPHOSCalibrationData gains("Gains",version);
672 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
674 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
675 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
677 cmngr->GetParameters(gains) ;
678 Int_t npeds=gains.NChannels() ;
679 fNch = fctdb->GetNchanels() ;
682 fhGains = new TH1F("hGainss","Gains mean",fNch,0.,fNch) ;
683 for(Int_t i=0;i<npeds;i++){
684 Int_t raw =fctdb->AbsId2Raw(i) ;
686 fhGains->SetBinContent(raw-1,gains.Data(i)) ;
687 fhGains->SetBinError(raw-1,gains.DataCheck(i)) ;
691 //_____________________________________________________________________________
692 void AliPHOSCalibrator::WriteGains(const char * version)
694 //Write gains through AliPHOSCalibrManager
695 //version and validity range(begin-end) are used to identify data
698 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
702 AliPHOSCalibrationData gains("Gains",version);
703 for(Int_t i=0; i<fNch;i++){
704 Int_t absid=fctdb->Raw2AbsId(i) ;
705 gains.SetData(absid,fhGains->GetBinContent(i)) ;
706 gains.SetDataCheck(absid,fhGainsWid->GetBinContent(i)) ;
709 // TIter next(fRunList) ;
710 // Int_t ibegin=99999;
712 // TObjString * file ;
713 // while((file=((TObjString*)next()))){
714 // TString s = file->GetString() ;
715 // TSubString ss = s(s.Last('_'),s.Last('.'));
717 // if(sscanf(ss.Data(),"%d",&tmp)){
724 // gains.SetValidityRange(ibegin,iend) ;
727 // gains.SetValidityRange(begin,end) ;
728 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
730 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
731 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
733 cmngr->WriteData(gains) ;
735 //_____________________________________________________________________________
736 void AliPHOSCalibrator::Print(const Option_t *)const
739 AliInfo(Form("--------------PHOS Calibrator-----------------\n")) ;
740 printf("Files to handle:\n") ;
741 TIter next(fRunList) ;
743 while((r=(TObjString *)(next())))
744 printf(" %s\n",r->GetName()) ;
746 printf("Name of ConTableDB:.....................%s\n",fConTableDB.Data()) ;
747 printf("File of ConTableDB:.....................%s\n",fConTableDBFile.Data() ) ;
748 printf("Maximal deviation from mean Gain (factor):.%f\n",fGainAcceptCorr) ;
749 printf("Maximal deviation of Pedestal from mean:...%f\n",fAcceptCorr) ;
750 printf("Range used in Gain histos:..............%f\n",fGainMax) ;
751 printf("Number of bins in Gain histos:..........%d\n",fNGainBins) ;
752 printf("Number of channels to calibrate:........%d\n",fNch) ;
753 printf("Number of bins in pedestal histos:......%d\n",fNChan) ;
754 printf("--------------------------------------------------\n") ;