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 **************************************************************************/
17 //_________________________________________________________________________
18 // Class to calculate calibration parameters from beam tests etc.
19 // First pass - one should calcuate pedestals, in the second pass - gains.
21 // To calculate pedestals we scan pedestals events and fill histos for each
22 // channel. Then in each histo we find maximum and fit it with Gaussian in the
23 // visinity of the maximum. Extracted mean and width of distribution are put into
24 // resulting histogram which cheks results for 'reasonability'.
26 // To evaluate gains: scans beam events and calculate gain in the cristall with
27 // maximal energy deposition, assuming, that 90% of energy deposited in it.
28 // For each channel separate histogramm is filled. When scan is finished,
29 // these histograms are fitted with Gaussian and finds mean and width, which
30 // are put in final histogram. Finaly gains are checked for deviation from mean.
32 // Finally fills database.
35 // AliPHOSCalibrator * c = new AliPHOSCalibrator("path/galice.root") ;
36 // c->AddRun("path2/galice.root") ;
37 // c->ScanPedestals();
38 // c->CalculatePedestals();
39 // c->WritePedestals();
41 // c->CalculateGains() ;
44 //*-- Author : D.Peressounko (RRC KI)
45 //////////////////////////////////////////////////////////////////////////////
47 // --- ROOT system ---
50 #include "TObjString.h"
52 #include "TClonesArray.h"
54 // --- Standard library ---
56 // --- AliRoot header files ---
58 #include "AliPHOSCalibrManager.h"
59 #include "AliPHOSCalibrationData.h"
60 #include "AliPHOSCalibrator.h"
61 #include "AliPHOSConTableDB.h"
62 #include "AliRawReaderDate.h"
63 #include "AliPHOSRawStream.h"
64 #include "AliPHOSDigit.h"
69 #define PHYSICS_EVENT 7
72 ClassImp(AliPHOSCalibrator)
75 //____________________________________________________________________________
76 AliPHOSCalibrator::AliPHOSCalibrator():TTask("AliPHOSCalibrator","Default")
78 //Default constuctor for root. Normally should not be used
87 fConTableDB = "Beamtest2002" ;
88 fConTableDBFile = "ConTableDB.root" ;
90 //____________________________________________________________________________
91 AliPHOSCalibrator::AliPHOSCalibrator(const char* file, const char* title):
92 TTask("AliPHOSCalibrator",title)
94 //Constructor which should normally be used.
95 //file: path/galice.root - header file
96 //title: branch name of PHOS reconstruction (e.g. "Default")
99 fRunList = new TList() ;
100 fRunList->SetOwner() ;
101 fRunList->Add(new TObjString(file)) ;
108 fAcceptCorr = 10 ; //Maximal deviation from mean, considered as normal
110 fGainAcceptCorr = 5 ; //Factor for gain deviation
116 fConTableDB = "Beamtest2002" ;
117 fConTableDBFile = "ConTableDB.root" ;
120 //____________________________________________________________________________
121 AliPHOSCalibrator::~AliPHOSCalibrator()
131 delete fhPedestalsWid ;
137 //____________________________________________________________________________
138 void AliPHOSCalibrator::AddRun(const char * filename)
140 //Adds one more run to list of runs, which will be scanned in ScanXXX methods
142 TObjString * fn = new TObjString(filename) ;
144 fRunList=new TList() ;
145 fRunList->SetOwner() ;
150 TIter next(fRunList) ;
152 while((r=(TObjString *)(next()))){
153 if(fn->String().CompareTo(r->String())==0){
154 AliError(Form("Run already in list: %s",filename)) ;
162 //____________________________________________________________________________
163 void AliPHOSCalibrator::Exec(Option_t * option)
165 // reads parameters and does the calibration
166 ScanPedestals(option);
167 CalculatePedestals();
173 //____________________________________________________________________________
174 void AliPHOSCalibrator::Init(void)
176 // intializes everything
178 //check if ConTableDB already read
180 SetConTableDB(fConTableDBFile) ;
183 fNch = fctdb->GetNchanels() ;
184 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
185 fhPedestalsWid= new TH1F("hPedestalsWid","Pedestals width",fNch,0.,fNch) ;
186 fhGains = new TH1F("hGains","Gains ",fNch,0.,fNch) ;
187 fhGainsWid = new TH1F("hGainsWid","Gains width",fNch,0.,fNch) ;
189 //____________________________________________________________________________
190 void AliPHOSCalibrator::SetConTableDB(const char * file,const char * name)
192 //Reads Connection Table database with name "name" from file "file"
194 if(file==0 || name == 0){
195 AliError(Form("Please, specify file with database and its title")) ;
198 if(fctdb && strcmp(fctdb->GetTitle(),name)==0) //already read
207 TFile * v = gROOT->GetFile(fConTableDBFile) ;
209 v = TFile::Open(fConTableDBFile) ;
211 AliError(Form("Can not open file with Connection Table DB: %s",fConTableDBFile.Data())) ;
214 fctdb = new AliPHOSConTableDB(*(dynamic_cast<AliPHOSConTableDB *>(v->Get("AliPHOSConTableDB")))) ;
218 //____________________________________________________________________________
219 void AliPHOSCalibrator::PlotPedestal(Int_t chanel)
221 //Plot histogram for a given channel, filled in Scan method
222 if(fPedHistos && fPedHistos->GetEntriesFast()){
223 static_cast<TH1F*>(fPedHistos->At(chanel))->Draw() ;
226 AliInfo(Form("Histograms not created yet! \n")) ;
229 //____________________________________________________________________________
230 void AliPHOSCalibrator::PlotPedestals(void)
232 // draws pedestals distribution
233 fhPedestals->Draw() ;
235 //____________________________________________________________________________
236 void AliPHOSCalibrator::PlotGain(Int_t chanel)
238 //Plot histogram for a given channel, filled in Scan method
239 if(fGainHistos && fGainHistos->GetEntriesFast()){
240 static_cast<TH1F*>(fGainHistos->At(chanel))->Draw() ;
243 AliInfo(Form("Histograms not created yet! \n")) ;
246 //____________________________________________________________________________
247 void AliPHOSCalibrator::PlotGains(void)
249 // draws gains distribution
252 //____________________________________________________________________________
253 void AliPHOSCalibrator::ScanPedestals(Option_t * option )
255 //scan all files in list fRunList and fill pedestal hisgrams
256 //option: "clear" - clear pedestal histograms filled up to now
257 // "deb" - plot file name currently processed
262 if(fPedHistos && strstr(option,"clear"))
263 fPedHistos->Delete() ;
265 fPedHistos = new TObjArray(fNch) ;
267 //Create histos for each channel, fills them and extracts mean values.
268 //First - prepare histos
270 for(ich=0;ich<fNch ;ich++){
271 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
275 TString name("Pedestal for channel ") ;
277 fPedHistos->AddAt(new TH1F(n,name,fNChan,0,fNChan),ich) ;
281 TIter next(fRunList) ;
283 while((file = static_cast<TObjString *>(next()))){
284 if(strstr(option,"deb"))
285 printf("Processing file %s \n ",file->String().Data()) ;
288 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
289 AliPHOSRawStream *rawStream = new AliPHOSRawStream(rawReader) ;
290 rawStream->SetConTableDB(fctdb) ;
291 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
293 //Scan all event in file
294 while(rawReader->NextEvent()){
295 //Is it PHYSICAL event
296 if(rawReader->GetType() == PHYSICS_EVENT){
298 if(rawStream->ReadDigits(digits)){
299 if(rawStream->IsPEDevent()){
300 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
301 AliPHOSDigit * digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
302 ich = fctdb->AbsId2Raw(digit->GetId());
304 Float_t amp = digit->GetAmp() ;
305 TH1F * hh = dynamic_cast<TH1F*>(fPedHistos->At(ich)) ;
313 if(strstr(option,"deb"))
314 AliInfo(Form(" found %d events \n ",nevents)) ;
320 //____________________________________________________________________________
321 void AliPHOSCalibrator::CalculatePedestals()
323 //Fit histograms, filled in ScanPedestals method with Gaussian
324 //find mean and width, check deviation from mean for each channel.
326 if(!fPedHistos || !fPedHistos->At(0)){
327 AliError(Form("You should run ScanPedestals first!")) ;
331 //Now fit results with Gauss
332 TF1 * gs = new TF1("gs","gaus",0.,10000.) ;
334 for(ich=0;ich<fNch ;ich++){
335 TH1F * h = static_cast<TH1F *>(fPedHistos->At(ich)) ;
336 Int_t max = h->GetMaximumBin() ;
337 Axis_t xmin = max/2. ;
338 Axis_t xmax = max*3/2 ;
339 gs->SetRange(xmin,xmax) ;
341 par[0] = h->GetBinContent(max) ;
344 gs->SetParameters(par[0],par[1],par[2]) ;
346 gs->GetParameters(par) ;
347 fhPedestals->SetBinContent(ich,par[1]) ;
348 fhPedestals->SetBinError(ich,par[2]) ;
349 fhPedestalsWid->Fill(ich,par[2]) ;
353 //now check reasonability of results
354 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
355 fhPedestals->Fit("p0","Q") ;
357 p0->GetParameters(&meanPed);
358 for(ich=0;ich<fNch ;ich++){
359 Float_t ped = fhPedestals->GetBinContent(ich) ;
360 if(ped < 0 || ped > meanPed+fAcceptCorr){
361 TString out("Pedestal of channel ") ;
365 out+= "it is too far from mean " ;
367 AliError(Form("PHOSCalibrator %s",out.Data())) ;
373 //____________________________________________________________________________
374 void AliPHOSCalibrator::ScanGains(Option_t * option)
376 //Scan all runs, listed in fRunList and fill histograms for all channels
377 //options: "clear" - clean histograms, filled up to now
378 // "deb" - print current file name
379 // "narrow" - scan only narrow beam events
383 if(fGainHistos && strstr(option,"clear"))
384 fGainHistos->Delete() ;
386 if(strstr(option,"deball"))
387 AliInfo(Form("creating array for %d channels \n",fNch)) ;
388 fGainHistos = new TObjArray(fNch) ;
391 //Create histos for each channel, fills them and extracts mean values.
392 //First - prepare histos
394 if(!fGainHistos->GetEntriesFast()){
396 for(ich=0;ich<fNch ;ich++){
399 TString name("Gains for channel ") ;
401 fGainHistos->AddAt(new TH1F(n,name,fNGainBins,0,fGainMax),ich) ;
402 // static_cast<TH1F*>(fGainHistos->At(ich))->Sumw2() ;
406 TIter next(fRunList) ;
408 while((file = static_cast<TObjString *>(next()))){
410 AliRawReaderDate *rawReader = new AliRawReaderDate(file->String().Data()) ;
411 AliPHOSRawStream *rawStream = new AliPHOSRawStream(rawReader) ;
412 rawStream->SetConTableDB(fctdb) ;
414 TClonesArray * digits = new TClonesArray("AliPHOSDigit",300) ;
416 //Scan all event in file
417 while(rawReader->NextEvent()){
418 //Is it PHYSICAL event
419 if(rawReader->GetType() == PHYSICS_EVENT){
420 if(rawStream->ReadDigits(digits)){
422 if(rawStream->IsNELevent() || rawStream->IsWELevent()){
424 AliPHOSDigit * digit ;
427 for(Int_t idigit = 0; idigit<digits->GetEntriesFast() ; idigit++){
428 digit = static_cast<AliPHOSDigit *>(digits->At(idigit) ) ;
429 if(digit->GetAmp() > max){
431 max = digit->GetAmp() ;
434 digit = static_cast<AliPHOSDigit *>(digits->At(imax) ) ;
435 Int_t ich = fctdb->AbsId2Raw(digit->GetId());
437 Float_t pedestal = fhPedestals->GetBinContent(ich) ;
438 const Float_t kshowerInCrystall = 0.9 ;
439 Float_t gain = fBeamEnergy*kshowerInCrystall/
440 (digit->GetAmp() - pedestal) ;
441 static_cast<TH1F*>(fGainHistos->At(ich))->Fill(gain) ;
450 if(strstr(option,"deb"))
451 AliInfo(Form(" found %d events \n",nevents)) ;
454 //____________________________________________________________________________
455 void AliPHOSCalibrator::CalculateGains(void)
459 if(!fGainHistos || !fGainHistos->GetEntriesFast()){
460 AliError(Form("You should run ScanGains first!")) ;
464 //Fit results with Landau
465 TF1 * gs = new TF1("gs","landau",0.,10000.) ;
467 for(ich=0;ich<fNch ;ich++){
468 TH1F * h = static_cast<TH1F *>(fGainHistos->At(ich)) ;
469 Int_t bmax = h->GetMaximumBin() ;
470 Axis_t center = h->GetBinCenter(bmax) ;
471 Axis_t xmin = center - 0.01 ;
472 Axis_t xmax = center + 0.02 ;
473 gs->SetRange(xmin,xmax) ;
475 par[0] = h->GetBinContent(bmax) ;
478 gs->SetParameters(par[0],par[1],par[2]) ;
480 gs->GetParameters(par) ;
481 fhGains->SetBinContent(ich,par[1]) ;
482 fhGains->SetBinError(ich,par[2]) ;
483 fhGainsWid->Fill(ich,par[2]) ;
487 //now check reasonability of results
488 TF1 * p0 = new TF1("p0","pol0",0.,fNch) ;
489 fhGains->Fit("p0","Q") ;
491 p0->GetParameters(&meanGain);
492 for(ich=0;ich<fNch ;ich++){
493 Float_t gain = fhGains->GetBinContent(ich) ;
494 if(gain < meanGain/fGainAcceptCorr || gain > meanGain*fGainAcceptCorr){
495 TString out("Gain of channel ") ;
499 out+= "it is too far from mean " ;
501 AliError(Form("PHOSCalibrator %s",out.Data())) ;
507 //____________________________________________________________________________
508 void AliPHOSCalibrator::ReadFromASCII(const char * filename){
509 // We read pedestals and gains from *.dat file with following format:
510 // 0 0 0 0 37.09 1972. // next nmodrows*nmodcols*ncryrows*ncrycols lines
511 // 0 0 0 1 28.53 2072. // contains <RR CC r c ped peak>
512 // 0 0 0 2 30.93 1938. //
513 // where module is an array of 8*8 crystals and RR and CC are module raw and column position
514 FILE * file = fopen(filename, "r");
516 Error("ReadFromASCII", "could not open file %s", filename);
519 if(!fctdb || !fhPedestals || !fhGains){
527 Int_t modRaw,modCol,raw,col;
530 while(fscanf(file,"%d %d %d %d %f %f",&modRaw,&modCol,&raw,&col,&ped,&pik)==6){
531 //Calculate plain crystal position:
532 Int_t rawPosition = (modRaw*8+raw)*fctdb->GetNColumns()+modCol*8+col ;
533 fhPedestals->SetBinContent(rawPosition,ped) ;
535 fhGains->SetBinContent(rawPosition,1./pik);
537 fhGains->SetBinContent(rawPosition,0.);
540 if(nread != fctdb->GetNColumns()*fctdb->GetNRaws()){
541 Error("ReadFromASCII","Read %d parameters instead of %d\n",nread,fctdb->GetNColumns()*fctdb->GetNRaws()) ;
545 //_____________________________________________________________________________
546 void AliPHOSCalibrator::WritePedestals(const char * version)
548 //Write calculated data to file using AliPHOSCalibrManager
549 //version and validitirange (begin-end) will be used to identify data
552 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
556 AliPHOSCalibrationData ped("Pedestals",version);
557 for(Int_t i=0; i<fNch;i++){
558 Int_t absid=fctdb->Raw2AbsId(i) ;
559 ped.SetData(absid,fhPedestals->GetBinContent(i)) ;
560 ped.SetDataCheck(absid,fhPedestalsWid->GetBinContent(i)) ;
563 // //evaluate validity range
565 // TIter next(fRunList) ;
566 // Int_t ibegin=99999;
568 // TObjString * file ;
569 // while((file=((TObjString*)next()))){
570 // TString s = file->GetString() ;
571 // TString ss = s(s.Last('_'),s.Last('.'));
573 // if(sscanf(ss.Data(),"%d",&tmp)){
580 // ped.SetValidityRange(ibegin,iend) ;
583 // ped.SetValidityRange(begin,end) ;
585 //check, may be Manager instance already configured?
586 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
588 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
589 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
591 cmngr->WriteData(ped) ;
593 //_____________________________________________________________________________
594 void AliPHOSCalibrator::ReadPedestals(const char * version)
596 //Read data from file using AliPHOSCalibrManager
597 //version and range will be used to choose proper data
599 AliPHOSCalibrationData ped("Pedestals",version);
600 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
602 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
603 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
605 cmngr->GetParameters(ped) ;
606 Int_t npeds=ped.NChannels() ;
607 fNch = fctdb->GetNchanels() ;
610 fhPedestals = new TH1F("hPedestals","Pedestals mean",fNch,0.,fNch) ;
611 for(Int_t i=0;i<npeds;i++){
612 Int_t raw =fctdb->AbsId2Raw(i) ;
614 fhPedestals->SetBinContent(raw-1,ped.Data(i)) ;
615 fhPedestals->SetBinError(raw-1,ped.DataCheck(i)) ;
619 //_____________________________________________________________________________
620 void AliPHOSCalibrator::ReadGains(const char * version)
622 //Read data from file using AliPHOSCalibrManager
623 //version and range will be used to choose proper data
625 AliPHOSCalibrationData gains("Gains",version);
626 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
628 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
629 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
631 cmngr->GetParameters(gains) ;
632 Int_t npeds=gains.NChannels() ;
633 fNch = fctdb->GetNchanels() ;
636 fhGains = new TH1F("hGainss","Gains mean",fNch,0.,fNch) ;
637 for(Int_t i=0;i<npeds;i++){
638 Int_t raw =fctdb->AbsId2Raw(i) ;
640 fhGains->SetBinContent(raw-1,gains.Data(i)) ;
641 fhGains->SetBinError(raw-1,gains.DataCheck(i)) ;
645 //_____________________________________________________________________________
646 void AliPHOSCalibrator::WriteGains(const char * version)
648 //Write gains through AliPHOSCalibrManager
649 //version and validity range(begin-end) are used to identify data
652 AliError(Form("\n Please, supply Connection Table DB (use SetConTableDB()) \n" )) ;
656 AliPHOSCalibrationData gains("Gains",version);
657 for(Int_t i=0; i<fNch;i++){
658 Int_t absid=fctdb->Raw2AbsId(i) ;
659 gains.SetData(absid,fhGains->GetBinContent(i)) ;
660 gains.SetDataCheck(absid,fhGainsWid->GetBinContent(i)) ;
663 // TIter next(fRunList) ;
664 // Int_t ibegin=99999;
666 // TObjString * file ;
667 // while((file=((TObjString*)next()))){
668 // TString s = file->GetString() ;
669 // TSubString ss = s(s.Last('_'),s.Last('.'));
671 // if(sscanf(ss.Data(),"%d",&tmp)){
678 // gains.SetValidityRange(ibegin,iend) ;
681 // gains.SetValidityRange(begin,end) ;
682 AliPHOSCalibrManager * cmngr = AliPHOSCalibrManager::GetInstance() ;
684 AliWarning(Form("Using database file 'PHOSBTCalibration.root'")) ;
685 cmngr = AliPHOSCalibrManager::GetInstance("PHOSBTCalibration.root") ;
687 cmngr->WriteData(gains) ;
689 //_____________________________________________________________________________
690 void AliPHOSCalibrator::Print()const
693 AliInfo(Form("--------------PHOS Calibrator-----------------\n")) ;
694 printf("Files to handle:\n") ;
695 TIter next(fRunList) ;
697 while((r=(TObjString *)(next())))
698 printf(" %s\n",r->GetName()) ;
700 printf("Name of ConTableDB:.....................%s\n",fConTableDB.Data()) ;
701 printf("File of ConTableDB:.....................%s\n",fConTableDBFile.Data() ) ;
702 printf("Maximal deviation from mean Gain (factor):.%f\n",fGainAcceptCorr) ;
703 printf("Maximal deviation of Pedestal from mean:...%f\n",fAcceptCorr) ;
704 printf("Range used in Gain histos:..............%f\n",fGainMax) ;
705 printf("Number of bins in Gain histos:..........%d\n",fNGainBins) ;
706 printf("Number of channels to calibrate:........%d\n",fNch) ;
707 printf("Number of bins in pedestal histos:......%d\n",fNChan) ;
708 printf("--------------------------------------------------\n") ;