AliPhysics  75b74d3 (75b74d3)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
AliMTRChEffAnalysis.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
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  **************************************************************************/
15 
16 #include "AliMTRChEffAnalysis.h"
17 
18 // ROOT includes
19 #include <Riostream.h>
20 #include "TObjString.h"
21 #include "TObjArray.h"
22 #include "TList.h"
23 #include "TSystem.h"
24 #include "TROOT.h"
25 #include "TPRegexp.h"
26 #include "TRegexp.h"
27 #include "TFile.h"
28 #include "TDirectory.h"
29 #include "TTree.h"
30 #include "TKey.h"
31 #include "TArrayI.h"
32 #include "TArrayD.h"
33 #include "TGrid.h"
34 #include "TGridResult.h"
35 #include "TGraphAsymmErrors.h"
36 #include "TH1.h"
37 #include "TH2.h"
38 #include "TAxis.h"
39 #include "TF1.h"
40 #include "TMath.h"
41 #include "TCanvas.h"
42 #include "TLegend.h"
43 #include "TLine.h"
44 #include "TLatex.h"
45 #include "TFileMerger.h"
46 #include "TFitResultPtr.h"
47 
48 #include "AliLog.h"
49 #include "AliMergeableCollection.h"
50 #include "AliCounterCollection.h"
51 
52 #include "AliAnalysisMuonUtility.h"
53 #include "AliTrigChEffOutput.h"
54 
55 #include "AliCDBManager.h"
56 #include "AliCDBStorage.h"
57 #include "AliCDBId.h"
58 #include "AliCDBEntry.h"
59 #include "AliMUONCDB.h"
60 #include "AliMUONTriggerEfficiencyCells.h"
61 
63 ClassImp(AliMTRChEffAnalysis) // Class implementation in ROOT context
65 
66 using std::cout;
67 using std::endl;
68 using std::cin;
69 using std::ofstream;
70 using std::ifstream;
71 
72 //________________________________________________________________________
74  TObject(),
75  fOutputs(0x0),
76  fConditions(0x0),
77  fMergedOutputs(0x0)
78 {
80 
81 }
82 
83 //________________________________________________________________________
84 AliMTRChEffAnalysis::AliMTRChEffAnalysis ( const char *localFileList, const char* outputName ) :
85  TObject(),
86  fOutputs(0x0),
87  fConditions(0x0),
88  fMergedOutputs(0x0)
89 {
91  SetOutList(localFileList,outputName);
92 }
93 
94 //________________________________________________________________________
96 {
97  //
99  //
100  delete fOutputs;
101  delete fConditions;
102  delete fMergedOutputs;
103 }
104 
105 //________________________________________________________________________
106 Bool_t AliMTRChEffAnalysis::AddSystematicCondition ( const char* physSel, const char* trigClassName, const char* centrality, Int_t itrackSel, Int_t imatch, Int_t imethod )
107 {
109  return SetCondition(physSel, trigClassName, centrality, itrackSel, imatch, imethod, kFALSE);
110 }
111 
112 //________________________________________________________________________
113 Bool_t AliMTRChEffAnalysis::AddToList ( const char *filename, const char *outputName )
114 {
116  AliTrigChEffOutput* trigOut = new AliTrigChEffOutput(filename,outputName);
117  if ( ! trigOut->GetMergeableCollection() ) {
118  AliError(Form("Cannot find %s in %s\n",outputName,filename));
119  return kFALSE;
120  }
121 
122  TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
123 
124 // // Delete counter collection to save memory
125 // trigOut->RemoveFromList(trigOut->GetCounterCollection());
126  // Just keep counter and mergeable collections
127  TList* effHistoList = GetEffHistoList(trigOut,condition);
128  effHistoList->SetName(filename);
129  TString currRun = AliAnalysisMuonUtility::GetRunNumberAsString(filename);
130  effHistoList->SetUniqueID(currRun.Atoi());
131 
132  if ( ! fOutputs ) {
133  fOutputs = new TObjArray();
134  fOutputs->SetOwner();
135  fOutputs->SetName(outputName);
136  }
137  fOutputs->Add(effHistoList);
138  delete trigOut;
139  return kTRUE;
140 }
141 
142 //________________________________________________________________________
143 TArrayI AliMTRChEffAnalysis::BoardsInRPC ( Int_t irpc ) const
144 {
146 
147  // FIXME: ugly and hardcoded, but avoid loading the mapping
148 
149  TArrayI boards;
150  if ( irpc == 0 || irpc == 9 ) {
151  Int_t arr[] = {26,27,28,29,48,49,50,51,68,69,84,85,100,101,113};
152  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
153  boards.Set(nBoards,arr);
154  if ( irpc == 9 )
155  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
156  }
157  else if ( irpc == 1 || irpc == 8 ) {
158  Int_t arr[] = {9,10,11,30,31,32,33,52,53,54,55,70,71,86,87,102,103,114};
159  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
160  boards.Set(nBoards,arr);
161  if ( irpc == 8 )
162  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
163  }
164  else if ( irpc == 2 || irpc == 7 ) {
165  Int_t arr[] = {12,13,34,35,56,57,72,73,88,89,104,105,115};
166  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
167  boards.Set(nBoards,arr);
168  if ( irpc == 7 )
169  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
170  }
171  else if ( irpc == 3 || irpc == 6 ) {
172  Int_t arr[] = {14,15,36,37,58,59,74,75,90,91,106,107,116};
173  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
174  boards.Set(nBoards,arr);
175  if ( irpc == 6 )
176  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
177  }
178  else if ( irpc == 4 || irpc == 5 ) {
179  Int_t arr[] = {16,38,60,76,92,108,117};
180  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
181  boards.Set(nBoards,arr);
182  if ( irpc == 5 )
183  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
184  }
185  else if ( irpc == 17 || irpc == 10 ) {
186  Int_t arr[] = {6,7,8,22,23,24,25,44,45,46,47,66,67,82,83,98,99,112};
187  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
188  boards.Set(nBoards,arr);
189  if ( irpc == 10 )
190  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
191  }
192  else if ( irpc == 16 || irpc == 11 ) {
193  Int_t arr[] = {4,5,20,21,42,43,64,65,80,81,96,97,111};
194  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
195  boards.Set(nBoards,arr);
196  if ( irpc == 11 )
197  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
198  }
199  else if ( irpc == 15 || irpc == 12 ) {
200  Int_t arr[] = {2,3,18,19,40,41,62,63,78,79,94,95,110};
201  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
202  boards.Set(nBoards,arr);
203  if ( irpc == 12 )
204  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
205  }
206  else if ( irpc == 14 || irpc == 13 ) {
207  Int_t arr[] = {1,17,39,61,77,93,109};
208  Int_t nBoards = sizeof(arr)/sizeof(arr[0]);
209  boards.Set(nBoards,arr);
210  if ( irpc == 13 )
211  for ( Int_t iboard=0; iboard<nBoards; iboard++ ) boards[iboard] += 117;
212  }
213 
214  return boards;
215 }
216 
217 //________________________________________________________________________
218 Int_t AliMTRChEffAnalysis::CompareEfficiencies ( const char* sources, const char* titles, const char* opt, const char* canvasNameSuffix ) const
219 {
221  TString srcs(sources);
222  if ( srcs.Contains("raw://") ) {
223  AliError("The method assumes that the specified storage is a SpecificStorage. Hence, please replace raw:// with the actual path in alien, e.g.: alien://folder=/alice/data/<year>/OCDB");
224  return -2;
225  }
226  TObjArray* sourceList = srcs.Tokenize(",");
227  TObjArray effMapList;
228  effMapList.SetOwner();
229 
230  TString trigEffCDBdir = "MUON/Calib/TriggerEfficiency";
231 
232  TIter next(sourceList);
233  TObjString* src = 0x0;
234  while ( (src = static_cast<TObjString*>(next())) ) {
235  TString currSrc = src->String();
236  if ( currSrc.BeginsWith("alien") && ! gGrid ) TGrid::Connect("alien://");
237 
238  AliMUONTriggerEfficiencyCells* effMap = 0x0;
239  if ( currSrc.EndsWith(".root") ) effMap = new AliMUONTriggerEfficiencyCells(currSrc.Data());
240  else {
241  TObjArray* dirRun = currSrc.Tokenize("?");
242  TString cdbPath = dirRun->UncheckedAt(0)->GetName();
243  TString runNum = ( dirRun->GetEntriesFast() > 1 ) ? dirRun->UncheckedAt(1)->GetName() : "";
244  AliCDBManager* mgr = AliCDBManager::Instance();
245  if ( ! mgr->GetDefaultStorage() ) mgr->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
246  mgr->SetSpecificStorage(trigEffCDBdir.Data(),cdbPath.Data());
247  Int_t runNumber = 0;
248  if ( runNum.IsNull() ) {
249  cout << "Please enter run number: " << endl;
250  cin >> runNumber;
251  }
252  else runNumber = runNum.Atoi();
253  mgr->SetRun(runNumber);
254  AliCDBEntry* cdbEntry = mgr->Get(trigEffCDBdir.Data());
255 
256  // The CDB manager replace the current effMap with another in case two specific storage are provided
257  // To avoid double deletion, the object must be cloned
258  effMap = static_cast<AliMUONTriggerEfficiencyCells*>(cdbEntry->GetObject()->Clone());
259  }
260  if ( ! effMap ) continue;
261  effMapList.Add(effMap);
262  }
263 
264  return CompareEfficiencies(&effMapList, titles, opt, canvasNameSuffix);
265 }
266 
267 //________________________________________________________________________
268 Int_t AliMTRChEffAnalysis::CompareEfficiencies ( TObjArray* effMapList, const char* titles, const char* opt, const char* canvasNameSuffix ) const
269 {
271 
272  TString sTitles(titles);
273  TObjArray* titleList = sTitles.Tokenize(",");
274 
275  Int_t nDiffs = 0;
276 
277  Int_t nLists = effMapList->GetEntriesFast();
278 
279  TString sCanvasNameSuffix(canvasNameSuffix);
280  if ( ! sCanvasNameSuffix.IsNull() && ! sCanvasNameSuffix.BeginsWith("_") ) sCanvasNameSuffix.Prepend("_");
281 
282  Double_t xpt, ypt, xref, yref;
283  enum {kEff, kDiff, kPull};
284  TString sOpt(opt);
285  sOpt.ToLower();
286  Int_t iopt = kEff;
287  TString yTitle = "Eff.";
288  if ( sOpt.Contains("diff") ) {
289  iopt = kDiff;
290  yTitle = "Eff. - (ref.Eff)";
291  }
292  else if ( sOpt.Contains("pull") ) {
293  iopt = kPull;
294  yTitle = "(Eff - (ref.Eff)) / err";
295  }
296  else nDiffs = -1;
297 
298  Bool_t needsLegend = ( nLists > 1 );
299 // if ( iopt != kEff ) needsLegend = nLists > 2;
300 
301  Int_t colors[] = {kBlack, kRed, kSpring, kTeal, kBlue, kViolet, kMagenta, kOrange, kGray};
302  Int_t ncolors = sizeof(colors)/sizeof(colors[0]);
303 
304  Int_t hTypes[2] = {AliMUONTriggerEfficiencyCells::kHboardCount,AliMUONTriggerEfficiencyCells::kHslatCount};
305  Int_t countTypes[3] = {AliMUONTriggerEfficiencyCells::kBendingEff,AliMUONTriggerEfficiencyCells::kNonBendingEff,AliMUONTriggerEfficiencyCells::kBothPlanesEff};
306 
307  TString currName = "";
308  Int_t ican = 0;
309  for ( Int_t itype=0; itype<2; itype++ ) {
310  TString xTitle = ( hTypes[itype] == AliMUONTriggerEfficiencyCells::kHslatCount ) ? "RPC" : "Board";
311  for ( Int_t icount=0; icount<3; icount++ ) {
312  TCanvas* can = 0x0;
313  for ( Int_t ich=0; ich<4; ich++ ) {
314  TLegend* leg = 0x0;
315 // Int_t hrefIdx = AliTrigChEffOutput::kNcounts + AliTrigChEffOutput::kNcounts*4*(itype-1) + 4*AliTrigChEffOutput::kAllTracks + ich;
316 // Int_t hIdx = AliTrigChEffOutput::kNcounts + AliTrigChEffOutput::kNcounts*4*(itype-1) + 4*icount + ich;
317  TGraphAsymmErrors* refGraph = 0x0;
318  for ( Int_t ilist=0; ilist<nLists; ilist++ ) {
319  TString currTitle = titleList->At(ilist)->GetName();
320  AliMUONTriggerEfficiencyCells* effMap = static_cast<AliMUONTriggerEfficiencyCells*>(effMapList->UncheckedAt(ilist));
321  currName = effMap->GetHistoName(hTypes[itype],AliMUONTriggerEfficiencyCells::kAllTracks,ich);
322  TH1* histoDen = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
323  currName = effMap->GetHistoName(hTypes[itype],countTypes[icount],ich);
324  TH1* histoNum = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
325  TGraphAsymmErrors* graph = new TGraphAsymmErrors(histoNum,histoDen,"e0");
326  currName = histoNum->GetName();
327  currName.ReplaceAll("Count","Eff");
328  currName.Append(Form("_%s",currTitle.Data()));
329  graph->SetName(currName.Data());
330 
331  if ( iopt != kEff ) {
332  if ( refGraph ) {
333  for ( Int_t ipt=0; ipt<graph->GetN(); ipt++ ) {
334  refGraph->GetPoint(ipt,xref,yref);
335  graph->GetPoint(ipt,xpt,ypt);
336  Double_t diff = ypt - yref;
337  if ( TMath::Abs(diff) > 1.e-4 ) nDiffs++;
338  if ( iopt == kDiff ) graph->SetPoint(ipt,xpt,diff);
339  else if ( iopt == kPull ) {
340  Double_t err = GetError(graph->GetErrorYlow(ipt),graph->GetErrorYhigh(ipt));
341  Double_t pull = ( err > 0. ) ? diff/err : 0.;
342  graph->SetPoint(ipt,xpt,pull);
343  }
344  } // loop on points
345  }
346  else {
347  refGraph = graph;
348  continue;
349  }
350  }
351 
352  if ( ! can ) {
353  currName = graph->GetName();
354  currName.Remove(currName.Length()-currTitle.Length()-5);
355  currName += sCanvasNameSuffix;
356  can = new TCanvas(currName.Data(),currName.Data(),20*ican,20*ican,600,600);
357  can->Divide(2,2);
358  ican++;
359  }
360  if ( needsLegend && ! leg ) {
361  leg = new TLegend(0.35, 0.15, 0.65, 0.45);
362  if ( refGraph ) leg->SetHeader(Form("Ref.: %s",titleList->At(0)->GetName()));
363  }
364  can->cd(ich+1);
365  gPad->SetGridy();
366  Int_t icolor = ( ilist < ncolors ) ? colors[ilist] : 20+ilist;
367  graph->SetLineColor(icolor);
368  graph->SetMarkerColor(icolor);
369  graph->SetMarkerStyle(20+ilist);
370  graph->SetMarkerSize(0.5);
371  graph->GetXaxis()->SetTitle(xTitle.Data());
372  graph->GetYaxis()->SetTitle(yTitle.Data());
373  graph->Draw((gPad->GetListOfPrimitives()->GetEntries()==0)?"ap":"p");
374  if ( leg ) leg->AddEntry(graph,titleList->At(ilist)->GetName(),"lp");
375  } // loop on lists
376  if (leg ) leg->Draw();
377  delete refGraph;
378  } // loop on chambers
379  } // loop on counts
380  } // loop on types
381 
382  delete titleList;
383 
384  return nDiffs;
385 }
386 
387 //________________________________________________________________________
389 {
390  if ( ! HasMergedResults() ) return;
391 
392  TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
393  TString titles = "";
394 
395  TObjArray effMapList;
396  effMapList.SetOwner();
397 
398  AliTrigChEffOutput* trigOut = 0x0;
399  TIter next(fMergedOutputs);
400  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
401  TList* effList = GetEffHistoList(trigOut, condition);
402  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
403  effMapList.Add(effMap);
404  titles += Form("%s,",trigOut->GetName());
405  }
406  titles.Remove(TString::kTrailing,',');
407 
408  CompareEfficiencies(&effMapList, titles.Data(), opt, "MergedComp");
409 }
410 
411 
412 //________________________________________________________________________
413 //TArrayI AliMTRChEffAnalysis::CheckHomogeneity ( const TGraphAsymmErrors* trendGraph, Double_t nSigmas, Bool_t revert ) const
414 //{
415 // /// Check efficiency homogeneity
416 // TArrayI ranges(trendGraph->GetN());
417 // Double_t xpt=0., ypt=0.;
418 //
419 // Double_t sumEffOverSigma2 = -10.;
420 // Double_t sumInvSimga2 = 0.001;
421 // Int_t irange = 0;
422 // Int_t npt = trendGraph->GetN();
423 // for ( Int_t ipt=0; ipt<npt; ipt++ ) {
424 // Int_t currPt = ( revert ) ? npt-1-ipt : ipt;
425 // trendGraph->GetPoint(currPt,xpt,ypt);
426 // Double_t err = GetError(trendGraph->GetErrorYlow(currPt),trendGraph->GetErrorYhigh(currPt));
427 // Double_t invSigma2 = 1./(err*err);
428 // Double_t effOverSigma2 = ypt*invSigma2;
429 //
430 // Double_t meanEff = sumEffOverSigma2 / sumInvSimga2;
431 // Double_t diff = ypt - meanEff;
432 // Double_t diffErr = TMath::Sqrt(err*err+1./sumInvSimga2);
434 // if ( TMath::Abs(diff)/diffErr > nSigmas ) {
435 // TString run = trendGraph->GetXaxis()->GetBinLabel(currPt+1);
436 // ranges[irange++] = run.Atoi();
437 // sumEffOverSigma2 = 0.;
438 // sumInvSimga2 = 0.;
439 // }
440 // sumEffOverSigma2 += effOverSigma2;
441 // sumInvSimga2 += invSigma2;
442 // }
443 // TString run = trendGraph->GetXaxis()->GetBinLabel(trendGraph->GetN());
444 // ranges[irange++] = run.Atoi();
445 // ranges.Set(irange);
446 // return ranges;
447 //}
448 
449 //________________________________________________________________________
450 void AliMTRChEffAnalysis::CopyDir(TDirectory *source) const
451 {
453  TDirectory *savdir = gDirectory;
454  TDirectory *adir = savdir->mkdir(source->GetName());
455  adir->cd();
456  //loop on all entries of this directory
457  TKey *key;
458  TIter nextkey(source->GetListOfKeys());
459  while ((key = (TKey*)nextkey())) {
460  const char *classname = key->GetClassName();
461  TClass *cl = gROOT->GetClass(classname);
462  if (!cl) continue;
463  if (cl->InheritsFrom(TDirectory::Class())) {
464  source->cd(key->GetName());
465  TDirectory *subdir = gDirectory;
466  adir->cd();
467  CopyDir(subdir);
468  adir->cd();
469  } else if (cl->InheritsFrom(TTree::Class())) {
470  TTree *T = (TTree*)source->Get(key->GetName());
471  adir->cd();
472  TTree *newT = T->CloneTree(-1,"fast");
473  newT->Write();
474  } else {
475  source->cd();
476  TObject *obj = key->ReadObj();
477  adir->cd();
478  obj->Write(obj->GetName(),TObject::kSingleKey);
479  delete obj;
480  }
481  }
482  adir->SaveSelf(kTRUE);
483  savdir->cd();
484 }
485 
486 
487 //________________________________________________________________________
488 Bool_t AliMTRChEffAnalysis::CopyLocally ( const char* runList, const char* path, const char* pattern, const char* localFileList, const char* outDir, const char* directory ) const
489 {
491  TString sPattern(pattern);
492  TString sOutDir(outDir);
493  TString sPath(path);
494  Bool_t isGrid = (! sPattern.IsNull());
495 
496  if ( sOutDir.IsNull() ) {
497  if ( isGrid ) {
498  TString data = sPath(TRegexp("/data/"));
499  TString year = sPath(TRegexp("/20[0-9][0-9]/"));
500  TString period = sPath(TRegexp("/LHC[0-9][0-9][a-z]"));
501  period.Append("/");
502  // TPRegexp re("/(muon|pass).*?/");
503  TString pass = AliAnalysisMuonUtility::GetPassName(path);
504  if ( pass.IsNull() ) pass = AliAnalysisMuonUtility::GetPassName(pattern);
505  sOutDir = data+year+period+pass;
506  sOutDir.ReplaceAll("//","/");
507  sOutDir.Remove(TString::kTrailing,'/');
508  sOutDir.Remove(TString::kLeading,'/');
509  }
510  else sOutDir = sPath;
511  sOutDir.Remove(TString::kTrailing,'/');
512  sOutDir.Remove(TString::kLeading,'/');
513  }
514 
515  TGridResult* res = 0x0;
516  if ( isGrid ) {
517  if ( ! gGrid ) TGrid::Connect("alien://");
518  res = gGrid->Query(path,pattern);
519  }
520 
521  TList* rl = GetRunList(runList);
522 
523  ofstream outFile(localFileList);
524  Bool_t allOk = kTRUE;
525  Bool_t prompt = kTRUE;
526  Bool_t overwrite = kFALSE;
527  for ( Int_t irun=0; irun<rl->GetEntries(); irun++ ) {
528  TString run = static_cast<TObjString*>(rl->At(irun))->String();
529  TString dest = Form("%s/%09i/QAresults.root",sOutDir.Data(),run.Atoi());
530  TString destDir = gSystem->DirName(dest.Data());
531  if ( gSystem->AccessPathName(destDir.Data()) ) ExecCommand(Form("mkdir -p %s",destDir.Data()),kFALSE);
532  Bool_t copyFile = kTRUE;
533  Bool_t isFileOk = kTRUE;
534  if ( gSystem->AccessPathName(dest) == 0 ) {
535  if ( prompt ) {
536  TString decision = "";
537  cout << "Local file " << dest.Data() << " already exist: overwrite? [y/n/ya/na (a=use decision for all)]" << endl;
538  cin >> decision;
539  if ( decision.EndsWith("a") ) prompt = kFALSE;
540  if ( decision.BeginsWith("y") ) overwrite = kTRUE;
541  else overwrite = kFALSE;
542  }
543  copyFile = overwrite;
544  }
545  if ( copyFile ) {
546  TString src = "";
547  if ( isGrid ) {
548  for ( Int_t ifile=0; ifile<res->GetEntries(); ifile++ ) {
549  TString inFilename = res->GetKey(ifile,"turl");
550  if ( inFilename.Contains(run.Data()) ) {
551  src = inFilename;
552  break;
553  }
554  }
555  if ( src.IsNull() ) {
556  AliWarning(Form("Cannot find output for run %s",run.Data()));
557  isFileOk = kFALSE;
558  }
559  else {
560  TFile* outFile = TFile::Open(dest,"RECREATE");
561  TFile* inFile = TFile::Open(src);
562  inFile->cd();
563  TDirectory* dir = static_cast<TDirectory*>(inFile->Get(directory));
564  if ( dir ) {
565  outFile->cd();
566  CopyDir(dir);
567  }
568  else isFileOk = kFALSE;
569  delete inFile;
570  delete outFile;
571  }
572  }
573  else {
574  src = Form("http://aliqamu.web.cern.ch/aliqamu/%s",dest.Data());
575  isFileOk = TFile::Cp(src.Data(),dest.Data());
576  }
577  }
578 
579  if ( isFileOk ) outFile << gSystem->pwd() << "/" << dest.Data() << endl;
580  else allOk = kFALSE;
581  }
582  delete rl;
583  outFile.close();
584  return allOk;
585 }
586 
587 //________________________________________________________________________
588 void AliMTRChEffAnalysis::DrawEffTrend ( Int_t itype, Int_t irpc, Double_t maxNsigmaOutliers, Double_t minEff, Double_t maxEff ) const
589 {
591  TString baseNames[3] = {"Chamber","RPC","Board"};
592  TString base = baseNames[itype] + "Eff";
593  if ( itype == AliTrigChEffOutput::kHboardEff ) {
594  if ( irpc < 0 ) {
595  AliWarning("Please specify RPC");
596  return;
597  }
598  base += Form("InRPC%i",irpc);
599  }
600 
601  Int_t nColumns = 2;
602  Int_t nRows = 2;
603  Int_t width = 600;
604  Int_t height = 600;
605  Int_t nDetEl = 4;
606  Int_t nCh = 1;
607  if ( itype != AliTrigChEffOutput::kHchamberEff ) {
608  nColumns = 6;
609  nRows = 3;
610  width = 1200;
611  height = 800;
612  nDetEl = 18;
613  nCh = 4;
614  }
615  TArrayI boards = BoardsInRPC(irpc);
616  if ( itype == AliTrigChEffOutput::kHboardEff ) nDetEl = boards.GetSize();
617 
618  for ( Int_t ich=0; ich<nCh; ich++ ) {
619  TString canName = base;
620  if ( itype != AliTrigChEffOutput::kHchamberEff ) canName += Form("Ch%i",11+ich);
621  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),25*ich,25*ich,width,height);
622  can->Divide(nColumns,nRows,0,0);
623  // can->SetTopMargin(0.03);
624  // can->SetBottomMargin(0.1);
625  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
626  can->cd(idetelem+1);
627  gPad->SetTicks(1,1);
628  gPad->SetGridy();
629  Int_t detElemId = idetelem;
630  if ( itype == AliTrigChEffOutput::kHchamberEff ) detElemId = 11+idetelem;
631  else if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
632 
633  TString title = Form("%s %i",baseNames[itype].Data(),detElemId);
634  TLegend* leg = new TLegend(0.2,0.15,0.8,0.4);
635  leg->SetHeader(title.Data());
636  for ( Int_t icount=0; icount<2; icount++ ) {
637  TGraphAsymmErrors* gr = GetTrendEff(itype, icount, ich, detElemId);
638  gr->SetLineColor(icount+1);
639  gr->SetMarkerColor(icount+1);
640  gr->SetMarkerStyle(24+2*icount);
641  gr->GetYaxis()->SetRangeUser(minEff,maxEff);
642  // gr->GetYaxis()->SetLabelSize(0.025*nRows);
643  // gr->GetXaxis()->SetLabelSize(0.025*nColumns);
644  gr->SetTitle("");
645  gr->Draw(icount==0?"ap":"p");
646  TString legTitle = ( icount==0 ) ? "bending plane" : "non-bending plane";
647  leg->AddEntry(gr,legTitle.Data(),"lp");
648  if ( maxNsigmaOutliers > 0. ) {
649  TGraphAsymmErrors* outliers = GetOutliers(gr,maxNsigmaOutliers);
650  outliers->SetLineColor(6+icount);
651  outliers->SetMarkerColor(6+icount);
652  outliers->SetMarkerStyle(20+2*icount);
653  outliers->SetLineWidth(2);
654  outliers->Draw("p");
655  legTitle.ReplaceAll("plane","outliers");
656  leg->AddEntry(outliers,legTitle.Data(),"lp");
657  }
658  }
659  leg->Draw();
660  }
661  if ( itype == AliTrigChEffOutput::kHchamberEff ) break;
662  }
663 }
664 
665 //________________________________________________________________________
666 void AliMTRChEffAnalysis::DrawStatContribution ( Int_t itype, Int_t irpc, Double_t maxNsigmaOutliers, Double_t minY, Double_t maxY ) const
667 {
669  if ( itype == AliTrigChEffOutput::kHchamberEff ) {
670  AliWarning("This function is valid only for itype 1 and 2");
671  return;
672  }
673 
674  TString baseNames[3] = {"Chamber","RPC","Board"};
675  TString base = baseNames[itype] + "Stat";
676  if ( itype == AliTrigChEffOutput::kHboardEff ) {
677  if ( irpc < 0 ) {
678  AliWarning("Please specify RPC");
679  return;
680  }
681  base += Form("InRPC%i",irpc);
682  }
683 
684  Int_t nColumns = 6;
685  Int_t nRows = 3;
686  Int_t width = 1200;
687  Int_t height = 800;
688  Int_t nDetEl = 18;
689  Int_t nCh = 1;
690 
691  TArrayI boards = BoardsInRPC(irpc);
692  if ( itype == AliTrigChEffOutput::kHboardEff ) nDetEl = boards.GetSize();
693 
694  for ( Int_t ich=0; ich<nCh; ich++ ) {
695  TH1* histos[nDetEl];
696  TH1* sumHistos = 0x0;
697  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
698  Int_t detElemId = idetelem;
699  if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
700  histos[idetelem] = GetTrend(itype, AliTrigChEffOutput::kAllTracks, ich, detElemId);
701  histos[idetelem]->SetName(Form("%s_stat",histos[idetelem]->GetName()));
702  histos[idetelem]->SetStats(0);
703  if ( sumHistos ) sumHistos->Add(histos[idetelem]);
704  else sumHistos = static_cast<TH1*>(histos[idetelem]->Clone("sumHistos"));
705  }
706 
707  TString canName = base; //Form("%sCh%i",base.Data(),11+ich);
708  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),25*ich,25*ich,width,height);
709  can->Divide(nColumns,nRows,0,0);
710  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
711  can->cd(idetelem+1);
712  gPad->SetTicks(1,1);
713  gPad->SetGridy();
714  Int_t detElemId = idetelem;
715  if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
716  TString title = Form("%s %i",baseNames[itype].Data(),detElemId);
717  TLegend* leg = new TLegend(0.2,0.65,0.8,0.9);
718  leg->SetHeader(title.Data());
719  TGraphAsymmErrors* gr = new TGraphAsymmErrors(histos[idetelem],sumHistos,"e0");
720  gr->SetHistogram(static_cast<TH1F*>(histos[idetelem]));
721 
722  gr->SetMarkerStyle(24);
723  gr->SetMarkerSize(0.5);
724  gr->GetYaxis()->SetRangeUser(minY,maxY);
725  gr->GetYaxis()->SetTitle(Form("Tracks in %s / Sum of tracks of %ss in %s",baseNames[itype].Data(),baseNames[itype].Data(),baseNames[itype-1].Data()));
726  gr->SetTitle("");
727  gr->Draw("ap");
728  TString legTitle = "stat";
729 // leg->AddEntry(gr,legTitle.Data(),"lp");
730  if ( maxNsigmaOutliers > 0. ) {
731  TGraphAsymmErrors* outliers = GetOutliers(gr,maxNsigmaOutliers);
732  outliers->SetLineColor(6);
733  outliers->SetMarkerColor(6);
734  outliers->SetMarkerStyle(20);
735  outliers->SetLineWidth(2);
736  outliers->Draw("p");
737  legTitle = "outliers";
738  leg->AddEntry(outliers,legTitle.Data(),"lp");
739  }
740 // }
741  leg->Draw();
742  }
743  delete sumHistos;
744  }
745 }
746 
747 //________________________________________________________________________
748 Bool_t AliMTRChEffAnalysis::DrawSystematicEnvelope ( Bool_t perRPC ) const
749 {
751  if ( ! HasMergedResults() ) return kFALSE;
752 
753  Int_t colors[] = {kBlack, kRed, kSpring, kTeal, kBlue, kViolet, kMagenta, kOrange, kGray};
754  Int_t ncolors = sizeof(colors)/sizeof(colors[0]);
755 
756  Int_t itype = ( perRPC ) ? AliMUONTriggerEfficiencyCells::kHslatCount : AliMUONTriggerEfficiencyCells::kHboardCount;
757  Int_t countTypes[2] = {AliMUONTriggerEfficiencyCells::kBendingEff,AliMUONTriggerEfficiencyCells::kNonBendingEff};
758 
759  Int_t nConditions = fConditions->GetEntriesFast();
760 
761 
762  TObjArray* refCondition = 0x0;
763  TObjArray condTitle(nConditions);
764  condTitle.SetOwner();
765 
766  Double_t xpt, ypt, xref, yref;
767  TArrayD eff(4), effErr(4);
768 
769  AliTrigChEffOutput* trigOut = 0x0;
770  TIter next(fMergedOutputs);
771  Int_t imerged = -1;
772  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
773  imerged++;
774 
775  // Get meaningful short titles for each systematic
776  if ( imerged == 0 ) {
777  for ( Int_t icond=0; icond<nConditions; icond++ ) {
778  TObjArray* condition = static_cast<TObjArray*>(fConditions->UncheckedAt(icond));
779  TString title = "";
780  if ( icond == 0 ) {
781  refCondition = condition;
782  title = condition->GetName();
783  }
784  for ( Int_t ic=0; ic<condition->GetEntriesFast(); ic++ ) {
785  TString currCond = static_cast<TObjString*>(condition->UncheckedAt(ic))->String();
786  TString refCond = static_cast<TObjString*>(refCondition->UncheckedAt(ic))->String();
787  if ( currCond == refCond ) continue;
788  TString add = currCond;
789  if ( ic == 3 ) add = trigOut->GetHistoName(-1,-1,-1,currCond.Atoi(),-1,-1);
790  else if ( ic == 4 ) add = trigOut->GetHistoName(-1,-1,-1,-1,currCond.Atoi(),-1);
791  else if ( ic == 5 ) add = trigOut->GetHistoName(-1,-1,-1,-1,-1,currCond.Atoi());
792  title += Form("_%s",add.Data());
793  }
794  title.Remove(TString::kLeading,'_');
795  condTitle.AddAt(new TObjString(title),icond);
796  }
797  }
798 
799  TArrayI isEmpty(nConditions);
800 
801  // First get the needed graphs
802  Int_t nDE = 0;
803  TObjArray effGraphs[8];
804  for ( Int_t iplane=0; iplane<8; iplane++ ) {
805  effGraphs[iplane].SetOwner();
806  }
807  TString titles = "";
808  TObjArray effMapList;
809  effMapList.SetOwner();
810  for ( Int_t icond=0; icond<nConditions; icond++ ) {
811  TObjArray* condition = static_cast<TObjArray*>(fConditions->UncheckedAt(icond));
812 
813  TList* effList = GetEffHistoList(trigOut, condition);
814  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
815  effMapList.Add(effMap);
816  titles += Form("%s,",condTitle.At(icond)->GetName());
817 
818  for ( Int_t ich=0; ich<4; ich++ ) {
819  TString currName = effMap->GetHistoName(itype,AliMUONTriggerEfficiencyCells::kAllTracks,ich);
820  TH1* histoDen = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
821  for ( Int_t icount=0; icount<2; icount++ ) {
822  Int_t iplane = 4*icount+ich;
823 
824  if ( histoDen->GetEntries() > 0 ) {
825  currName = effMap->GetHistoName(itype,countTypes[icount],ich);
826  TH1* histoNum = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
827  TGraphAsymmErrors* gr = new TGraphAsymmErrors(histoNum,histoDen,"e0");
828  nDE = gr->GetN();
829  effGraphs[iplane].AddAtAndExpand(gr,icond);
830  }
831  else {
832  isEmpty[icond] = 1;
833  AliWarning(Form("No entries in count %i and ch %i for %s\n",icount,ich,condTitle.At(icond)->GetName()));
834  }
835  }
836  }
837  }
838 
839  titles.Remove(TString::kTrailing,',');
840  CompareEfficiencies(&effMapList, titles, "diff", trigOut->GetName());
841 
842  // Draw average dispersion per plane
843  TString canName = Form("EffSyst_%s",trigOut->GetName());
844  Int_t pos = 25*(imerged+1);
845  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),pos,pos,1200,800);
846  can->Divide(4,2,0,0);
847 
848  for ( Int_t icount=0; icount<2; icount++ ) {
849  for ( Int_t ich=0; ich<4; ich++ ) {
850  Int_t iplane = 4*icount+ich;
851  can->cd(iplane+1);
852  gPad->SetTicks(1,1);
853  gPad->SetLogy();
854  TLegend* leg = new TLegend(0.15,0.7,0.9,0.9);
855  leg->SetHeader(trigOut->GetHistoName(-1,icount,ich,-1,-1,-1));
856  TH1* sumHisto = 0x0;
857  for ( Int_t icond=1; icond<nConditions; icond++ ) {
858  if ( isEmpty[icond] == 1 ) continue;
859  TH1* histo = new TH1D(Form("syst_%s_%s_plane%i_ch%i",trigOut->GetName(),condTitle[icond]->GetName(),icount,11+ich),"",200,-0.1,0.1);
860  histo->GetXaxis()->SetTitle("Eff.-(ref.Eff.)");
861  histo->GetYaxis()->SetTitle("1/#sigma^{2}");
862 
863  TGraphAsymmErrors* gr = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(icond));
864  TGraphAsymmErrors* grRef = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(0));
865  for ( Int_t ipt=0; ipt<gr->GetN(); ipt++ ) {
866  gr->GetPoint(ipt,xpt,ypt);
867  Double_t err = GetError(gr->GetErrorYlow(ipt),gr->GetErrorYhigh(ipt));
868  Double_t invErr2 = ( err > 0. ) ? 1./(err*err) : 0.;
869  grRef->GetPoint(ipt,xref,yref);
870 
871  Double_t diff = ypt-yref;
872  histo->Fill(diff,invErr2);
873  }
874 
875  if ( ! sumHisto ) {
876  sumHisto = static_cast<TH1*>(histo->Clone(Form("syst_%s_plane%i_ch%i",trigOut->GetName(),icount,11+ich)));
877  sumHisto->SetLineColor(1);
878  sumHisto->Draw();
879  leg->AddEntry(sumHisto,"All systematics","l");
880  }
881  else sumHisto->Add(histo);
882  Int_t icolor = ( icond < ncolors ) ? colors[icond] : 20+icond;
883  histo->SetLineColor(icolor);
884  histo->Draw("same");
885  leg->AddEntry(histo,condTitle[icond]->GetName(),"l");
886  } // loop on conditions
887  leg->Draw();
888  } // loop on chambers
889  } // loop on counts
890 
891 
892  canName = Form("TriggerEff_3outOf4_syst_%s",trigOut->GetName());
893  pos += 50;
894  TCanvas* canSyst = new TCanvas(canName.Data(),canName.Data(),pos,pos,600,600);
895  canSyst->SetLogy();
896  TLegend* leg = new TLegend(0.15,0.7,0.9,0.4);
897 // leg->SetHeader(trigOut->GetHistoName(-1,icount,ich,-1,-1,-1));
898  TH1* histo[nConditions];
899  for ( Int_t icond=0; icond<nConditions; icond++ ) {
900  if ( isEmpty[icond] == 1 ) continue;
901  histo[icond] = new TH1D(Form("TriggerEff_syst_%s_%s",condTitle[icond]->GetName(),trigOut->GetName()),"Dispersion of trigger probability (3/4)",200,-0.1,0.1);
902  histo[icond]->GetXaxis()->SetTitle("Trig. prob. - (ref. trig. prob)");
903  histo[icond]->GetYaxis()->SetTitle("1/#sigma^{2}");
904  }
905 
906  for ( Int_t ipt=0; ipt<nDE; ipt++ ) {
907  Double_t refTrigProb = 0.; // refTrigProbErr = 0.;
908  for ( Int_t icond=0; icond<nConditions; icond++ ) {
909  if ( isEmpty[icond] == 1 ) continue;
910  Double_t trigProb = 1., trigProbErr2 = 0.;
911  for ( Int_t icount=0; icount<2; icount++ ) {
912  for ( Int_t ich=0; ich<4; ich++ ) {
913  Int_t iplane = 4*icount+ich;
914  TGraphAsymmErrors* gr = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(icond));
915  gr->GetPoint(ipt,xpt,ypt);
916  eff[ich] = ypt;
917  effErr[ich] = GetError(gr->GetErrorYlow(ipt),gr->GetErrorYhigh(ipt));
918  } // loop on chambers
919  Double_t effErr34 = 0.;
920  Double_t eff34 = GetThreeOfFour(eff,effErr,effErr34);
921  trigProb *= eff34;
922  trigProbErr2 += effErr34*effErr34;
923  } // loop on counts
924 
925  if ( icond == 0 ) {
926  refTrigProb = trigProb;
927 // refTrigProbErr = trigProbErr;
928  }
929  else {
930  Double_t invErr2 = ( trigProbErr2>0. ) ? 1./trigProbErr2 : 0.;
931  histo[icond]->Fill(trigProb-refTrigProb,invErr2);
932  }
933  } // loop on conditions
934  } // loop on points
935 
936  for ( Int_t icond=0; icond<nConditions; icond++ ) {
937  if ( isEmpty[icond] == 1 ) continue;
938  TString title = ( icond == 0 ) ? "All systematics" : condTitle[icond]->GetName();
939  Int_t icolor = ( icond < ncolors ) ? colors[icond] : 20+icond;
940  histo[icond]->SetLineColor(icolor);
941  histo[icond]->Draw((icond == 0)?"":"same");
942  leg->AddEntry(histo[icond],title.Data(),"l");
943  if ( icond>0 ) histo[0]->Add(histo[icond]);
944  }
945  leg->Draw();
946  } // loop on merged output
947  return kTRUE;
948 }
949 
950 //________________________________________________________________________
951 Bool_t AliMTRChEffAnalysis::ExecCommand ( TString command, Bool_t prompt ) const
952 {
954  TString decision = "y";
955 
956  if ( gROOT->IsBatch() ) prompt = kFALSE; // To run with crontab
957 
958  if ( prompt ) {
959  cout << command.Data() << " ? [y/n]" << endl;
960  cin >> decision;
961  }
962 
963  decision.ToLower();
964  if ( decision == "y" ) {
965  cout << "Executing: " << command.Data() << endl;
966  gSystem->Exec(command.Data());
967  return kTRUE;
968  }
969 
970  return kFALSE;
971 }
972 
973 //________________________________________________________________________
974 Double_t AliMTRChEffAnalysis::FitRangesFunc ( Double_t* x, Double_t* par )
975 {
982 
983  Double_t xx = x[0];
984  Double_t val = par[1];
985  Int_t nChanges = par[0];
986  Double_t matchDiff = 123456789.;
987  Int_t matchChange = -1;
988  for ( Int_t iknot=0; iknot<nChanges; iknot++ ) {
989  Int_t iparChange = 2*(iknot+1);
990  Double_t diff = xx - par[iparChange];
991  if ( diff >= 0. && diff < matchDiff ) {
992  matchDiff = diff;
993  matchChange = iparChange;
994  }
995  }
996  if ( matchChange >= 0 ) val = par[matchChange+1];
997 
998  return val;
999 }
1000 
1001 //________________________________________________________________________
1002 Double_t AliMTRChEffAnalysis::GetAverageStat ( Int_t firstRun, Int_t lastRun, Int_t itype,Bool_t excludePeriphericBoard ) const
1003 {
1004  TH1* statHisto = 0x0;
1005 
1006  UInt_t uFirstRun = (UInt_t)firstRun;
1007  UInt_t uLastRun = (UInt_t)lastRun;
1008 
1009  TIter next(fOutputs);
1010  TList* effHistoList = 0x0;
1011  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1012  UInt_t run = effHistoList->GetUniqueID();
1013  if ( run < uFirstRun || run > uLastRun ) continue;
1014  TH1* histo = GetHisto(effHistoList,itype,AliTrigChEffOutput::kAllTracks,0);
1015 // if ( ! histo ) continue;
1016  if ( statHisto ) statHisto->Add(histo);
1017  else statHisto = static_cast<TH1*>(histo->Clone("tmpStatHisto"));
1018 // delete histo;
1019  }
1020  if ( ! statHisto ) return 0.;
1021 
1022  Double_t statPerDE = 0.;
1023  Double_t nDe = 0.;
1024  if ( itype == AliTrigChEffOutput::kHboardEff && excludePeriphericBoard ) {
1025  Int_t excludeBoardsHalf[] = {1, 17, 39, 61, 77, 93,109,
1026  16, 38, 60, 76, 92, 108, 117,
1027  110, 111, 112, 113, 114, 115, 116};
1028  Int_t nExcludedHalf = sizeof(excludeBoardsHalf)/sizeof(excludeBoardsHalf[0]);
1029  for ( Int_t ibin=1; ibin<=statHisto->GetNbinsX(); ibin++ ) {
1030  Bool_t skip = kFALSE;
1031  for ( Int_t iexcl=0; iexcl<nExcludedHalf; iexcl++ ) {
1032  if ( ibin == excludeBoardsHalf[iexcl] || ibin == excludeBoardsHalf[iexcl]+117 ) {
1033  skip = kTRUE;
1034  break;
1035  }
1036  }
1037  if ( skip ) continue;
1038  statPerDE += statHisto->GetBinContent(ibin);
1039  nDe += 1.;
1040  }
1041  }
1042  else {
1043  statPerDE = statHisto->Integral();
1044  nDe = (Double_t)statHisto->GetNbinsX();
1045  }
1046  statPerDE = nDe > 0. ? statPerDE/nDe : -1.;
1047 
1048  delete statHisto;
1049  return statPerDE;
1050 }
1051 
1052 //________________________________________________________________________
1053 TList* AliMTRChEffAnalysis::GetEffHistoList ( AliTrigChEffOutput* trigOut, TObjArray* condition ) const
1054 {
1056  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1057  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1058  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1059 
1060  return trigOut->GetEffHistoList(condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName(),itrackSel,imatch,imethod);
1061 }
1062 
1063 //________________________________________________________________________
1064 Double_t AliMTRChEffAnalysis::GetError ( Double_t errLow, Double_t errHigh ) const
1065 {
1067  return TMath::Max(errLow,errHigh);
1068 }
1069 
1070 //________________________________________________________________________
1071 TH1* AliMTRChEffAnalysis::GetHisto ( TList* effHistoList, Int_t itype, Int_t icount, Int_t ichamber ) const
1072 {
1074  Int_t ihisto = ( itype == AliTrigChEffOutput::kHchamberEff ) ? icount : AliTrigChEffOutput::kNcounts + 4*AliTrigChEffOutput::kNcounts*(itype-1) + 4*icount + ichamber;
1075  return static_cast<TH1*>(effHistoList->At(ihisto));
1076 }
1077 
1078 //________________________________________________________________________
1079 TArrayI AliMTRChEffAnalysis::GetHomogeneusRanges ( Double_t chi2Cut, Int_t maxNRanges, Double_t minEffVariation, Bool_t perRPC, TArrayI* forcedChanges, Double_t minEff, Double_t maxEff )
1080 {
1082 
1083  Int_t nRuns = fOutputs->GetEntriesFast();
1084 
1085  TH1F* hRunChangeCount = new TH1F("runChangeCount","Number of RPCs changing efficiency per run",nRuns,-0.5,-0.5+(Double_t)nRuns);
1086  hRunChangeCount->SetXTitle("Run num.");
1087  hRunChangeCount->SetYTitle("Num. of RPCs with change in eff.");
1088  for ( Int_t irun=0; irun<nRuns; irun++ ) {
1089  hRunChangeCount->GetXaxis()->SetBinLabel(irun+1,Form("%i",GetRunNumber(irun)));
1090  }
1091 
1093 
1094  Int_t nCanvas = perRPC ? 4 : 18;
1095  TObjArray canList(nCanvas);
1096 
1097  for ( Int_t irpc=0; irpc<18; irpc++ ) {
1098  Int_t icount = AliTrigChEffOutput::kBothPlanesEff;
1099  TArrayI boards = BoardsInRPC(irpc);
1100  Int_t firstDetEl = perRPC ? irpc : 0;
1101  Int_t lastDetEl = perRPC ? irpc : boards.GetSize()-1;
1102 
1103  for ( Int_t ich=0; ich<4; ich++ ) {
1104  Int_t ican = perRPC ? ich : irpc;
1105  TCanvas* can = static_cast<TCanvas*>(canList.At(ican));
1106  if ( ! can ) {
1107  TString canName = perRPC ? Form("testRanges_ch%i",11+ich) : Form("testRanges_RPC%i",irpc);
1108  can = new TCanvas(canName.Data(),canName.Data(),10*ich,10*ich,1200,800);
1109  can->Divide(6,3,0,0);
1110  canList.AddAt(can,ican);
1111  }
1112 
1113  for ( Int_t idetel=firstDetEl; idetel<=lastDetEl; idetel++ ) {
1114  Int_t currDE = ( perRPC ) ? idetel : boards[idetel];
1115 // for ( Int_t icount=0; icount<2; icount++ ) {
1116  TGraphAsymmErrors* trendGraph = GetTrendEff(itype,icount,ich,currDE);
1117  TArrayI range = GetHomogeneusRanges(trendGraph,chi2Cut,maxNRanges,minEffVariation,forcedChanges, kTRUE);
1118  trendGraph->GetYaxis()->SetRangeUser(minEff,maxEff);
1119 
1120  can->cd(idetel+1);
1121  gPad->SetTicks(1,1);
1122  TString drawOpt = ( gPad->GetListOfPrimitives()->GetEntries() == 0 ) ? "ap" : "p";
1123 
1124  if ( ! perRPC ) {
1125  Int_t icolor = ich+1;
1126  trendGraph->SetLineColor(icolor);
1127  trendGraph->SetMarkerColor(icolor);
1128  trendGraph->SetMarkerStyle(24+ich);
1129  TF1* func = static_cast<TF1*>(trendGraph->GetListOfFunctions()->At(0));
1130  if ( func ) {
1131  func->SetLineWidth(2);
1132  func->SetLineColor(icolor);
1133  }
1134  }
1135  trendGraph->GetXaxis()->SetLabelSize(0.07);
1136 
1137  trendGraph->Draw(drawOpt.Data());
1138  for ( Int_t ichange=2; ichange<range.GetSize(); ichange++ ) {
1139  // Store only the run when the change applies
1140  if ( ichange%2 == 1 ) continue;
1141  Int_t runIdx = range[ichange];
1142 // if ( ichange != 0 ) {
1143  TLine* line = new TLine(runIdx,minEff,runIdx,maxEff);
1144  line->SetLineStyle(2);
1145  line->Draw("same");
1146  TLatex text;
1147  text.DrawLatex((Double_t)(runIdx-3),maxEff-0.1*(maxEff-minEff)*(Double_t)(ichange/2),Form("%i",GetRunNumber(runIdx)));
1148 // }
1149  hRunChangeCount->Fill(runIdx);
1150  if ( hRunChangeCount->GetBinContent(runIdx+1) == 1 ) {
1151  TString infoMsg = Form("Efficiency change in %i triggered by ch %i RPC %i",GetRunNumber(runIdx),11+ich,irpc);
1152  if ( ! perRPC ) infoMsg += Form(" Board %i",currDE);
1153  AliInfo(infoMsg.Data());
1154  }
1155  } // loop on change
1156  } // loop on detection element
1157 // }
1158  } // loop on chambers
1159  } // loop on RPC
1160 
1161  // Clusterize contiguous runs
1162  TArrayI runChangeClust(nRuns);
1163  Double_t sumWgtRun = 0.;
1164  Double_t sumWgt = 0;
1165  for ( Int_t irun=0; irun<=nRuns; irun++ ) {
1166  if ( irun == nRuns || hRunChangeCount->GetBinContent(irun+1) == 0 ) {
1167  if ( sumWgt > 0. ) {
1168  Int_t averageRun = TMath::Nint(sumWgtRun / sumWgt);
1169  AliDebug(2,Form("Average run: %i => %i\n",averageRun,GetRunNumber(averageRun)));
1170  runChangeClust[averageRun]++;
1171  sumWgtRun = 0.;
1172  sumWgt = 0.;
1173  }
1174  }
1175  if ( irun == nRuns ) break;
1176 
1177  AliDebug(2,Form("irun %i => %i: wgt %g",irun,GetRunNumber(irun),hRunChangeCount->GetBinContent(irun+1)));
1178 
1179 // Double_t wgt = (Double_t)runChangeCount[irun];
1180  Double_t wgt = hRunChangeCount->GetBinContent(irun+1);
1181  if ( forcedChanges ) {
1182  for ( Int_t ichange=0; ichange<forcedChanges->GetSize(); ichange++ ) {
1183  if ( GetRunNumber(irun) == forcedChanges->At(ichange) ) wgt *= 10.;
1184  }
1185  }
1186  sumWgtRun += wgt*(Double_t)irun;
1187  sumWgt += wgt;
1188  }
1189 
1190  TCanvas* summaryCan = new TCanvas("effChangeSummary","effChangeSummary",50,50,600,600);
1191  summaryCan->SetLogy();
1192  hRunChangeCount->GetXaxis()->LabelsOption("v");
1193  hRunChangeCount->Draw();
1194 
1195  Int_t ientry = 0;
1196  TArrayI runRanges(nRuns);
1197  runRanges[ientry++] = GetRunNumber(0);
1198  for ( Int_t irun=1; irun<nRuns; irun++ ) {
1199  if ( runChangeClust[irun] > 0 ) {
1200  runRanges[ientry++] = GetRunNumber(irun-1);
1201  runRanges[ientry++] = GetRunNumber(irun);
1202  }
1203  }
1204  runRanges[ientry++] = GetRunNumber(nRuns-1);
1205  runRanges.Set(ientry);
1206  return runRanges;
1207 }
1208 
1209 //________________________________________________________________________
1210 TArrayI AliMTRChEffAnalysis::GetHomogeneusRanges ( TGraphAsymmErrors* trendGraph, Double_t chi2Cut, Int_t maxNRanges, Double_t minEffVariation, TArrayI* forcedChanges, Bool_t returnIndex )
1211 {
1213 
1214  TArrayI runRanges;
1215  TF1* func = 0x0;
1216  TArrayD forcedChangesBin;
1217  Int_t nForced = 0;
1218  if ( forcedChanges ) {
1219  forcedChangesBin.Set(forcedChanges->GetSize());
1220  for ( Int_t ichange=0; ichange<forcedChanges->GetSize(); ichange++ ) {
1221  Int_t idx = GetIndexFromRun(forcedChanges->At(ichange));
1222  if ( idx >= 0 ) forcedChangesBin[nForced++] = (Double_t)idx;
1223  else AliWarning(Form("Cannot find run %i\n",forcedChanges->At(ichange)));
1224  }
1225  }
1226 
1227  Double_t minNormChi2 = 123456789.;
1228  Int_t minNormChi2Step = -1;
1229  TString fitOpt = "NQ0";
1230 // Double_t fakeVal = -999.;
1231 // TArrayD params(2*maxNRanges);
1232 // params.Reset(fakeVal);
1233 
1234  for ( Int_t istep=0; istep<maxNRanges; istep++ ) {
1235  Int_t nPars = 2*(istep+1);
1236  Double_t xMin = trendGraph->GetXaxis()->GetXmin();
1237  Double_t xMax = trendGraph->GetXaxis()->GetXmax();
1238  func = new TF1("rangeFunc",this,&AliMTRChEffAnalysis::FitRangesFunc,xMin,xMax,nPars,"AliMTRChEffAnalysis","FitRanges");
1239  func->FixParameter(0,istep);
1240  for ( Int_t ipar=1; ipar<nPars; ipar++ ) {
1241 // if ( ipar >= 2*istep ) {
1242 // if ( TMath::Abs(params[ipar]-fakeVal) < 0.01 ) {
1243 // if ( ipar%2 == 1 ) params[ipar] = 0.95;
1244 // else params[ipar] = (xMax-xMin) * (Double_t)(ipar/2)/((Double_t)(istep+1));
1245 // }
1246 // func->SetParameter(ipar,params[ipar]);
1247  Double_t val = ( ipar%2 == 1 ) ? 0.95 : (xMax-xMin) * (Double_t)(ipar/2)/((Double_t)(istep+1));
1248  func->SetParameter(ipar,val);
1249  if ( ipar%2 == 0 ) func->SetParLimits(ipar,xMin,xMax);
1250  }
1251 
1252  TFitResultPtr fitResult = trendGraph->Fit(func,fitOpt.Data());
1253  // If fit converges close to a point where a break is forced
1254  // fix parameters and redo the fit
1255  if ( forcedChanges ) {
1256  Bool_t hasFixedPars = kFALSE;
1257  for ( Int_t iforced=0; iforced<nForced; iforced++ ) {
1258  for ( Int_t jstep=0; jstep<istep; jstep++ ) {
1259  Int_t ipar = 2*(jstep+1);
1260  if ( TMath::Abs(forcedChangesBin[iforced]-func->GetParameter(ipar)) > 2. ) continue;
1261  func->FixParameter(ipar,forcedChangesBin[iforced]);
1262  hasFixedPars = kTRUE;
1263  }
1264  }
1265  if ( hasFixedPars ) fitResult = trendGraph->Fit(func,fitOpt.Data());
1266  }
1267 
1268 // for ( Int_t ipar=1; ipar<nPars; ipar++ ) {
1269 // params[ipar] = func->GetParameter(ipar);
1270 // }
1271 
1272  Double_t normChi2 = func->GetChisquare() / ((Double_t)func->GetNDF());
1273  if ( normChi2 < minNormChi2 ) {
1274  minNormChi2 = normChi2;
1275  minNormChi2Step = istep;
1276  }
1277 // if ( static_cast<int>(fitResult) == 0 && normChi2 < chi2Cut ) break;
1278  if ( normChi2 < chi2Cut ) break; // REMEMBER TO CHECK
1279  delete func;
1280  func = 0x0;
1281  }
1282 
1283  if ( func ) {
1284  trendGraph->GetListOfFunctions()->Add(func->Clone());
1285  Int_t nSteps = (Int_t)func->GetParameter(0);
1286 
1287  // The runs for which the efficiency changes could be unsorted
1288  // (when forced values are requires)
1289  // Copy the parameters in arrays to sort them
1290  Int_t nPoints = nSteps+1;
1291  TArrayD parRunIdx(nPoints);
1292  TArrayD parEff(nPoints);
1293  for ( Int_t ipar=0; ipar<func->GetNpar(); ipar++ ) {
1294  Int_t istep = ipar/2;
1295  if ( ipar%2 == 0 ) parRunIdx[istep] = func->GetParameter(ipar);
1296  else parEff[istep] = func->GetParameter(ipar);
1297  }
1298  parRunIdx[0] = 0.;
1299  TArrayI sortIdx(nPoints);
1300  TMath::Sort(nPoints,parRunIdx.GetArray(),sortIdx.GetArray(),kFALSE);
1301 
1302  runRanges.Set(2*nPoints);
1303  Int_t irun = 0;
1304  runRanges[irun++] = returnIndex ? 0 : GetRunNumber(0);
1305  for ( Int_t ipoint=1; ipoint<nPoints; ipoint++ ) {
1306  Double_t deltaEff = TMath::Abs(parEff[sortIdx[ipoint]]-parEff[sortIdx[ipoint-1]]);
1307 // if ( ipoint>=2 ) deltaEff = TMath::Max(deltaEff,TMath::Abs(parEff[sortIdx[ipoint]]-parEff[sortIdx[ipoint-2]]));
1308  if ( deltaEff < minEffVariation ) {
1309  AliWarning(Form("Efficiency variation for %s is %g => consider uniform",trendGraph->GetName(),deltaEff));
1310  continue;
1311  }
1312  Int_t runChangeIdx = TMath::Nint(parRunIdx[sortIdx[ipoint]]);
1313  AliDebug(1,Form("Change run: %s => %g => %i %i",trendGraph->GetName(),parRunIdx[sortIdx[ipoint]],runChangeIdx,GetRunNumber(runChangeIdx)));
1314  runRanges[irun++] = returnIndex ? runChangeIdx-1 : GetRunNumber(runChangeIdx-1);
1315  runRanges[irun++] = returnIndex ? runChangeIdx : GetRunNumber(runChangeIdx);
1316  }
1317  Int_t lastPt = trendGraph->GetN()-1;
1318  runRanges[irun++] = returnIndex ? lastPt : GetRunNumber(lastPt);
1319  runRanges.Set(irun);
1320  }
1321  else {
1322  AliWarning(Form("Fit did not converge for %s (minimum chi2 %g for step %i)",trendGraph->GetName(),minNormChi2,minNormChi2Step));
1323  }
1324  return runRanges;
1325 }
1326 
1327 //________________________________________________________________________
1328 TString AliMTRChEffAnalysis::GetIdentifier ( AliTrigChEffOutput* trigOut, TObjArray* condition, Int_t itype, Int_t icount, Int_t ichamber ) const
1329 {
1331 // AliTrigChEffOutput* trigOut = static_cast<AliTrigChEffOutput*>(fOutputs->At(0));
1332 // if ( ! trigOut ) {
1333 // AliWarning("No output object found!");
1334 // delete arr;
1335 // return "";
1336 // }
1337  TString identifier = Form("/%s/%s/%s/",condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName());
1338  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1339  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1340  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1341  TString objName = trigOut->GetHistoName(itype, icount, ichamber, itrackSel, imatch, imethod);
1342  identifier += objName;
1343  return identifier;
1344 }
1345 
1346 //________________________________________________________________________
1347 Int_t AliMTRChEffAnalysis::GetIndexFromRun ( UInt_t runNumber ) const
1348 {
1350  for ( Int_t ipt=0; ipt<fOutputs->GetEntriesFast(); ipt++ ) {
1351  UInt_t run = fOutputs->UncheckedAt(ipt)->GetUniqueID();
1352  if ( run == runNumber ) return ipt;
1353  }
1354  return -1;
1355 }
1356 
1357 //________________________________________________________________________
1358 TGraphAsymmErrors* AliMTRChEffAnalysis::GetOutliers ( TGraphAsymmErrors* graph, Double_t maxNsigmas ) const
1359 {
1361 // TGraphAsymmErrors* outliers = new TGraphAsymmErrors();
1362  TGraphAsymmErrors* outliers = new TGraphAsymmErrors(*graph);
1363  outliers->SetHistogram(static_cast<TH1F*>(graph->GetHistogram()->Clone(Form("%s_outliers",graph->GetHistogram()->GetName()))));
1364 // outliers->SetLineColor(graph->GetLineColor()+1);
1365  if ( graph->GetListOfFunctions()->GetEntries() == 0 ) graph->Fit("pol0","Q0");
1366  TF1* func = static_cast<TF1*>(graph->GetListOfFunctions()->At(0));
1367 
1368  Double_t xpt, ypt;
1369  Int_t nremoved = 0;
1370  for ( Int_t ipt=0; ipt<graph->GetN(); ipt++ ) {
1371  graph->GetPoint(ipt,xpt,ypt);
1372  Double_t diff = ypt - func->Eval(xpt);
1373  Double_t err = ( diff > 0. ) ? graph->GetErrorYlow(ipt) : graph->GetErrorYhigh(ipt);
1374  if ( err < 0. || TMath::Abs(diff)/err > maxNsigmas ) continue;
1375  outliers->RemovePoint(ipt-nremoved);
1376  nremoved++;
1377 // outliers->SetPoint(iopt,xpt,ypt);
1378  }
1379  return outliers;
1380 }
1381 
1382 //________________________________________________________________________
1383 Int_t AliMTRChEffAnalysis::GetRunNumber ( Int_t ipt ) const
1384 {
1386  if ( ipt < 0 || ipt >= fOutputs->GetEntriesFast() ) return -1;
1387  return fOutputs->UncheckedAt(ipt)->GetUniqueID();
1388 }
1389 
1390 //________________________________________________________________________
1391 TList* AliMTRChEffAnalysis::GetRunList ( const char* runList ) const
1392 {
1394  TList* rl = new TList;
1395  rl->SetOwner();
1396  TString sRunList = gSystem->ExpandPathName(runList);
1397  if ( gSystem->AccessPathName(sRunList) || sRunList.EndsWith(".root") ) {
1398  sRunList.ReplaceAll(","," ");
1399  if ( sRunList.IsDigit() ) {
1400  TObjArray* arr = sRunList.Tokenize(" ");
1401  for ( Int_t iarr=0; iarr<arr->GetEntries(); iarr++ ) {
1402  rl->Add(new TObjString(arr->At(iarr)->GetName()));
1403  }
1404  delete arr;
1405  }
1406  }
1407  else {
1408  ifstream inFile(sRunList.Data());
1409  TString currLine = "";
1410  while ( ! inFile.eof() ) {
1411  currLine.ReadLine(inFile);
1412  TString currRun = AliAnalysisMuonUtility::GetRunNumberAsString(currLine);
1413  if ( ! currRun.IsNull() ) rl->Add(new TObjString(currRun));
1414  }
1415  inFile.close();
1416  }
1417  rl->Sort();
1418  return rl;
1419 }
1420 
1421 //________________________________________________________________________
1422 TH1* AliMTRChEffAnalysis::GetSum ( AliTrigChEffOutput* trigOut, TObjArray* condition, Int_t itype, Int_t icount, Int_t ichamber ) const
1423 {
1425  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1426  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1427  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1428  TString objName = trigOut->GetHistoName(itype, icount, ichamber, itrackSel, imatch, imethod);
1429  return static_cast<TH1*>(trigOut->GetSum(condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName(),objName));
1430 }
1431 
1432 //________________________________________________________________________
1433 TH1* AliMTRChEffAnalysis::GetTrend ( Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem ) const
1434 {
1436  if ( itype == AliTrigChEffOutput::kHchamberEff ) {
1437  if ( idetelem < 0 && ichamber >=0 ) idetelem = 11+ichamber;
1438  }
1439  TH1* outHisto = 0x0;
1440 
1441  TIter next(fOutputs);
1442  TList* effHistoList = 0x0;
1443  Int_t ibin = 0;
1444  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1445  ibin++;
1446 // TString identifier = GetIdentifier(trigOut,condition,itype,icount,ichamber);
1447  if ( ! outHisto ) {
1448 // TString outName = identifier;
1449  TString outName = Form("histo_type%i_count%i_ch%i_",itype,icount,11+ichamber);
1450 // outName.ReplaceAll("/","_");
1451  outName += Form("%i_trend",idetelem);
1452  outHisto = new TH1D(outName.Data(),outName.Data(),fOutputs->GetEntriesFast(),0.,(Double_t)fOutputs->GetEntriesFast());
1453  outHisto->SetDirectory(0);
1454  outHisto->GetXaxis()->SetTitle("Run num.");
1455  }
1456  UInt_t run = effHistoList->GetUniqueID();
1457  outHisto->GetXaxis()->SetBinLabel(ibin,Form("%u",run));
1458  TH1* histo = GetHisto(effHistoList,itype,icount,ichamber);
1459  Int_t currBin = histo->GetXaxis()->FindBin(idetelem);
1460  outHisto->SetBinContent(ibin,histo->GetBinContent(currBin));
1461  outHisto->SetBinError(ibin,histo->GetBinError(currBin));
1462  }
1463  if ( outHisto ) outHisto->GetXaxis()->LabelsOption("v");
1464  return outHisto;
1465 }
1466 
1467 //________________________________________________________________________
1468 TGraphAsymmErrors* AliMTRChEffAnalysis::GetTrendEff ( Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem ) const
1469 {
1471  if ( icount == AliTrigChEffOutput::kAllTracks ) {
1472  AliWarning("Chose either bending plane, non-bending plane or both planes");
1473  return NULL;
1474  }
1475  TH1* histoNum = GetTrend(itype,icount,ichamber,idetelem);
1476  TH1* histoDen = GetTrend(itype,AliTrigChEffOutput::kAllTracks,ichamber,idetelem);
1477  TGraphAsymmErrors* graph = new TGraphAsymmErrors(histoNum,histoDen,"e0");
1478  histoNum->Reset();
1479  histoNum->SetStats(kFALSE);
1480  graph->SetHistogram(static_cast<TH1F*>(histoNum));
1481  graph->GetYaxis()->SetTitle("Efficiency");
1482  graph->SetMarkerSize(0.5);
1483 // for ( Int_t ibin=1; ibin<=histoNum->GetXaxis()->GetNbins(); ibin++ ) {
1484 // graph->GetXaxis()->SetBinLabel(ibin,histoNum->GetXaxis()->GetBinLabel(ibin));
1485 // }
1486 // delete histoNum;
1487  delete histoDen;
1488  return graph;
1489 }
1490 
1491 //________________________________________________________________________
1492 Double_t AliMTRChEffAnalysis::GetThreeOfFour ( TArrayD eff, TArrayD effErr, Double_t &probErr ) const
1493 {
1495  Double_t binomialEff = 0.;
1496  Double_t sumErr2 = 0.;
1497  for ( Int_t jch=-1; jch<4; jch++ ) {
1498  Double_t prodEff = 1.;
1499  Double_t prodErr2 = 0.;
1500  for ( Int_t ich=0; ich<4; ich++ ) {
1501  Double_t currEff = ( ich == jch ) ? 1.-eff[ich] : eff[ich];
1502  prodEff *= currEff;
1503  Double_t relErr = ( currEff>0. ) ? effErr[ich]/currEff : 0.;
1504  prodErr2 += relErr*relErr;
1505  }
1506  binomialEff += prodEff;
1507  sumErr2 += prodEff*prodEff*prodErr2;
1508  }
1509  probErr = TMath::Sqrt(sumErr2);
1510  return binomialEff;
1511 }
1512 
1513 //________________________________________________________________________
1515 {
1517  if ( ! fMergedOutputs || fMergedOutputs->IsEmpty() ) {
1518  AliError("You first need to merge efficiency objects with MergeOutput");
1519  return kFALSE;
1520  }
1521  return kTRUE;
1522 }
1523 
1524 //________________________________________________________________________
1525 Bool_t AliMTRChEffAnalysis::MergeOutput ( TArrayI runRanges, Double_t averageStatError, Bool_t isIndex )
1526 {
1528 
1529  if ( runRanges.GetSize()%2 == 1 ) {
1530  AliError("Run ranges expected in the form: start_period1,end_period1,start_period2,end_period2... => even number expected");
1531  return kFALSE;
1532  }
1533 
1534  TArrayI mergedRanges = MergeRangesForStat(runRanges, averageStatError);
1535 
1536  Int_t nRanges = mergedRanges.GetSize()/2;
1537 
1538 
1539  if ( fMergedOutputs ) fMergedOutputs->Delete();
1540  else {
1541  fMergedOutputs = new TObjArray(nRanges);
1542  fMergedOutputs->SetOwner();
1543  }
1544 
1545  TList* effHistoList = 0x0;
1546  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1547  Int_t firstRun = mergedRanges[2*irange];
1548  Int_t lastRun = mergedRanges[2*irange+1];
1549  if ( isIndex ) {
1550  firstRun = GetRunNumber(firstRun);
1551  lastRun = GetRunNumber(lastRun);
1552  }
1553  UInt_t uFirstRun = (UInt_t)firstRun;
1554  UInt_t uLastRun = (UInt_t)lastRun;
1555 
1556  TString filename = "";
1557 
1558  TIter next(fOutputs);
1559  TFileMerger fileMerger;
1560  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1561  UInt_t run = effHistoList->GetUniqueID();
1562  if ( run < uFirstRun || run > uLastRun ) continue;
1563  filename = effHistoList->GetName();
1564  if ( firstRun == lastRun ) continue;
1565  fileMerger.AddFile(filename.Data(),kFALSE);
1566  }
1567 
1568  TString sRange = Form("%i_%i",firstRun,lastRun);
1569  TString mergedFilename = Form("mergedTrigEff_runs_%s.root",sRange.Data());
1570 
1571  if ( firstRun == lastRun ) TFile::Cp(filename.Data(),mergedFilename.Data(),kFALSE);
1572  else {
1573  fileMerger.OutputFile(mergedFilename.Data());
1574  fileMerger.Merge();
1575  }
1576  AliTrigChEffOutput* trigOut = new AliTrigChEffOutput(mergedFilename);
1577  trigOut->SetName(sRange.Data());
1578  fMergedOutputs->Add(trigOut);
1579  }
1580  return kTRUE;
1581 }
1582 
1583 //________________________________________________________________________
1584 TArrayI AliMTRChEffAnalysis::MergeRangesForStat ( TArrayI runRanges, Double_t averageStatError, Bool_t excludePeriphericBoards ) const
1585 {
1586  if ( averageStatError <= 0. || averageStatError >= 1. ) return runRanges;
1587 
1588  // FIXME: statstical error for binomial depends on the value of epsilon
1589  // for the moment let's assume an average efficiency of 0.9 (underestimated)
1590  // in the future one could maybe actually calculate it
1591  // (still we're working with average values...so the precision is limited)
1592  Double_t effForStatCalc = 0.9;
1593 
1594  Double_t averageStatNeeded = effForStatCalc*(1.-effForStatCalc)/(averageStatError*averageStatError);
1595 
1596  AliInfo(Form("Average statistics needed to reach precision of %g : %g",averageStatError,averageStatNeeded));
1597 
1598  Int_t nRanges = runRanges.GetSize()/2;
1599 
1600  TArrayD averageStat(nRanges);
1601  Double_t fullStat = 0.;
1602  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1603  averageStat[irange] = GetAverageStat(runRanges[2*irange],runRanges[2*irange+1],AliTrigChEffOutput::kHboardEff,excludePeriphericBoards);
1604  fullStat += averageStat[irange];
1605  }
1606 
1607  TArrayI mergedRanges(runRanges.GetSize());
1608  Int_t imerged = 0;
1609  mergedRanges[imerged++] = runRanges[0];
1610  Double_t mergedAverageStat = 0., remainingStat = fullStat;
1611  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1612  Int_t istart = 2*irange;
1613  Int_t iend = istart+1;
1614  mergedAverageStat += averageStat[irange];
1615  remainingStat -= averageStat[irange];
1616 
1617  AliInfo(Form("%i - %i => stat %g",runRanges[2*irange],runRanges[2*irange+1],averageStat[irange]));
1618 
1619  if ( ( mergedAverageStat >= averageStatNeeded && remainingStat >= averageStatNeeded ) || iend == runRanges.GetSize()-1 ) {
1620  mergedRanges[imerged++] = runRanges[iend];
1621  AliInfo(Form(" merged range %i - %i => stat %g",mergedRanges[imerged-2],mergedRanges[imerged-1],mergedAverageStat));
1622  mergedAverageStat = 0.;
1623  Int_t nextRun = iend+1;
1624  if ( nextRun < runRanges.GetSize() ) mergedRanges[imerged++] = runRanges[nextRun];
1625  }
1626  }
1627  mergedRanges.Set(imerged);
1628  return mergedRanges;
1629 }
1630 
1631 //________________________________________________________________________
1632 Bool_t AliMTRChEffAnalysis::SetCondition ( const char* physSel, const char* trigClassName, const char* centrality, Int_t itrackSel, Int_t imatch, Int_t imethod, Bool_t isBasic )
1633 {
1635  if ( ! fConditions ) {
1636  fConditions = new TObjArray();
1637  fConditions->SetOwner();
1638  }
1639 
1640  TString sCondition = Form("%s;%s;%s;%i;%i;%i",physSel,trigClassName,centrality,itrackSel,imatch,imethod);
1641  TObjArray* foundCondition = static_cast<TObjArray*>(fConditions->FindObject(sCondition.Data()));
1642  TObjArray* basicCondition = static_cast<TObjArray*>(fConditions->At(0));
1643 
1644  TObjArray* addCondition = 0x0;
1645 
1646  if ( foundCondition ) {
1647  if ( isBasic ) {
1648  if ( foundCondition == basicCondition ) return kFALSE;
1649  else {
1650  fConditions->Remove(foundCondition);
1651  addCondition = foundCondition;
1652  }
1653  }
1654  else {
1655  AliInfo("Systematic condition already added");
1656  return kFALSE;
1657  }
1658  }
1659  else {
1660  addCondition = sCondition.Tokenize(";");
1661  addCondition->SetName(sCondition.Data());
1662  }
1663 
1664 
1665  if ( isBasic ) {
1666  if ( basicCondition ) {
1667  AliInfo(Form("Changing current eff. condition: %s\n",basicCondition->GetName()));
1668  fConditions->Remove(basicCondition);
1669  delete basicCondition;
1670  }
1671  fConditions->AddAt(addCondition,0);
1672  fConditions->Compress();
1673 
1674  // Update outputs
1675  if ( fOutputs ) {
1676  TObjArray* tmpOutputs = static_cast<TObjArray*>(fOutputs->Clone());
1677  fOutputs->Delete();
1678  TIter next(tmpOutputs);
1679  TList* effHistiList = 0x0;
1680  while ( (effHistiList = static_cast<TList*>(next())) ) {
1681  AddToList(effHistiList->GetName(), fOutputs->GetName());
1682  }
1683  delete tmpOutputs;
1684  }
1685  }
1686  else fConditions->Add(addCondition);
1687 
1688  return kTRUE;
1689 }
1690 
1691 //________________________________________________________________________
1693 {
1696 }
1697 
1698 
1699 //________________________________________________________________________
1700 Bool_t AliMTRChEffAnalysis::SetEffConditions ( const char* physSel, const char* trigClassName, const char* centrality, Int_t itrackSel, Int_t imatch, Int_t imethod )
1701 {
1703  return SetCondition(physSel, trigClassName, centrality, itrackSel, imatch, imethod, kTRUE);
1704 }
1705 
1706 //________________________________________________________________________
1707 Bool_t AliMTRChEffAnalysis::SetOutList ( const char *localFileList, const char *outputName )
1708 {
1710 
1712 
1713  TString filename(localFileList);
1714  gSystem->ExpandPathName(filename);
1715  if ( gSystem->AccessPathName(filename.Data()) ) {
1716  AliWarning(Form("Cannot find %s",filename.Data()));
1717  return kFALSE;
1718  }
1719  if ( filename.EndsWith(".root") ) return AddToList(filename.Data(),outputName);
1720 
1721  Bool_t isOk = kTRUE;
1722  ifstream inFile(filename.Data());
1723  TString currLine = "";
1724  while ( ! inFile.eof() ) {
1725  currLine.ReadLine(inFile);
1726  if ( currLine.IsNull() ) continue;
1727  if ( gSystem->AccessPathName(currLine.Data()) ) continue;
1728  if ( ! AddToList(currLine.Data(), outputName) ) isOk = kFALSE;
1729  }
1730  inFile.close();
1731 
1732  return isOk;
1733 }
1734 
1735 //________________________________________________________________________
1736 Bool_t AliMTRChEffAnalysis::SetResultsFromGrid ( const char *runList, const char *path, const char *pattern, const char* localFileList, const char* outDir, const char *directory, const char* outputName )
1737 {
1739  CopyLocally(runList,path,pattern,localFileList,outDir,directory);
1740  return SetOutList(localFileList,outputName);
1741 }
1742 
1743 //________________________________________________________________________
1744 Bool_t AliMTRChEffAnalysis::SetResultsFromWeb ( const char *runList, const char *path, const char* localFileList, const char* outDir, const char *directory, const char* outputName )
1745 {
1747  CopyLocally(runList,path,"",localFileList,outDir,directory);
1748  return SetOutList(localFileList,outputName);
1749 }
1750 
1751 //________________________________________________________________________
1752 Bool_t AliMTRChEffAnalysis::WriteMergedToOCDB ( const char* outputCDB ) const
1753 {
1755  if ( ! HasMergedResults() ) return kFALSE;
1756  TString outCDBdir(outputCDB);
1757  if ( outCDBdir.Contains("://") && ! outCDBdir.BeginsWith("local") ) {
1758  AliError("A local directory is required to store the information");
1759  return kFALSE;
1760  }
1761  outCDBdir.ReplaceAll("local://","");
1762  if ( gSystem->AccessPathName(outCDBdir.Data()) ) {
1763  if ( ! ExecCommand(Form("mkdir -p %s",outCDBdir.Data()), kTRUE) ) {
1764  AliError(Form("Failed to create directory %s",outCDBdir.Data()));
1765  return kFALSE;
1766  }
1767  }
1768  TString outCDB = outCDBdir;
1769  outCDB.Prepend("local://");
1770 
1771  AliCDBManager* mgr = AliCDBManager::Instance();
1772  if ( ! mgr->GetDefaultStorage() ) mgr->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
1773 
1774  TString trigEffCDBdir = "MUON/Calib/TriggerEfficiency";
1775 
1776  mgr->SetSpecificStorage(trigEffCDBdir.Data(),outCDB.Data());
1777 
1778  AliCDBStorage* specificStorage = mgr->GetSpecificStorage(trigEffCDBdir.Data());
1779 
1780  TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
1781 
1782  AliTrigChEffOutput* trigOut = 0x0;
1783  TIter next(fMergedOutputs);
1784  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
1785  TList* effList = GetEffHistoList(trigOut,condition);
1786  TString runRange = trigOut->GetName();
1787 
1788 // // Write full merged output in file
1789 // TFile* file = TFile::Open(Form("mergedTrigEff_runs_%s.root",runRange.Data()),"RECREATE");
1790 // effList->Write("triggerChamberEff",TObject::kSingleKey);
1791 // trigOut->GetOutput()->Write(trigOut->GetOutput()->GetName(),TObject::kSingleKey);
1792 // file->Close();
1793 // delete file;
1794 
1795 
1796  // Write OCDB object
1797  TObjArray* currRange = runRange.Tokenize("_");
1798  Int_t firstRun = static_cast<TObjString*>(currRange->UncheckedAt(0))->String().Atoi();
1799  Int_t lastRun = static_cast<TObjString*>(currRange->UncheckedAt(1))->String().Atoi();
1800  delete currRange;
1801 
1802  // If an object is already there, ask to remove it or keep it
1803  for ( Int_t irun=0; irun<2; irun++ ) {
1804  Int_t runnr = ( irun == 0 ) ? firstRun : lastRun;
1805  specificStorage->QueryCDB(runnr);
1806  TObjArray* allIdsForRun = specificStorage->GetQueryCDBList();
1807  TIter nextId(allIdsForRun);
1808  AliCDBId* id = 0x0;
1809  while ((id = dynamic_cast<AliCDBId*>(nextId()))) {
1810  TString path(id->GetPath());
1811  Int_t foundFirst = id->GetFirstRun();
1812  Int_t foundLast = id->GetLastRun();
1813  Int_t version = id->GetVersion();
1814  Int_t subversion = id->GetSubVersion();
1815  TString locPath = Form("%s/%s/Run%d_%d_v%d_s%d.root",outCDBdir.Data(),path.Data(),foundFirst,foundLast,version,subversion);
1816  if ( ! gSystem->AccessPathName(locPath.Data()) ) {
1817  ExecCommand(Form("rm %s",locPath.Data()), kTRUE);
1818  }
1819  }
1820  }
1821 
1822  // Save the CDB object in the specific storage
1823  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
1824  AliMUONCDB::WriteToCDB(effMap, "MUON/Calib/TriggerEfficiency", firstRun, lastRun, "Measured efficiencies");
1825  delete effMap; // CAVEAT: effMap is owner of efflist
1826  }
1827  return kTRUE;
1828 }
1829 
1830 //___________________________________________________________________________
1831 //Bool_t AliMTRChEffAnalysis::WriteMerged() const
1832 //{
1833 // /// Write merged efficiencies on file
1834 // if ( ! HasMergedResults ) return kFALSE;
1835 //
1836 // TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
1837 //
1838 // AliTrigChEffOutput* trigOut = 0x0;
1839 // TIter next(fMergedOutputs);
1840 // while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
1841 // TFile* file = TFile::Open(Form("mergedTrigEff_runs_%s.root",trigOut->GetName()),"RECREATE");
1842 // TList* effList = GetEffHistoList(trigOut,condition);
1843 // effList->Write("triggerChamberEff",TObject::kSingleKey);
1844 // trigOut->GetOutput()->Write(trigOut->GetOutput()->GetName(),TObject::kSingleKey);
1845 // file->Close();
1846 // delete file;
1847 // delete effList;
1848 // }
1849 // return kTRUE;
1850 //}
Counts per cathode histogram index.
Int_t GetIndexFromRun(UInt_t runNumber) const
ClassImp(AliAnalysisTaskTriggerRates) AliAnalysisTaskTriggerRates
TList * GetEffHistoList(TString physSel, TString trigClassNames, TString centrality, Int_t itrackSel, Int_t imatch, Int_t imethod)
void CompareMergedEfficiencies(const char *opt) const
void CopyDir(TDirectory *source) const
Double_t GetError(Double_t errLow, Double_t errHigh) const
const char * title
Definition: MakeQAPdf.C:26
tracks used for calculation
Bool_t CopyLocally(const char *runList, const char *path, const char *pattern, const char *localFileList, const char *outDir, const char *directory) const
Hit pattern from tracker track extrapolation.
Double_t GetThreeOfFour(TArrayD eff, TArrayD effErr, Double_t &probErr) const
TSystem * gSystem
TGraphAsymmErrors * GetTrendEff(Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem) const
centrality
TArrayI MergeRangesForStat(TArrayI runRanges, Double_t averageStatError, Bool_t excludePeriphericBoards=kTRUE) const
Counts per slat histogram index.
Int_t CompareEfficiencies(const char *sources, const char *titles, const char *opt, const char *canvasNameSuffix="") const
Bool_t SetResultsFromGrid(const char *runList, const char *path, const char *pattern, const char *localFileList="localFileList.txt", const char *outDir="", const char *directory="MTR_ChamberEffMap", const char *outputName="testMTRChamberEff")
Int_t nDE
TList * GetEffHistoList(AliTrigChEffOutput *trigOut, TObjArray *condition) const
Number of count type.
Bool_t AddToList(const char *filename, const char *outputName)
TObjArray * fMergedOutputs
!<! List of conditions for trigger efficiency
Output for Trig chamber effieincy.
Bool_t SetEffConditions(const char *physSel, const char *trigClassName, const char *centrality, Int_t itrackSel, Int_t imatch, Int_t imethod)
Output for Trig chamber effieincy.
void DrawStatContribution(Int_t itype, Int_t irpc, Double_t maxNsigmaOutliers=-1., Double_t minY=0., Double_t maxY=0.15) const
Double_t GetAverageStat(Int_t firstRun, Int_t lastRun, Int_t itype=AliTrigChEffOutput::kHboardEff, Bool_t excludePeriphericBoards=kTRUE) const
Bool_t SetOutList(const char *localFileList, const char *outputName)
TArrayI GetHomogeneusRanges(Double_t chi2Cut=3, Int_t maxNRanges=4, Double_t minEffVariation=0.005, Bool_t perRPC=kTRUE, TArrayI *forcedChanges=0x0, Double_t minEff=0.85, Double_t maxEff=1.01)
TString GetIdentifier(AliTrigChEffOutput *trigOut, TObjArray *condition, Int_t itype, Int_t icount, Int_t ichamber) const
Bool_t Data(TH1F *h, Double_t *rangefit, Bool_t writefit, Double_t &sgn, Double_t &errsgn, Double_t &bkg, Double_t &errbkg, Double_t &sgnf, Double_t &errsgnf, Double_t &sigmafit, Int_t &status)
TH1 * GetHisto(TList *effHistoList, Int_t itype, Int_t icount, Int_t ichamber) const
Bool_t SetCondition(const char *physSel, const char *trigClassName, const char *centrality, Int_t itrackSel, Int_t imatch, Int_t imethod, Bool_t isBasic)
Bool_t WriteMergedToOCDB(const char *outputCDB="CDB") const
TList * GetRunList(const char *runList) const
Int_t GetRunNumber(Int_t ipt) const
Bool_t HasMergedResults() const
Bool_t ExecCommand(TString command, Bool_t prompt) const
TArrayI BoardsInRPC(Int_t irpc) const
Double_t FitRangesFunc(Double_t *x, Double_t *par)
Counts per board histogram index.
void DrawEffTrend(Int_t itype, Int_t irpc, Double_t maxNsigmasOutliers=-1., Double_t minEff=0.8, Double_t maxEff=1.01) const
TObjArray * fConditions
!<! List of outputs
TGraphAsymmErrors * GetOutliers(TGraphAsymmErrors *graph, Double_t maxNsigmas=3.) const
Bool_t AddSystematicCondition(const char *physSel, const char *trigClassName, const char *centrality, Int_t itrackSel, Int_t imatch, Int_t imethod)
Bool_t SetResultsFromWeb(const char *runList, const char *path, const char *localFileList="localFileList.txt", const char *outDir="", const char *directory="MTR_ChamberEffMap", const char *outputName="testMTRChamberEff")
Bool_t DrawSystematicEnvelope(Bool_t perRPC=kFALSE) const
Bool_t MergeOutput(TArrayI runRanges, Double_t averageStatError=0.01, Bool_t isIndex=kFALSE)
TString GetHistoName(Int_t itype, Int_t icount, Int_t ichamber, Int_t itrackSel, Int_t imatch, Int_t imethod)
TH1 * GetSum(AliTrigChEffOutput *trigOut, TObjArray *condition, Int_t itype, Int_t icount, Int_t ichamber) const
TH1 * GetTrend(Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem) const