1 /**************************************************************************
2 * Copyright(c) 2008-2010, 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 // Author: A. Mastroserio //
20 // This class is used in the detector algorithm framework //
21 // to process the data stored in special container files //
22 // (see AliITSOnlineSPDfo). The "good" set of DAC values //
24 ////////////////////////////////////////////////////////////
35 #include <THnSparse.h>
39 #include "AliITSOnlineSPDfoChipConfig.h"
40 #include "AliITSOnlineSPDfoChip.h"
41 #include "AliITSOnlineSPDfoInfo.h"
42 #include "AliITSOnlineSPDfo.h"
43 #include "AliITSOnlineSPDfoAnalyzer.h"
48 AliITSOnlineSPDfoAnalyzer::AliITSOnlineSPDfoAnalyzer(const TString fileName, Bool_t readFromGridFile) :
55 fHighOccupancyCheck(kTRUE)
59 for(Int_t iqual =0; iqual<kNqualityFlags; iqual++) fGeneralThresholds[iqual]=0;
61 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
62 for (UInt_t hs=0; hs<6; hs++) {
63 for(Int_t i =0; i< kNqualityFlags; i++) fNh[i][hs][chipNr]=NULL;
67 Init(readFromGridFile);
69 //-------------------------------------------------------------------
70 AliITSOnlineSPDfoAnalyzer::AliITSOnlineSPDfoAnalyzer(const AliITSOnlineSPDfoAnalyzer& foan) :
71 fFileName(foan.fFileName),
76 fFOHandler(foan.fFOHandler),
77 fHighOccupancyCheck(foan.fHighOccupancyCheck)
80 // copy constructor, only copies the filename and params (not the processed data
83 for(Int_t iqual =0; iqual<3; iqual++) fGeneralThresholds[iqual] =foan.fGeneralThresholds[iqual];
84 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
85 for (UInt_t hs=0; hs<6; hs++) {
86 for(Int_t i=0; i<kNqualityFlags;i++) fNh[i][hs][chipNr]=NULL;
92 //-------------------------------------------------------------------
93 AliITSOnlineSPDfoAnalyzer::~AliITSOnlineSPDfoAnalyzer()
99 for (UInt_t hs=0; hs<6; hs++) {
100 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
101 for(Int_t i=0; i<kNqualityFlags ; i++ ) if(fNh[i][hs][chipNr]!=NULL) delete fNh[i][hs][chipNr];
106 //-------------------------------------------------------------------
107 AliITSOnlineSPDfoAnalyzer& AliITSOnlineSPDfoAnalyzer::operator=(const AliITSOnlineSPDfoAnalyzer& foan)
109 // assignment operator, only copies the filename and params (not the processed data)
111 for (UInt_t hs=0; hs<6; hs++) {
112 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
113 for(Int_t i=0; i<kNqualityFlags ; i++ ) if(fNh[i][hs][chipNr]!=NULL) delete fNh[i][hs][chipNr];
117 fFileName=foan.fFileName;
118 fHighOccupancyCheck=foan.fHighOccupancyCheck;
123 //-------------------------------------------------------------------
124 void AliITSOnlineSPDfoAnalyzer::Init(Bool_t readFromGridFile)
127 // first checks type of container and then initializes container obj
129 if (!readFromGridFile) {
130 TFile* p0 = TFile::Open(fFileName.Data());
132 printf("no file open!");
136 fFOHandler = new AliITSOnlineSPDfo();
137 fFOHandler->SetFile(fFileName);
142 //-------------------------------------------------------------------
143 void AliITSOnlineSPDfoAnalyzer::SetGeneralThresholds(Float_t thre[3])
145 // here the settings for the 3 quality flags are defined
146 for(Int_t i=0; i< 3; i++) fGeneralThresholds[i]=thre[i];
148 //-------------------------------------------------------------------
149 void AliITSOnlineSPDfoAnalyzer::SetNdimensions()
152 // here the axis of the N-dim histograms are setted
155 printf("no fo object. Exiting...\n");
159 TArrayI array = fFOHandler->GetDACscanParams();
160 fNdims = array.GetSize()/3;
161 fNbins = new Int_t[fNdims];
162 fXmin = new Double_t[fNdims];
163 fXmax = new Double_t[fNdims];
164 for(Int_t i=0; i< fNdims; i++){
165 fXmin[i] = array.At(3*i)-0.25;
166 fXmax[i] = array.At(3*i+1)+0.25;
167 fNbins[i] = (Int_t)((fXmax[i]-fXmin[i])/0.5)+1;//to avoid Int->Double conversion problems when checking results.
170 //-------------------------------------------------------------------
171 void AliITSOnlineSPDfoAnalyzer::BuildTHnSparse(Int_t ihs, Int_t ichip)
174 // here the N-dim histogram is booked per chip in one HS
177 if(!fNdims) SetNdimensions();
179 for(Int_t iqual =0; iqual < 3; iqual++) fNh[iqual][ihs][ichip] = new THnSparseI(Form("h%i_HS%i_C%i",iqual,ihs,ichip), Form("h%i_HS%i_C%i",iqual,ihs,ichip), fNdims, fNbins, fXmin, fXmax);
181 //-------------------------------------------------------------------
182 Int_t AliITSOnlineSPDfoAnalyzer::Select(const AliITSOnlineSPDfoChip *chip) const
185 // Selects the DAC values if in the chip: the I configuration corresponds to
186 // 0% efficiency ( no FO response case). All the others shoud be within the
187 // predefined thresholds
189 if(!fFOHandler->GetFOscanInfo()) {
190 printf("no general information object in the file. Exiting...\n");
194 Double_t npulses = (fFOHandler->GetFOscanInfo())->GetNumTriggers();
197 Info("AliITSOnlineSPDfoAnalyzer::Select","no trigger pulses set. Exiting...");
201 TObjArray *array = chip->GetChipConfigInfo();
203 printf("No measurement array found in the chip!!\n");
211 Int_t processedconfigurations = chip->GetNumberOfChipConfigs();
216 for(Int_t isteps =0; isteps < processedconfigurations; isteps++){
218 Int_t matrixId = ((AliITSOnlineSPDfoChipConfig*)array->At(isteps))->GetChipConfigMatrixId();
219 counts = (Float_t)(((AliITSOnlineSPDfoChipConfig*)array->At(isteps))->GetChipConfigCounter());
220 if(matrixId==0 && counts > 0) return -1;
221 if(fHighOccupancyCheck && matrixId ==6) continue;
223 Float_t efficiency = counts/npulses;
226 Int_t response = IsSelected(efficiency);
228 if(quality < response) quality = response;
235 //-----------------------------------------------------
236 Int_t AliITSOnlineSPDfoAnalyzer::IsSelected(Float_t eff) const
239 // returns the quality of the selection
242 for(Int_t i=0; i<3; i++){
243 if(eff <= 1.+ fGeneralThresholds[i] && eff >= 1. - fGeneralThresholds[i] ) return i;
247 //----------------------------------------------------
248 void AliITSOnlineSPDfoAnalyzer::Process()
251 // The procedure is the following:
252 // - DAC 4-tuples are checked
253 // - if the 4-tuple survives the selection, the chip-related histograms are filled.
254 // (- Per each histogram the mean values of each axis are taken)
258 Warning("AliITSOnlineSPDfoAnalyzer::Process","no fo object. Exiting.. \n");
263 TIter iter((fFOHandler->GetFile())->GetListOfKeys());
264 while ((key = (TKey*)(iter.Next()))) {
265 TString classname = key->GetClassName();
266 if(classname.Contains("OnlineSPD")) break;
268 TObjArray *array = (TObjArray*)(fFOHandler->GetFile())->Get(key->GetName()); // array of chips corresponding to the DACS (size 1-60)
270 printf("no array found! Exiting...\n");
274 Double_t *dacvalues = fFOHandler->GetDACvaluesD(key->GetName(), GetFOHandler()->GetFOscanInfo()->GetNumDACindex());
276 for(Int_t i=0; i< array->GetSize(); i++){
277 AliITSOnlineSPDfoChip *chip = (AliITSOnlineSPDfoChip *)array->At(i);
280 Int_t hs = chip->GetActiveHS();
281 Int_t chipid = chip->GetChipId();
282 Int_t quality = Select(chip);
283 if(quality<0) continue;
284 if(!fNh[quality][hs][chipid]) BuildTHnSparse(hs,chipid);
285 fNh[quality][hs][chipid]->Fill(dacvalues);
288 if(dacvalues) delete [] dacvalues;
291 //---------------------------------------------
292 void AliITSOnlineSPDfoAnalyzer::WriteToFile(TString outputfile)
295 // The 4-dim histograms are stored into a file
298 TFile * f = TFile::Open(outputfile.Data(),"recreate");
299 for(Int_t ihs =0; ihs < 6; ihs++) {
300 for(Int_t ichip =0; ichip < 10; ichip++){
301 for(Int_t i=0; i<kNqualityFlags ; i++ ) {
302 if(fNh[i][ihs][ichip]) f->WriteObjectAny(fNh[i][ihs][ichip],"THnSparse",Form("h%i_hs%i_chip%i",i,ihs,ichip));
309 //---------------------------------------------
310 void AliITSOnlineSPDfoAnalyzer::CheckResults(TString filename, Int_t hs, Int_t ichip, Int_t iqual) const
313 //The chip related 4-dim histograms are produced and stored into eps files
316 TFile *f = TFile::Open(filename.Data());
318 Info("AliITSOnlineSPDfoAnalyzer::CehckResults","no file open!. Exiting...\n");
325 TIter iter(f->GetListOfKeys());
326 while((obj=iter.Next())){
327 TString name = obj->GetName();
328 hn=(THnSparse*)f->Get(name.Data());
329 if(name.Contains(Form("hs%i",hs)) && name.Contains(Form("chip%i",ichip)) && name.Contains(Form("h%i",iqual))) GetCanvases(hn,hs,ichip,iqual);
332 //-----------------------------------------------------------------------------------------------
333 void AliITSOnlineSPDfoAnalyzer::GetCanvases(const THnSparse *hn,Int_t hs, Int_t chip, Int_t iqual) const
336 // 1-Dim and 2 Dim Projections of 4-dim histograms are visualized in canvases per quality selection
340 if(!hn) {printf("no thnsparse...exiting!\n"); return;}
342 gStyle->SetPalette(1);
346 if( (fFOHandler->GetFOscanInfo())->GetDACindex(0) == 20 ) dacname[0] = "FOPOL";
347 else dacname[0] = "possible DAC-name/ DAC-register-number mismatch";
348 if( (fFOHandler->GetFOscanInfo())->GetDACindex(1) == 17 ) dacname[1] = "CONVPOL";
349 else dacname[1] = "possible DAC-name/ DAC-register-number mismatch";
350 if( (fFOHandler->GetFOscanInfo())->GetDACindex(2) == 16 ) dacname[2] = "COMPREF";
351 else dacname[2] = "possible DAC-name/ DAC-register-number mismatch";
352 if( (fFOHandler->GetFOscanInfo())->GetDACindex(3) == 39 ) dacname[3] = "pre_VTH";
353 else dacname[3] = "possible DAC-name/ DAC-register-number mismatch";
355 TString titles = Form("|eff-1| <= %f for CHIP %i in HS %i",fGeneralThresholds[iqual],hs,chip);
358 for(Int_t i=0; i<2; i++) {
359 c[i] = new TCanvas(Form("c%i",i+1),Form(" %i DIM plots. %s ",i+1,titles.Data()),1200,800);
364 for(Int_t idim =0; idim<2; idim++){
365 for(Int_t k=1; k<5; k++){
371 h1 = hn->Projection(k-1);
373 printf("no histogram!!...\n\n\n");
375 h1->SetXTitle(Form("DAC %i ( %s )",k-1,dacname[k-1].Data()));
376 h1->SetYTitle("entries (eff within thresholds)");
383 h2 = hn->Projection(k-1,k);
384 h2->SetXTitle(Form("DAC %i ( %s )",k,dacname[k].Data())); h2->SetYTitle(Form("DAC %i ( %s )",k-1,dacname[k-1].Data()));
385 h2->SetTitleOffset(2,"Y"); h2->SetTitleOffset(1.5,"X");
387 h2 = hn->Projection(0,3);
388 h2->SetXTitle(Form("DAC %i ( %s )",3,dacname[3].Data())); h2->SetYTitle(Form("DAC %i ( %s )",0, dacname[0].Data()));
389 h2->SetTitleOffset(2,"Y"); h2->SetTitleOffset(1.5,"X");
396 c[idim]->SaveAs(Form("c%iDIM_%i_%i_%i.eps",idim,iqual,hs,chip));
399 //-----------------------------------------------------
400 TArrayI AliITSOnlineSPDfoAnalyzer::ChooseDACValues(Int_t hs, Int_t chip) const
403 // here the "best" 4 dacs are chosen. The most present are taken.
404 // If the n-tuple does not correspond to a meaningful entry, the
405 // closest value to the mean point in the n-dimensional histogram
409 for(Int_t jj=0;jj<5;jj++)tmp[jj]=NULL;
410 if(fNdims > 5) printf("AliITSOnlineSPDfoAnalyzer::ChooseDACValues -> N. of dimensions are more than expected! Break! \n");
411 TArrayI dacs(fNdims+1);
413 for(Int_t i=0; i<fNdims+1; i++) dacs.AddAt(-1,i);
415 for(Int_t iqual =0; iqual < kNqualityFlags; iqual++){
416 if(!fNh[iqual][hs][chip]) continue;
417 if(fNh[iqual][hs][chip]->GetEntries()==0) continue;
418 for(Int_t idim =0; idim<fNdims; idim++){
419 if(dacs.At(idim)>=0) continue;
420 tmp[idim] = fNh[iqual][hs][chip]->Projection(idim);
421 dacs.AddAt((Int_t)tmp[idim]->GetBinLowEdge(tmp[idim]->GetMaximumBin()+1),idim);
423 if(fFOHandler->GetFOscanInfo()->GetDACindex(idim)==fFOHandler->kIdPreVTH && CorrectPreVTHChioce(tmp[idim],bin)) {
424 dacs.AddAt((Int_t)tmp[idim]->GetBinLowEdge(bin+1),idim);
426 dacs.AddAt(iqual,fNdims);
430 if(!IsExisting(dacs,hs,chip) && dacs.At(fNdims)>-1) {
431 TArrayI centraldacs = GetCentralDACS(dacs.At(fNdims),hs,chip,tmp);
436 //_____________________________________________________
437 Bool_t AliITSOnlineSPDfoAnalyzer::IsExisting(TArrayI dacs,Int_t hs, Int_t chip) const
440 // check the N-tuple corresponds to a real one
443 if(dacs.At(fNdims)<0) return kFALSE;
444 Bool_t isOk = kFALSE;
446 Int_t size = dacs.GetSize()-1;
447 Double_t *entry = new Double_t[size];
448 for(Int_t i=0; i<size; i++) entry[i] = dacs.At(i);
449 Int_t checkbin = fNh[dacs.At(dacs.GetSize()-1)][hs][chip]->GetBin(entry,kFALSE); // kFALSE does not allocate another bin
450 if(checkbin > -1) isOk = kTRUE;
454 //-----------------------------------------------------------
455 TArrayI AliITSOnlineSPDfoAnalyzer::GetCentralDACS(Int_t qualityflag, Int_t hs, Int_t chip, TH1D **hd) const
458 // This method gets the DAC values which are closest to the mean point in the N-dim histogram
459 // It is called when the most probable DAC set does not correspond to a real entry in the N-dim
463 TArrayI dacs(fNdims+1);
465 Double_t *mean=new Double_t[fNdims];
466 Int_t *goodbins=new Int_t[fNdims];
467 Double_t distance = 9999999;
468 for(Int_t i=0; i<fNdims ;i++){
469 mean[i]=hd[i]->GetMean();
474 Int_t *bins = new Int_t[fNdims];
475 Double_t *val=new Double_t[fNdims];
477 for(Int_t in=0; in< fNh[qualityflag][hs][chip]->GetNbins() ; in++){
479 fNh[qualityflag][hs][chip]->GetBinContent(in,bins);
481 for(Int_t j=0; j<fNdims; j++) {
482 val[j] = hd[j]->GetBinCenter(bins[j]);
483 r2+=TMath::Power(val[j]-mean[j],2);
488 fNh[qualityflag][hs][chip]->GetBinContent(in,goodbins);
493 for(Int_t k=0; k<fNdims; k++) {
494 dacs.AddAt((Int_t)(hd[k]->GetBinCenter(goodbins[k]) + 0.5*hd[k]->GetBinWidth(goodbins[k])),k);
497 dacs.AddAt(qualityflag,fNdims);
506 //-------------------------------------------------------
507 void AliITSOnlineSPDfoAnalyzer::ReadParamsFromLocation(const Char_t *dirName)
510 // opens file (default name) in dir dirName and reads parameters from it
511 // The file should be in database
514 TString paramsFileName = Form("./%s/focalib_params.txt",dirName);
517 paramsFile.open(paramsFileName, ifstream::in);
518 if (paramsFile.fail()) {
519 printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName.Data());
525 paramsFile >> paramN;
526 if (paramsFile.eof()) break;
527 paramsFile >> paramV;
528 SetParam(paramN,paramV);
529 if (paramsFile.eof()) break;
534 //---------------------------------------------------------
535 void AliITSOnlineSPDfoAnalyzer::SetParam(const Char_t *pname, const Char_t *pval)
538 // sets a single parameter when reading from a file in the database
541 TString name = pname;
545 if (name.CompareTo("fGeneralThresholds0")==0) {
546 fGeneralThresholds[0] = val.Atof();
548 else if (name.CompareTo("fGeneralThresholds1")==0) {
549 fGeneralThresholds[1] = val.Atof();
551 else if (name.CompareTo("fGeneralThresholds2")==0) {
552 fGeneralThresholds[2] = val.Atof();
555 Error("AliITSOnlineSPDscanAnalyzer::SetParam","Parameter %s in configuration file unknown.",name.Data());
558 //--------------------------------------------------------
559 Bool_t AliITSOnlineSPDfoAnalyzer::CorrectPreVTHChioce(const TH1D *h,Int_t &bin) const
562 // Checks if more maxima of the same height are present in the pre_VTH case
566 Int_t maxbin = (Int_t)h->GetMaximumBin();
567 Double_t maxentries = h->GetBinContent(maxbin);
572 for(Int_t i=0; i< h->GetNbinsX(); i++){
573 if(h->GetBinContent(i) == maxentries){
574 if(binindex <= i) binindex =i;