AliPhysics  b43479f (b43479f)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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 //________________________________________________________________________
388 Int_t AliMTRChEffAnalysis::CompareEfficiencyMethods ( const char* source, const char* opt, const char* canvasNameSuffix ) const
389 {
391  if ( ! fConditions ) {
392  AliWarning("No condition found! Please specify the default efficiency condition with SetEffConditions and then add additional tests with AddSystematicCondition");
393  return -1;
394  }
395 
396  AliTrigChEffOutput trigOut(source);
397 
398  Int_t nConditions = fConditions->GetEntriesFast();
399 
400  TObjArray condTitle(nConditions);
401  condTitle.SetOwner();
402  GetShortConditionTitles(&trigOut,condTitle);
403 
404  TString titles = "";
405  TObjArray effMapList;
406  effMapList.SetOwner();
407 
408  for ( Int_t icond=0; icond<nConditions; icond++ ) {
409  TObjArray* condition = static_cast<TObjArray*>(fConditions->UncheckedAt(icond));
410 
411  TList* effList = GetEffHistoList(&trigOut, condition);
412  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
413  effMapList.Add(effMap);
414  titles += Form("%s,",condTitle.At(icond)->GetName());
415  }
416 
417  titles.Remove(TString::kTrailing,',');
418  return CompareEfficiencies(&effMapList, titles, opt, canvasNameSuffix);
419 }
420 
421 
422 //________________________________________________________________________
424 {
425  if ( ! HasMergedResults() ) return;
426 
427  TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
428  TString titles = "";
429 
430  TObjArray effMapList;
431  effMapList.SetOwner();
432 
433  AliTrigChEffOutput* trigOut = 0x0;
434  TIter next(fMergedOutputs);
435  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
436  TList* effList = GetEffHistoList(trigOut, condition);
437  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
438  effMapList.Add(effMap);
439  titles += Form("%s,",trigOut->GetName());
440  }
441  titles.Remove(TString::kTrailing,',');
442 
443  CompareEfficiencies(&effMapList, titles.Data(), opt, "MergedComp");
444 }
445 
446 
447 //________________________________________________________________________
448 //TArrayI AliMTRChEffAnalysis::CheckHomogeneity ( const TGraphAsymmErrors* trendGraph, Double_t nSigmas, Bool_t revert ) const
449 //{
450 // /// Check efficiency homogeneity
451 // TArrayI ranges(trendGraph->GetN());
452 // Double_t xpt=0., ypt=0.;
453 //
454 // Double_t sumEffOverSigma2 = -10.;
455 // Double_t sumInvSimga2 = 0.001;
456 // Int_t irange = 0;
457 // Int_t npt = trendGraph->GetN();
458 // for ( Int_t ipt=0; ipt<npt; ipt++ ) {
459 // Int_t currPt = ( revert ) ? npt-1-ipt : ipt;
460 // trendGraph->GetPoint(currPt,xpt,ypt);
461 // Double_t err = GetError(trendGraph->GetErrorYlow(currPt),trendGraph->GetErrorYhigh(currPt));
462 // Double_t invSigma2 = 1./(err*err);
463 // Double_t effOverSigma2 = ypt*invSigma2;
464 //
465 // Double_t meanEff = sumEffOverSigma2 / sumInvSimga2;
466 // Double_t diff = ypt - meanEff;
467 // Double_t diffErr = TMath::Sqrt(err*err+1./sumInvSimga2);
469 // if ( TMath::Abs(diff)/diffErr > nSigmas ) {
470 // TString run = trendGraph->GetXaxis()->GetBinLabel(currPt+1);
471 // ranges[irange++] = run.Atoi();
472 // sumEffOverSigma2 = 0.;
473 // sumInvSimga2 = 0.;
474 // }
475 // sumEffOverSigma2 += effOverSigma2;
476 // sumInvSimga2 += invSigma2;
477 // }
478 // TString run = trendGraph->GetXaxis()->GetBinLabel(trendGraph->GetN());
479 // ranges[irange++] = run.Atoi();
480 // ranges.Set(irange);
481 // return ranges;
482 //}
483 
484 //________________________________________________________________________
485 void AliMTRChEffAnalysis::CopyDir(TDirectory *source) const
486 {
488  TDirectory *savdir = gDirectory;
489  TDirectory *adir = savdir->mkdir(source->GetName());
490  adir->cd();
491  //loop on all entries of this directory
492  TKey *key;
493  TIter nextkey(source->GetListOfKeys());
494  while ((key = (TKey*)nextkey())) {
495  const char *classname = key->GetClassName();
496  TClass *cl = gROOT->GetClass(classname);
497  if (!cl) continue;
498  if (cl->InheritsFrom(TDirectory::Class())) {
499  source->cd(key->GetName());
500  TDirectory *subdir = gDirectory;
501  adir->cd();
502  CopyDir(subdir);
503  adir->cd();
504  } else if (cl->InheritsFrom(TTree::Class())) {
505  TTree *T = (TTree*)source->Get(key->GetName());
506  adir->cd();
507  TTree *newT = T->CloneTree(-1,"fast");
508  newT->Write();
509  } else {
510  source->cd();
511  TObject *obj = key->ReadObj();
512  adir->cd();
513  obj->Write(obj->GetName(),TObject::kSingleKey);
514  delete obj;
515  }
516  }
517  adir->SaveSelf(kTRUE);
518  savdir->cd();
519 }
520 
521 
522 //________________________________________________________________________
523 Bool_t AliMTRChEffAnalysis::CopyLocally ( const char* runList, const char* path, const char* pattern, const char* localFileList, const char* outDir, const char* directory ) const
524 {
526  TString sPattern(pattern);
527  TString sOutDir(outDir);
528  TString sPath(path);
529  Bool_t isGrid = (! sPattern.IsNull());
530 
531  if ( sOutDir.IsNull() ) {
532  if ( isGrid ) {
533  TString data = sPath(TRegexp("/data/"));
534  TString year = sPath(TRegexp("/20[0-9][0-9]/"));
535  TString period = sPath(TRegexp("/LHC[0-9][0-9][a-z]"));
536  period.Append("/");
537  // TPRegexp re("/(muon|pass).*?/");
538  TString pass = AliAnalysisMuonUtility::GetPassName(path);
539  if ( pass.IsNull() ) pass = AliAnalysisMuonUtility::GetPassName(pattern);
540  sOutDir = data+year+period+pass;
541  sOutDir.ReplaceAll("//","/");
542  sOutDir.Remove(TString::kTrailing,'/');
543  sOutDir.Remove(TString::kLeading,'/');
544  }
545  else sOutDir = sPath;
546  sOutDir.Remove(TString::kTrailing,'/');
547  sOutDir.Remove(TString::kLeading,'/');
548  }
549 
550  TGridResult* res = 0x0;
551  if ( isGrid ) {
552  if ( ! gGrid ) TGrid::Connect("alien://");
553  res = gGrid->Query(path,pattern);
554  }
555 
556  TList* rl = GetRunList(runList);
557 
558  ofstream outFile(localFileList);
559  Bool_t allOk = kTRUE;
560  Bool_t prompt = kTRUE;
561  Bool_t overwrite = kFALSE;
562  for ( Int_t irun=0; irun<rl->GetEntries(); irun++ ) {
563  TString run = static_cast<TObjString*>(rl->At(irun))->String();
564  TString dest = Form("%s/%09i/QAresults.root",sOutDir.Data(),run.Atoi());
565  TString destDir = gSystem->DirName(dest.Data());
566  if ( gSystem->AccessPathName(destDir.Data()) ) ExecCommand(Form("mkdir -p %s",destDir.Data()),kFALSE);
567  Bool_t copyFile = kTRUE;
568  Bool_t isFileOk = kTRUE;
569  if ( gSystem->AccessPathName(dest) == 0 ) {
570  if ( prompt ) {
571  TString decision = "";
572  cout << "Local file " << dest.Data() << " already exist: overwrite? [y/n/ya/na (a=use decision for all)]" << endl;
573  cin >> decision;
574  if ( decision.EndsWith("a") ) prompt = kFALSE;
575  if ( decision.BeginsWith("y") ) overwrite = kTRUE;
576  else overwrite = kFALSE;
577  }
578  copyFile = overwrite;
579  }
580  if ( copyFile ) {
581  TString src = "";
582  if ( isGrid ) {
583  for ( Int_t ifile=0; ifile<res->GetEntries(); ifile++ ) {
584  TString inFilename = res->GetKey(ifile,"turl");
585  if ( inFilename.Contains(run.Data()) ) {
586  src = inFilename;
587  break;
588  }
589  }
590  if ( src.IsNull() ) {
591  AliWarning(Form("Cannot find output for run %s",run.Data()));
592  isFileOk = kFALSE;
593  }
594  else {
595  TFile* outFile = TFile::Open(dest,"RECREATE");
596  TFile* inFile = TFile::Open(src);
597  inFile->cd();
598  TDirectory* dir = static_cast<TDirectory*>(inFile->Get(directory));
599  if ( dir ) {
600  outFile->cd();
601  CopyDir(dir);
602  }
603  else isFileOk = kFALSE;
604  delete inFile;
605  delete outFile;
606  }
607  }
608  else {
609  src = Form("http://aliqamu.web.cern.ch/aliqamu/%s",dest.Data());
610  isFileOk = TFile::Cp(src.Data(),dest.Data());
611  }
612  }
613 
614  if ( isFileOk ) outFile << gSystem->pwd() << "/" << dest.Data() << endl;
615  else allOk = kFALSE;
616  }
617  delete rl;
618  outFile.close();
619  return allOk;
620 }
621 
622 //________________________________________________________________________
623 void AliMTRChEffAnalysis::DrawEffTrend ( Int_t itype, Int_t irpc, Double_t maxNsigmaOutliers, Double_t minEff, Double_t maxEff ) const
624 {
626  TString baseNames[3] = {"Chamber","RPC","Board"};
627  TString base = baseNames[itype] + "Eff";
628  if ( itype == AliTrigChEffOutput::kHboardEff ) {
629  if ( irpc < 0 ) {
630  AliWarning("Please specify RPC");
631  return;
632  }
633  base += Form("InRPC%i",irpc);
634  }
635 
636  Int_t nColumns = 2;
637  Int_t nRows = 2;
638  Int_t width = 600;
639  Int_t height = 600;
640  Int_t nDetEl = 4;
641  Int_t nCh = 1;
642  if ( itype != AliTrigChEffOutput::kHchamberEff ) {
643  nColumns = 6;
644  nRows = 3;
645  width = 1200;
646  height = 800;
647  nDetEl = 18;
648  nCh = 4;
649  }
650  TArrayI boards = BoardsInRPC(irpc);
651  if ( itype == AliTrigChEffOutput::kHboardEff ) nDetEl = boards.GetSize();
652 
653  for ( Int_t ich=0; ich<nCh; ich++ ) {
654  TString canName = base;
655  if ( itype != AliTrigChEffOutput::kHchamberEff ) canName += Form("Ch%i",11+ich);
656  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),25*ich,25*ich,width,height);
657  can->Divide(nColumns,nRows,0,0);
658  // can->SetTopMargin(0.03);
659  // can->SetBottomMargin(0.1);
660  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
661  can->cd(idetelem+1);
662  gPad->SetTicks(1,1);
663  gPad->SetGridy();
664  Int_t detElemId = idetelem;
665  if ( itype == AliTrigChEffOutput::kHchamberEff ) detElemId = 11+idetelem;
666  else if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
667 
668  TString title = Form("%s %i",baseNames[itype].Data(),detElemId);
669  TLegend* leg = new TLegend(0.2,0.15,0.8,0.4);
670  leg->SetHeader(title.Data());
671  for ( Int_t icount=0; icount<2; icount++ ) {
672  TGraphAsymmErrors* gr = GetTrendEff(itype, icount, ich, detElemId);
673  gr->SetLineColor(icount+1);
674  gr->SetMarkerColor(icount+1);
675  gr->SetMarkerStyle(24+2*icount);
676  gr->GetYaxis()->SetRangeUser(minEff,maxEff);
677  // gr->GetYaxis()->SetLabelSize(0.025*nRows);
678  // gr->GetXaxis()->SetLabelSize(0.025*nColumns);
679  gr->SetTitle("");
680  gr->Draw(icount==0?"ap":"p");
681  TString legTitle = ( icount==0 ) ? "bending plane" : "non-bending plane";
682  leg->AddEntry(gr,legTitle.Data(),"lp");
683  if ( maxNsigmaOutliers > 0. ) {
684  TGraphAsymmErrors* outliers = GetOutliers(gr,maxNsigmaOutliers);
685  outliers->SetLineColor(6+icount);
686  outliers->SetMarkerColor(6+icount);
687  outliers->SetMarkerStyle(20+2*icount);
688  outliers->SetLineWidth(2);
689  outliers->Draw("p");
690  legTitle.ReplaceAll("plane","outliers");
691  leg->AddEntry(outliers,legTitle.Data(),"lp");
692  }
693  }
694  leg->Draw();
695  }
696  if ( itype == AliTrigChEffOutput::kHchamberEff ) break;
697  }
698 }
699 
700 //________________________________________________________________________
701 void AliMTRChEffAnalysis::DrawStatContribution ( Int_t itype, Int_t irpc, Double_t maxNsigmaOutliers, Double_t minY, Double_t maxY ) const
702 {
704  if ( itype == AliTrigChEffOutput::kHchamberEff ) {
705  AliWarning("This function is valid only for itype 1 and 2");
706  return;
707  }
708 
709  TString baseNames[3] = {"Chamber","RPC","Board"};
710  TString base = baseNames[itype] + "Stat";
711  if ( itype == AliTrigChEffOutput::kHboardEff ) {
712  if ( irpc < 0 ) {
713  AliWarning("Please specify RPC");
714  return;
715  }
716  base += Form("InRPC%i",irpc);
717  }
718 
719  Int_t nColumns = 6;
720  Int_t nRows = 3;
721  Int_t width = 1200;
722  Int_t height = 800;
723  Int_t nDetEl = 18;
724  Int_t nCh = 1;
725 
726  TArrayI boards = BoardsInRPC(irpc);
727  if ( itype == AliTrigChEffOutput::kHboardEff ) nDetEl = boards.GetSize();
728 
729  for ( Int_t ich=0; ich<nCh; ich++ ) {
730  TH1* histos[nDetEl];
731  TH1* sumHistos = 0x0;
732  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
733  Int_t detElemId = idetelem;
734  if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
735  histos[idetelem] = GetTrend(itype, AliTrigChEffOutput::kAllTracks, ich, detElemId);
736  histos[idetelem]->SetName(Form("%s_stat",histos[idetelem]->GetName()));
737  histos[idetelem]->SetStats(0);
738  if ( sumHistos ) sumHistos->Add(histos[idetelem]);
739  else sumHistos = static_cast<TH1*>(histos[idetelem]->Clone("sumHistos"));
740  }
741 
742  TString canName = base; //Form("%sCh%i",base.Data(),11+ich);
743  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),25*ich,25*ich,width,height);
744  can->Divide(nColumns,nRows,0,0);
745  for ( Int_t idetelem=0; idetelem<nDetEl; idetelem++ ) {
746  can->cd(idetelem+1);
747  gPad->SetTicks(1,1);
748  gPad->SetGridy();
749  Int_t detElemId = idetelem;
750  if ( itype == AliTrigChEffOutput::kHboardEff ) detElemId = boards[idetelem];
751  TString title = Form("%s %i",baseNames[itype].Data(),detElemId);
752  TLegend* leg = new TLegend(0.2,0.65,0.8,0.9);
753  leg->SetHeader(title.Data());
754  TGraphAsymmErrors* gr = new TGraphAsymmErrors(histos[idetelem],sumHistos,"e0");
755  gr->SetHistogram(static_cast<TH1F*>(histos[idetelem]));
756 
757  gr->SetMarkerStyle(24);
758  gr->SetMarkerSize(0.5);
759  gr->GetYaxis()->SetRangeUser(minY,maxY);
760  gr->GetYaxis()->SetTitle(Form("Tracks in %s / Sum of tracks of %ss in %s",baseNames[itype].Data(),baseNames[itype].Data(),baseNames[itype-1].Data()));
761  gr->SetTitle("");
762  gr->Draw("ap");
763  TString legTitle = "stat";
764 // leg->AddEntry(gr,legTitle.Data(),"lp");
765  if ( maxNsigmaOutliers > 0. ) {
766  TGraphAsymmErrors* outliers = GetOutliers(gr,maxNsigmaOutliers);
767  outliers->SetLineColor(6);
768  outliers->SetMarkerColor(6);
769  outliers->SetMarkerStyle(20);
770  outliers->SetLineWidth(2);
771  outliers->Draw("p");
772  legTitle = "outliers";
773  leg->AddEntry(outliers,legTitle.Data(),"lp");
774  }
775 // }
776  leg->Draw();
777  }
778  delete sumHistos;
779  }
780 }
781 
782 //________________________________________________________________________
783 Bool_t AliMTRChEffAnalysis::DrawSystematicEnvelope ( Bool_t perRPC ) const
784 {
786  if ( ! HasMergedResults() ) return kFALSE;
787 
788  Int_t colors[] = {kBlack, kRed, kSpring, kTeal, kBlue, kViolet, kMagenta, kOrange, kGray};
789  Int_t ncolors = sizeof(colors)/sizeof(colors[0]);
790 
791  Int_t itype = ( perRPC ) ? AliMUONTriggerEfficiencyCells::kHslatCount : AliMUONTriggerEfficiencyCells::kHboardCount;
792  Int_t countTypes[2] = {AliMUONTriggerEfficiencyCells::kBendingEff,AliMUONTriggerEfficiencyCells::kNonBendingEff};
793 
794  Int_t nConditions = fConditions->GetEntriesFast();
795 
796  TObjArray condTitle(nConditions);
797  condTitle.SetOwner();
798 
799  Double_t xpt, ypt, xref, yref;
800  TArrayD eff(4), effErr(4);
801 
802  AliTrigChEffOutput* trigOut = 0x0;
803  TIter next(fMergedOutputs);
804  Int_t imerged = -1;
805  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
806  imerged++;
807 
808  // Get meaningful short titles for each systematic
809  if ( imerged == 0 ) GetShortConditionTitles(trigOut,condTitle);
810 
811  TArrayI isEmpty(nConditions);
812 
813  // First get the needed graphs
814  Int_t nDE = 0;
815  TObjArray effGraphs[8];
816  for ( Int_t iplane=0; iplane<8; iplane++ ) {
817  effGraphs[iplane].SetOwner();
818  }
819  TString titles = "";
820  TObjArray effMapList;
821  effMapList.SetOwner();
822  for ( Int_t icond=0; icond<nConditions; icond++ ) {
823  TObjArray* condition = static_cast<TObjArray*>(fConditions->UncheckedAt(icond));
824 
825  TList* effList = GetEffHistoList(trigOut, condition);
826  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
827  effMapList.Add(effMap);
828  titles += Form("%s,",condTitle.At(icond)->GetName());
829 
830  for ( Int_t ich=0; ich<4; ich++ ) {
831  TString currName = effMap->GetHistoName(itype,AliMUONTriggerEfficiencyCells::kAllTracks,ich);
832  TH1* histoDen = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
833  for ( Int_t icount=0; icount<2; icount++ ) {
834  Int_t iplane = 4*icount+ich;
835 
836  if ( histoDen->GetEntries() > 0 ) {
837  currName = effMap->GetHistoName(itype,countTypes[icount],ich);
838  TH1* histoNum = static_cast<TH1*>(effMap->GetHistoList()->FindObject(currName.Data()));
839  TGraphAsymmErrors* gr = new TGraphAsymmErrors(histoNum,histoDen,"e0");
840  nDE = gr->GetN();
841  effGraphs[iplane].AddAtAndExpand(gr,icond);
842  }
843  else {
844  isEmpty[icond] = 1;
845  AliWarning(Form("No entries in count %i and ch %i for %s\n",icount,ich,condTitle.At(icond)->GetName()));
846  }
847  }
848  }
849  }
850 
851  titles.Remove(TString::kTrailing,',');
852  CompareEfficiencies(&effMapList, titles, "diff", trigOut->GetName());
853 
854  // Draw average dispersion per plane
855  TString canName = Form("EffSyst_%s",trigOut->GetName());
856  Int_t pos = 25*(imerged+1);
857  TCanvas* can = new TCanvas(canName.Data(),canName.Data(),pos,pos,1200,800);
858  can->Divide(4,2,0,0);
859 
860  for ( Int_t icount=0; icount<2; icount++ ) {
861  for ( Int_t ich=0; ich<4; ich++ ) {
862  Int_t iplane = 4*icount+ich;
863  can->cd(iplane+1);
864  gPad->SetTicks(1,1);
865  gPad->SetLogy();
866  TLegend* leg = new TLegend(0.15,0.7,0.9,0.9);
867  leg->SetHeader(trigOut->GetHistoName(-1,icount,ich,-1,-1,-1));
868  TH1* sumHisto = 0x0;
869  for ( Int_t icond=1; icond<nConditions; icond++ ) {
870  if ( isEmpty[icond] == 1 ) continue;
871  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);
872  histo->GetXaxis()->SetTitle("Eff.-(ref.Eff.)");
873  histo->GetYaxis()->SetTitle("1/#sigma^{2}");
874 
875  TGraphAsymmErrors* gr = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(icond));
876  TGraphAsymmErrors* grRef = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(0));
877  for ( Int_t ipt=0; ipt<gr->GetN(); ipt++ ) {
878  gr->GetPoint(ipt,xpt,ypt);
879  Double_t err = GetError(gr->GetErrorYlow(ipt),gr->GetErrorYhigh(ipt));
880  Double_t invErr2 = ( err > 0. ) ? 1./(err*err) : 0.;
881  grRef->GetPoint(ipt,xref,yref);
882 
883  Double_t diff = ypt-yref;
884  histo->Fill(diff,invErr2);
885  }
886 
887  if ( ! sumHisto ) {
888  sumHisto = static_cast<TH1*>(histo->Clone(Form("syst_%s_plane%i_ch%i",trigOut->GetName(),icount,11+ich)));
889  sumHisto->SetLineColor(1);
890  sumHisto->Draw();
891  leg->AddEntry(sumHisto,"All systematics","l");
892  }
893  else sumHisto->Add(histo);
894  Int_t icolor = ( icond < ncolors ) ? colors[icond] : 20+icond;
895  histo->SetLineColor(icolor);
896  histo->Draw("same");
897  leg->AddEntry(histo,condTitle[icond]->GetName(),"l");
898  } // loop on conditions
899  leg->Draw();
900  } // loop on chambers
901  } // loop on counts
902 
903 
904  canName = Form("TriggerEff_3outOf4_syst_%s",trigOut->GetName());
905  pos += 50;
906  TCanvas* canSyst = new TCanvas(canName.Data(),canName.Data(),pos,pos,600,600);
907  canSyst->SetLogy();
908  TLegend* leg = new TLegend(0.15,0.7,0.9,0.4);
909 // leg->SetHeader(trigOut->GetHistoName(-1,icount,ich,-1,-1,-1));
910  TH1* histo[nConditions];
911  for ( Int_t icond=0; icond<nConditions; icond++ ) {
912  if ( isEmpty[icond] == 1 ) continue;
913  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);
914  histo[icond]->GetXaxis()->SetTitle("Trig. prob. - (ref. trig. prob)");
915  histo[icond]->GetYaxis()->SetTitle("1/#sigma^{2}");
916  }
917 
918  for ( Int_t ipt=0; ipt<nDE; ipt++ ) {
919  Double_t refTrigProb = 0.; // refTrigProbErr = 0.;
920  for ( Int_t icond=0; icond<nConditions; icond++ ) {
921  if ( isEmpty[icond] == 1 ) continue;
922  Double_t trigProb = 1., trigProbErr2 = 0.;
923  for ( Int_t icount=0; icount<2; icount++ ) {
924  for ( Int_t ich=0; ich<4; ich++ ) {
925  Int_t iplane = 4*icount+ich;
926  TGraphAsymmErrors* gr = static_cast<TGraphAsymmErrors*>(effGraphs[iplane].UncheckedAt(icond));
927  gr->GetPoint(ipt,xpt,ypt);
928  eff[ich] = ypt;
929  effErr[ich] = GetError(gr->GetErrorYlow(ipt),gr->GetErrorYhigh(ipt));
930  } // loop on chambers
931  Double_t effErr34 = 0.;
932  Double_t eff34 = GetThreeOfFour(eff,effErr,effErr34);
933  trigProb *= eff34;
934  trigProbErr2 += effErr34*effErr34;
935  } // loop on counts
936 
937  if ( icond == 0 ) {
938  refTrigProb = trigProb;
939 // refTrigProbErr = trigProbErr;
940  }
941  else {
942  Double_t invErr2 = ( trigProbErr2>0. ) ? 1./trigProbErr2 : 0.;
943  histo[icond]->Fill(trigProb-refTrigProb,invErr2);
944  }
945  } // loop on conditions
946  } // loop on points
947 
948  for ( Int_t icond=0; icond<nConditions; icond++ ) {
949  if ( isEmpty[icond] == 1 ) continue;
950  TString title = ( icond == 0 ) ? "All systematics" : condTitle[icond]->GetName();
951  Int_t icolor = ( icond < ncolors ) ? colors[icond] : 20+icond;
952  histo[icond]->SetLineColor(icolor);
953  histo[icond]->Draw((icond == 0)?"":"same");
954  leg->AddEntry(histo[icond],title.Data(),"l");
955  if ( icond>0 ) histo[0]->Add(histo[icond]);
956  }
957  leg->Draw();
958  } // loop on merged output
959  return kTRUE;
960 }
961 
962 //________________________________________________________________________
963 Bool_t AliMTRChEffAnalysis::ExecCommand ( TString command, Bool_t prompt ) const
964 {
966  TString decision = "y";
967 
968  if ( gROOT->IsBatch() ) prompt = kFALSE; // To run with crontab
969 
970  if ( prompt ) {
971  cout << command.Data() << " ? [y/n]" << endl;
972  cin >> decision;
973  }
974 
975  decision.ToLower();
976  if ( decision == "y" ) {
977  cout << "Executing: " << command.Data() << endl;
978  gSystem->Exec(command.Data());
979  return kTRUE;
980  }
981 
982  return kFALSE;
983 }
984 
985 //________________________________________________________________________
986 Double_t AliMTRChEffAnalysis::FitRangesFunc ( Double_t* x, Double_t* par )
987 {
994 
995  Double_t xx = x[0];
996  Double_t val = par[1];
997  Int_t nChanges = par[0];
998  Double_t matchDiff = 123456789.;
999  Int_t matchChange = -1;
1000  for ( Int_t iknot=0; iknot<nChanges; iknot++ ) {
1001  Int_t iparChange = 2*(iknot+1);
1002  Double_t diff = xx - par[iparChange];
1003  if ( diff >= 0. && diff < matchDiff ) {
1004  matchDiff = diff;
1005  matchChange = iparChange;
1006  }
1007  }
1008  if ( matchChange >= 0 ) val = par[matchChange+1];
1009 
1010  return val;
1011 }
1012 
1013 //________________________________________________________________________
1014 Double_t AliMTRChEffAnalysis::GetAverageStat ( Int_t firstRun, Int_t lastRun, Int_t itype,Bool_t excludePeriphericBoard ) const
1015 {
1016  TH1* statHisto = 0x0;
1017 
1018  UInt_t uFirstRun = (UInt_t)firstRun;
1019  UInt_t uLastRun = (UInt_t)lastRun;
1020 
1021  TIter next(fOutputs);
1022  TList* effHistoList = 0x0;
1023  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1024  UInt_t run = effHistoList->GetUniqueID();
1025  if ( run < uFirstRun || run > uLastRun ) continue;
1026  TH1* histo = GetHisto(effHistoList,itype,AliTrigChEffOutput::kAllTracks,0);
1027 // if ( ! histo ) continue;
1028  if ( statHisto ) statHisto->Add(histo);
1029  else statHisto = static_cast<TH1*>(histo->Clone("tmpStatHisto"));
1030 // delete histo;
1031  }
1032  if ( ! statHisto ) return 0.;
1033 
1034  Double_t statPerDE = 0.;
1035  Double_t nDe = 0.;
1036  if ( itype == AliTrigChEffOutput::kHboardEff && excludePeriphericBoard ) {
1037  Int_t excludeBoardsHalf[] = {1, 17, 39, 61, 77, 93,109,
1038  16, 38, 60, 76, 92, 108, 117,
1039  110, 111, 112, 113, 114, 115, 116};
1040  Int_t nExcludedHalf = sizeof(excludeBoardsHalf)/sizeof(excludeBoardsHalf[0]);
1041  for ( Int_t ibin=1; ibin<=statHisto->GetNbinsX(); ibin++ ) {
1042  Bool_t skip = kFALSE;
1043  for ( Int_t iexcl=0; iexcl<nExcludedHalf; iexcl++ ) {
1044  if ( ibin == excludeBoardsHalf[iexcl] || ibin == excludeBoardsHalf[iexcl]+117 ) {
1045  skip = kTRUE;
1046  break;
1047  }
1048  }
1049  if ( skip ) continue;
1050  statPerDE += statHisto->GetBinContent(ibin);
1051  nDe += 1.;
1052  }
1053  }
1054  else {
1055  statPerDE = statHisto->Integral();
1056  nDe = (Double_t)statHisto->GetNbinsX();
1057  }
1058  statPerDE = nDe > 0. ? statPerDE/nDe : -1.;
1059 
1060  delete statHisto;
1061  return statPerDE;
1062 }
1063 
1064 //________________________________________________________________________
1065 TList* AliMTRChEffAnalysis::GetEffHistoList ( AliTrigChEffOutput* trigOut, TObjArray* condition ) const
1066 {
1068  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1069  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1070  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1071 
1072  return trigOut->GetEffHistoList(condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName(),itrackSel,imatch,imethod);
1073 }
1074 
1075 //________________________________________________________________________
1076 Double_t AliMTRChEffAnalysis::GetError ( Double_t errLow, Double_t errHigh ) const
1077 {
1079  return TMath::Max(errLow,errHigh);
1080 }
1081 
1082 //________________________________________________________________________
1083 TH1* AliMTRChEffAnalysis::GetHisto ( TList* effHistoList, Int_t itype, Int_t icount, Int_t ichamber ) const
1084 {
1086  Int_t ihisto = ( itype == AliTrigChEffOutput::kHchamberEff ) ? icount : AliTrigChEffOutput::kNcounts + 4*AliTrigChEffOutput::kNcounts*(itype-1) + 4*icount + ichamber;
1087  return static_cast<TH1*>(effHistoList->At(ihisto));
1088 }
1089 
1090 //________________________________________________________________________
1091 TArrayI AliMTRChEffAnalysis::GetHomogeneousRanges ( Double_t chi2Cut, Int_t maxNRanges, Double_t minEffVariation, Bool_t perRPC, TArrayI* forcedChanges, Double_t minEff, Double_t maxEff )
1092 {
1094 
1095  Int_t nRuns = fOutputs->GetEntriesFast();
1096 
1097  TH1F* hRunChangeCount = new TH1F("runChangeCount","Number of RPCs changing efficiency per run",nRuns,-0.5,-0.5+(Double_t)nRuns);
1098  hRunChangeCount->SetXTitle("Run num.");
1099  hRunChangeCount->SetYTitle("Num. of RPCs with change in eff.");
1100  for ( Int_t irun=0; irun<nRuns; irun++ ) {
1101  hRunChangeCount->GetXaxis()->SetBinLabel(irun+1,Form("%i",GetRunNumber(irun)));
1102  }
1103 
1105 
1106  Int_t nCanvas = perRPC ? 4 : 18;
1107  TObjArray canList(nCanvas);
1108 
1109  for ( Int_t irpc=0; irpc<18; irpc++ ) {
1110  Int_t icount = AliTrigChEffOutput::kBothPlanesEff;
1111  TArrayI boards = BoardsInRPC(irpc);
1112  Int_t firstDetEl = perRPC ? irpc : 0;
1113  Int_t lastDetEl = perRPC ? irpc : boards.GetSize()-1;
1114 
1115  for ( Int_t ich=0; ich<4; ich++ ) {
1116  Int_t ican = perRPC ? ich : irpc;
1117  TCanvas* can = static_cast<TCanvas*>(canList.At(ican));
1118  if ( ! can ) {
1119  TString canName = perRPC ? Form("testRanges_ch%i",11+ich) : Form("testRanges_RPC%i",irpc);
1120  can = new TCanvas(canName.Data(),canName.Data(),10*ich,10*ich,1200,800);
1121  can->Divide(6,3,0,0);
1122  can->SetMargin(0.,0.,0.,0.);
1123  canList.AddAt(can,ican);
1124  }
1125 
1126  for ( Int_t idetel=firstDetEl; idetel<=lastDetEl; idetel++ ) {
1127  Int_t currDE = ( perRPC ) ? idetel : boards[idetel];
1128 // for ( Int_t icount=0; icount<2; icount++ ) {
1129  TGraphAsymmErrors* trendGraph = GetTrendEff(itype,icount,ich,currDE);
1130  TArrayI range = GetHomogeneousRanges(trendGraph,chi2Cut,maxNRanges,minEffVariation,forcedChanges, kTRUE);
1131  trendGraph->GetYaxis()->SetRangeUser(minEff,maxEff);
1132 
1133  can->cd(idetel+1);
1134  gPad->SetTicks(1,1);
1135  gPad->SetMargin(0.08,0.,0.08,0.);
1136  TString drawOpt = ( gPad->GetListOfPrimitives()->GetEntries() == 0 ) ? "ap" : "p";
1137 
1138  TString legendName = Form("%s_%i",can->GetName(),currDE);
1139  TLegend* leg = static_cast<TLegend*>(gPad->GetListOfPrimitives()->FindObject(legendName.Data()));
1140  if ( ! leg ) {
1141  leg = new TLegend(0.2,0.15,0.8,0.4);
1142  leg->SetHeader(Form("%s %i",perRPC?"RPC":"Board",currDE));
1143  leg->SetName(legendName.Data());
1144  }
1145 
1146  if ( ! perRPC ) {
1147  Int_t icolor = ich+1;
1148  trendGraph->SetLineColor(icolor);
1149  trendGraph->SetMarkerColor(icolor);
1150  trendGraph->SetMarkerStyle(24+ich);
1151  TF1* func = static_cast<TF1*>(trendGraph->GetListOfFunctions()->At(0));
1152  if ( func ) {
1153  func->SetLineWidth(2);
1154  func->SetLineColor(icolor);
1155  }
1156  }
1157  trendGraph->GetXaxis()->SetLabelSize(0.07);
1158  trendGraph->SetTitle("");
1159 
1160  trendGraph->Draw(drawOpt.Data());
1161  leg->AddEntry(trendGraph,Form("Chamber %i",11+ich),"lp");
1162  leg->Draw();
1163  for ( Int_t ichange=2; ichange<range.GetSize(); ichange++ ) {
1164  // Store only the run when the change applies
1165  if ( ichange%2 == 1 ) continue;
1166  Int_t runIdx = range[ichange];
1167 // if ( ichange != 0 ) {
1168  TLine* line = new TLine(runIdx,minEff,runIdx,maxEff);
1169  line->SetLineStyle(2);
1170  line->Draw("same");
1171  TLatex text;
1172  text.DrawLatex((Double_t)(runIdx-3),maxEff-0.1*(maxEff-minEff)*(Double_t)(ichange/2),Form("%i",GetRunNumber(runIdx)));
1173 // }
1174  hRunChangeCount->Fill(runIdx);
1175  if ( hRunChangeCount->GetBinContent(runIdx+1) == 1 ) {
1176  TString infoMsg = Form("Efficiency change in %i triggered by ch %i RPC %i",GetRunNumber(runIdx),11+ich,irpc);
1177  if ( ! perRPC ) infoMsg += Form(" Board %i",currDE);
1178  AliInfo(infoMsg.Data());
1179  }
1180  } // loop on change
1181  } // loop on detection element
1182 // }
1183  } // loop on chambers
1184  } // loop on RPC
1185 
1186  // Clusterize contiguous runs
1187  TArrayI runChangeClust(nRuns);
1188  Double_t sumWgtRun = 0.;
1189  Double_t sumWgt = 0;
1190  for ( Int_t irun=0; irun<=nRuns; irun++ ) {
1191  if ( irun == nRuns || hRunChangeCount->GetBinContent(irun+1) == 0 ) {
1192  if ( sumWgt > 0. ) {
1193  Int_t averageRun = TMath::Nint(sumWgtRun / sumWgt);
1194  AliDebug(2,Form("Average run: %i => %i\n",averageRun,GetRunNumber(averageRun)));
1195  runChangeClust[averageRun]++;
1196  sumWgtRun = 0.;
1197  sumWgt = 0.;
1198  }
1199  }
1200  if ( irun == nRuns ) break;
1201 
1202  AliDebug(2,Form("irun %i => %i: wgt %g",irun,GetRunNumber(irun),hRunChangeCount->GetBinContent(irun+1)));
1203 
1204 // Double_t wgt = (Double_t)runChangeCount[irun];
1205  Double_t wgt = hRunChangeCount->GetBinContent(irun+1);
1206  if ( forcedChanges ) {
1207  for ( Int_t ichange=0; ichange<forcedChanges->GetSize(); ichange++ ) {
1208  if ( GetRunNumber(irun) == forcedChanges->At(ichange) ) wgt *= 10.;
1209  }
1210  }
1211  sumWgtRun += wgt*(Double_t)irun;
1212  sumWgt += wgt;
1213  }
1214 
1215  TCanvas* summaryCan = new TCanvas("effChangeSummary","effChangeSummary",50,50,600,600);
1216  summaryCan->SetLogy();
1217  hRunChangeCount->GetXaxis()->LabelsOption("v");
1218  hRunChangeCount->Draw();
1219 
1220  Int_t ientry = 0;
1221  TArrayI runRanges(nRuns);
1222  runRanges[ientry++] = GetRunNumber(0);
1223  for ( Int_t irun=1; irun<nRuns; irun++ ) {
1224  if ( runChangeClust[irun] > 0 ) {
1225  runRanges[ientry++] = GetRunNumber(irun-1);
1226  runRanges[ientry++] = GetRunNumber(irun);
1227  }
1228  }
1229  runRanges[ientry++] = GetRunNumber(nRuns-1);
1230  runRanges.Set(ientry);
1231  return runRanges;
1232 }
1233 
1234 //________________________________________________________________________
1235 TArrayI AliMTRChEffAnalysis::GetHomogeneousRanges ( TGraphAsymmErrors* trendGraph, Double_t chi2Cut, Int_t maxNRanges, Double_t minEffVariation, TArrayI* forcedChanges, Bool_t returnIndex )
1236 {
1238 
1239  TArrayI runRanges;
1240  TF1* func = 0x0;
1241  TArrayD forcedChangesBin;
1242  Int_t nForced = 0;
1243  if ( forcedChanges ) {
1244  forcedChangesBin.Set(forcedChanges->GetSize());
1245  for ( Int_t ichange=0; ichange<forcedChanges->GetSize(); ichange++ ) {
1246  Int_t idx = GetIndexFromRun(forcedChanges->At(ichange));
1247  if ( idx >= 0 ) forcedChangesBin[nForced++] = (Double_t)idx;
1248  else AliWarning(Form("Cannot find run %i\n",forcedChanges->At(ichange)));
1249  }
1250  }
1251 
1252  Double_t minNormChi2 = 123456789.;
1253  Int_t minNormChi2Step = -1;
1254  TString fitOpt = "NQ0";
1255 // Double_t fakeVal = -999.;
1256 // TArrayD params(2*maxNRanges);
1257 // params.Reset(fakeVal);
1258 
1259  for ( Int_t istep=0; istep<maxNRanges; istep++ ) {
1260  Int_t nPars = 2*(istep+1);
1261  Double_t xMin = trendGraph->GetXaxis()->GetXmin();
1262  Double_t xMax = trendGraph->GetXaxis()->GetXmax();
1263  func = new TF1("rangeFunc",this,&AliMTRChEffAnalysis::FitRangesFunc,xMin,xMax,nPars,"AliMTRChEffAnalysis","FitRanges");
1264  func->FixParameter(0,istep);
1265  for ( Int_t ipar=1; ipar<nPars; ipar++ ) {
1266 // if ( ipar >= 2*istep ) {
1267 // if ( TMath::Abs(params[ipar]-fakeVal) < 0.01 ) {
1268 // if ( ipar%2 == 1 ) params[ipar] = 0.95;
1269 // else params[ipar] = (xMax-xMin) * (Double_t)(ipar/2)/((Double_t)(istep+1));
1270 // }
1271 // func->SetParameter(ipar,params[ipar]);
1272  Double_t val = ( ipar%2 == 1 ) ? 0.95 : (xMax-xMin) * (Double_t)(ipar/2)/((Double_t)(istep+1));
1273  func->SetParameter(ipar,val);
1274  if ( ipar%2 == 0 ) func->SetParLimits(ipar,xMin,xMax);
1275  }
1276 
1277  TFitResultPtr fitResult = trendGraph->Fit(func,fitOpt.Data());
1278  // If fit converges close to a point where a break is forced
1279  // fix parameters and redo the fit
1280  if ( forcedChanges ) {
1281  Bool_t hasFixedPars = kFALSE;
1282  for ( Int_t iforced=0; iforced<nForced; iforced++ ) {
1283  for ( Int_t jstep=0; jstep<istep; jstep++ ) {
1284  Int_t ipar = 2*(jstep+1);
1285  if ( TMath::Abs(forcedChangesBin[iforced]-func->GetParameter(ipar)) > 2. ) continue;
1286  func->FixParameter(ipar,forcedChangesBin[iforced]);
1287  hasFixedPars = kTRUE;
1288  }
1289  }
1290  if ( hasFixedPars ) fitResult = trendGraph->Fit(func,fitOpt.Data());
1291  }
1292 
1293 // for ( Int_t ipar=1; ipar<nPars; ipar++ ) {
1294 // params[ipar] = func->GetParameter(ipar);
1295 // }
1296 
1297  Double_t normChi2 = func->GetChisquare() / ((Double_t)func->GetNDF());
1298  if ( normChi2 < minNormChi2 ) {
1299  minNormChi2 = normChi2;
1300  minNormChi2Step = istep;
1301  }
1302 // if ( static_cast<int>(fitResult) == 0 && normChi2 < chi2Cut ) break;
1303  if ( normChi2 < chi2Cut ) break; // REMEMBER TO CHECK
1304  delete func;
1305  func = 0x0;
1306  }
1307 
1308  if ( func ) {
1309  trendGraph->GetListOfFunctions()->Add(func->Clone());
1310  Int_t nSteps = (Int_t)func->GetParameter(0);
1311 
1312  // The runs for which the efficiency changes could be unsorted
1313  // (when forced values are requires)
1314  // Copy the parameters in arrays to sort them
1315  Int_t nPoints = nSteps+1;
1316  TArrayD parRunIdx(nPoints);
1317  TArrayD parEff(nPoints);
1318  for ( Int_t ipar=0; ipar<func->GetNpar(); ipar++ ) {
1319  Int_t istep = ipar/2;
1320  if ( ipar%2 == 0 ) parRunIdx[istep] = func->GetParameter(ipar);
1321  else parEff[istep] = func->GetParameter(ipar);
1322  }
1323  parRunIdx[0] = 0.;
1324  TArrayI sortIdx(nPoints);
1325  TMath::Sort(nPoints,parRunIdx.GetArray(),sortIdx.GetArray(),kFALSE);
1326 
1327  runRanges.Set(2*nPoints);
1328  Int_t irun = 0;
1329  runRanges[irun++] = returnIndex ? 0 : GetRunNumber(0);
1330  for ( Int_t ipoint=1; ipoint<nPoints; ipoint++ ) {
1331  Double_t deltaEff = TMath::Abs(parEff[sortIdx[ipoint]]-parEff[sortIdx[ipoint-1]]);
1332 // if ( ipoint>=2 ) deltaEff = TMath::Max(deltaEff,TMath::Abs(parEff[sortIdx[ipoint]]-parEff[sortIdx[ipoint-2]]));
1333  if ( deltaEff < minEffVariation ) {
1334  AliWarning(Form("Efficiency variation for %s is %g => consider uniform",trendGraph->GetName(),deltaEff));
1335  continue;
1336  }
1337  Int_t runChangeIdx = TMath::Nint(parRunIdx[sortIdx[ipoint]]);
1338  AliDebug(1,Form("Change run: %s => %g => %i %i",trendGraph->GetName(),parRunIdx[sortIdx[ipoint]],runChangeIdx,GetRunNumber(runChangeIdx)));
1339  runRanges[irun++] = returnIndex ? runChangeIdx-1 : GetRunNumber(runChangeIdx-1);
1340  runRanges[irun++] = returnIndex ? runChangeIdx : GetRunNumber(runChangeIdx);
1341  }
1342  Int_t lastPt = trendGraph->GetN()-1;
1343  runRanges[irun++] = returnIndex ? lastPt : GetRunNumber(lastPt);
1344  runRanges.Set(irun);
1345  }
1346  else {
1347  AliWarning(Form("Fit did not converge for %s (minimum chi2 %g for step %i)",trendGraph->GetName(),minNormChi2,minNormChi2Step));
1348  }
1349  return runRanges;
1350 }
1351 
1352 //________________________________________________________________________
1353 TString AliMTRChEffAnalysis::GetIdentifier ( AliTrigChEffOutput* trigOut, TObjArray* condition, Int_t itype, Int_t icount, Int_t ichamber ) const
1354 {
1356 // AliTrigChEffOutput* trigOut = static_cast<AliTrigChEffOutput*>(fOutputs->At(0));
1357 // if ( ! trigOut ) {
1358 // AliWarning("No output object found!");
1359 // delete arr;
1360 // return "";
1361 // }
1362  TString identifier = Form("/%s/%s/%s/",condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName());
1363  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1364  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1365  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1366  TString objName = trigOut->GetHistoName(itype, icount, ichamber, itrackSel, imatch, imethod);
1367  identifier += objName;
1368  return identifier;
1369 }
1370 
1371 //________________________________________________________________________
1372 Int_t AliMTRChEffAnalysis::GetIndexFromRun ( UInt_t runNumber ) const
1373 {
1375  for ( Int_t ipt=0; ipt<fOutputs->GetEntriesFast(); ipt++ ) {
1376  UInt_t run = fOutputs->UncheckedAt(ipt)->GetUniqueID();
1377  if ( run == runNumber ) return ipt;
1378  }
1379  return -1;
1380 }
1381 
1382 //________________________________________________________________________
1383 TGraphAsymmErrors* AliMTRChEffAnalysis::GetOutliers ( TGraphAsymmErrors* graph, Double_t maxNsigmas ) const
1384 {
1386 // TGraphAsymmErrors* outliers = new TGraphAsymmErrors();
1387  TGraphAsymmErrors* outliers = new TGraphAsymmErrors(*graph);
1388  outliers->SetHistogram(static_cast<TH1F*>(graph->GetHistogram()->Clone(Form("%s_outliers",graph->GetHistogram()->GetName()))));
1389 // outliers->SetLineColor(graph->GetLineColor()+1);
1390  if ( graph->GetListOfFunctions()->GetEntries() == 0 ) graph->Fit("pol0","Q0");
1391  TF1* func = static_cast<TF1*>(graph->GetListOfFunctions()->At(0));
1392 
1393  Double_t xpt, ypt;
1394  Int_t nremoved = 0;
1395  for ( Int_t ipt=0; ipt<graph->GetN(); ipt++ ) {
1396  graph->GetPoint(ipt,xpt,ypt);
1397  Double_t diff = ypt - func->Eval(xpt);
1398  Double_t err = ( diff > 0. ) ? graph->GetErrorYlow(ipt) : graph->GetErrorYhigh(ipt);
1399  if ( err < 0. || TMath::Abs(diff)/err > maxNsigmas ) continue;
1400  outliers->RemovePoint(ipt-nremoved);
1401  nremoved++;
1402 // outliers->SetPoint(iopt,xpt,ypt);
1403  }
1404  return outliers;
1405 }
1406 
1407 //________________________________________________________________________
1408 Int_t AliMTRChEffAnalysis::GetRunNumber ( Int_t ipt ) const
1409 {
1411  if ( ipt < 0 || ipt >= fOutputs->GetEntriesFast() ) return -1;
1412  return fOutputs->UncheckedAt(ipt)->GetUniqueID();
1413 }
1414 
1415 //________________________________________________________________________
1416 TList* AliMTRChEffAnalysis::GetRunList ( const char* runList ) const
1417 {
1419  TList* rl = new TList;
1420  rl->SetOwner();
1421  TString sRunList = gSystem->ExpandPathName(runList);
1422  if ( gSystem->AccessPathName(sRunList) || sRunList.EndsWith(".root") ) {
1423  sRunList.ReplaceAll(","," ");
1424  if ( sRunList.IsDigit() ) {
1425  TObjArray* arr = sRunList.Tokenize(" ");
1426  for ( Int_t iarr=0; iarr<arr->GetEntries(); iarr++ ) {
1427  rl->Add(new TObjString(arr->At(iarr)->GetName()));
1428  }
1429  delete arr;
1430  }
1431  }
1432  else {
1433  ifstream inFile(sRunList.Data());
1434  TString currLine = "";
1435  while ( ! inFile.eof() ) {
1436  currLine.ReadLine(inFile);
1437  TString currRun = AliAnalysisMuonUtility::GetRunNumberAsString(currLine);
1438  if ( ! currRun.IsNull() ) rl->Add(new TObjString(currRun));
1439  }
1440  inFile.close();
1441  }
1442  rl->Sort();
1443  return rl;
1444 }
1445 
1446 //________________________________________________________________________
1447 Bool_t AliMTRChEffAnalysis::GetShortConditionTitles ( AliTrigChEffOutput* trigOut, TObjArray& condTitles ) const
1448 {
1450 
1451  Int_t nConditions = fConditions->GetEntriesFast();
1452  TObjArray* refCondition = 0x0;
1453 
1454  for ( Int_t icond=0; icond<nConditions; icond++ ) {
1455  TObjArray* condition = static_cast<TObjArray*>(fConditions->UncheckedAt(icond));
1456  TString title = "";
1457  if ( icond == 0 ) {
1458  refCondition = condition;
1459  title = condition->GetName();
1460  }
1461  for ( Int_t ic=0; ic<condition->GetEntriesFast(); ic++ ) {
1462  TString currCond = static_cast<TObjString*>(condition->UncheckedAt(ic))->String();
1463  TString refCond = static_cast<TObjString*>(refCondition->UncheckedAt(ic))->String();
1464  if ( currCond == refCond ) continue;
1465  TString add = currCond;
1466  if ( ic == 3 ) add = trigOut->GetHistoName(-1,-1,-1,currCond.Atoi(),-1,-1);
1467  else if ( ic == 4 ) add = trigOut->GetHistoName(-1,-1,-1,-1,currCond.Atoi(),-1);
1468  else if ( ic == 5 ) add = trigOut->GetHistoName(-1,-1,-1,-1,-1,currCond.Atoi());
1469  title += Form("_%s",add.Data());
1470  }
1471  title.Remove(TString::kLeading,'_');
1472  title.ReplaceAll(",","|");
1473  condTitles.AddAt(new TObjString(title),icond);
1474  }
1475 
1476  return kTRUE;
1477 }
1478 
1479 //________________________________________________________________________
1480 TH1* AliMTRChEffAnalysis::GetSum ( AliTrigChEffOutput* trigOut, TObjArray* condition, Int_t itype, Int_t icount, Int_t ichamber ) const
1481 {
1483  Int_t itrackSel = static_cast<TObjString*>(condition->At(3))->String().Atoi();
1484  Int_t imatch = static_cast<TObjString*>(condition->At(4))->String().Atoi();
1485  Int_t imethod = static_cast<TObjString*>(condition->At(5))->String().Atoi();
1486  TString objName = trigOut->GetHistoName(itype, icount, ichamber, itrackSel, imatch, imethod);
1487  return static_cast<TH1*>(trigOut->GetSum(condition->At(0)->GetName(),condition->At(1)->GetName(),condition->At(2)->GetName(),objName));
1488 }
1489 
1490 //________________________________________________________________________
1491 TH1* AliMTRChEffAnalysis::GetTrend ( Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem ) const
1492 {
1494  if ( itype == AliTrigChEffOutput::kHchamberEff ) {
1495  if ( idetelem < 0 && ichamber >=0 ) idetelem = 11+ichamber;
1496  }
1497  TH1* outHisto = 0x0;
1498 
1499  TIter next(fOutputs);
1500  TList* effHistoList = 0x0;
1501  Int_t ibin = 0;
1502  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1503  ibin++;
1504 // TString identifier = GetIdentifier(trigOut,condition,itype,icount,ichamber);
1505  if ( ! outHisto ) {
1506 // TString outName = identifier;
1507  TString outName = Form("histo_type%i_count%i_ch%i_",itype,icount,11+ichamber);
1508 // outName.ReplaceAll("/","_");
1509  outName += Form("%i_trend",idetelem);
1510  outHisto = new TH1D(outName.Data(),outName.Data(),fOutputs->GetEntriesFast(),0.,(Double_t)fOutputs->GetEntriesFast());
1511  outHisto->SetDirectory(0);
1512  outHisto->GetXaxis()->SetTitle("Run num.");
1513  }
1514  UInt_t run = effHistoList->GetUniqueID();
1515  outHisto->GetXaxis()->SetBinLabel(ibin,Form("%u",run));
1516  TH1* histo = GetHisto(effHistoList,itype,icount,ichamber);
1517  Int_t currBin = histo->GetXaxis()->FindBin(idetelem);
1518  outHisto->SetBinContent(ibin,histo->GetBinContent(currBin));
1519  outHisto->SetBinError(ibin,histo->GetBinError(currBin));
1520  }
1521  if ( outHisto ) outHisto->GetXaxis()->LabelsOption("v");
1522  return outHisto;
1523 }
1524 
1525 //________________________________________________________________________
1526 TGraphAsymmErrors* AliMTRChEffAnalysis::GetTrendEff ( Int_t itype, Int_t icount, Int_t ichamber, Int_t idetelem ) const
1527 {
1529  if ( icount == AliTrigChEffOutput::kAllTracks ) {
1530  AliWarning("Chose either bending plane, non-bending plane or both planes");
1531  return NULL;
1532  }
1533  TH1* histoNum = GetTrend(itype,icount,ichamber,idetelem);
1534  TH1* histoDen = GetTrend(itype,AliTrigChEffOutput::kAllTracks,ichamber,idetelem);
1535  TGraphAsymmErrors* graph = new TGraphAsymmErrors(histoNum,histoDen,"e0");
1536  histoNum->Reset();
1537  histoNum->SetStats(kFALSE);
1538  graph->SetHistogram(static_cast<TH1F*>(histoNum));
1539  graph->GetYaxis()->SetTitle("Efficiency");
1540  graph->SetMarkerSize(0.5);
1541 // for ( Int_t ibin=1; ibin<=histoNum->GetXaxis()->GetNbins(); ibin++ ) {
1542 // graph->GetXaxis()->SetBinLabel(ibin,histoNum->GetXaxis()->GetBinLabel(ibin));
1543 // }
1544 // delete histoNum;
1545  delete histoDen;
1546  return graph;
1547 }
1548 
1549 //________________________________________________________________________
1550 Double_t AliMTRChEffAnalysis::GetThreeOfFour ( TArrayD eff, TArrayD effErr, Double_t &probErr ) const
1551 {
1553  Double_t binomialEff = 0.;
1554  Double_t sumErr2 = 0.;
1555  for ( Int_t jch=-1; jch<4; jch++ ) {
1556  Double_t prodEff = 1.;
1557  Double_t prodErr2 = 0.;
1558  for ( Int_t ich=0; ich<4; ich++ ) {
1559  Double_t currEff = ( ich == jch ) ? 1.-eff[ich] : eff[ich];
1560  prodEff *= currEff;
1561  Double_t relErr = ( currEff>0. ) ? effErr[ich]/currEff : 0.;
1562  prodErr2 += relErr*relErr;
1563  }
1564  binomialEff += prodEff;
1565  sumErr2 += prodEff*prodEff*prodErr2;
1566  }
1567  probErr = TMath::Sqrt(sumErr2);
1568  return binomialEff;
1569 }
1570 
1571 //________________________________________________________________________
1573 {
1575  if ( ! fMergedOutputs || fMergedOutputs->IsEmpty() ) {
1576  AliError("You first need to merge efficiency objects with MergeOutput");
1577  return kFALSE;
1578  }
1579  return kTRUE;
1580 }
1581 
1582 //________________________________________________________________________
1583 Bool_t AliMTRChEffAnalysis::MergeOutput ( TArrayI runRanges, Double_t averageStatError, Bool_t isIndex )
1584 {
1586 
1587  if ( runRanges.GetSize()%2 == 1 ) {
1588  AliError("Run ranges expected in the form: start_period1,end_period1,start_period2,end_period2... => even number expected");
1589  return kFALSE;
1590  }
1591 
1592  TArrayI mergedRanges = MergeRangesForStat(runRanges, averageStatError);
1593 
1594  Int_t nRanges = mergedRanges.GetSize()/2;
1595 
1596 
1597  if ( fMergedOutputs ) fMergedOutputs->Delete();
1598  else {
1599  fMergedOutputs = new TObjArray(nRanges);
1600  fMergedOutputs->SetOwner();
1601  }
1602 
1603  TList* effHistoList = 0x0;
1604  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1605  Int_t firstRun = mergedRanges[2*irange];
1606  Int_t lastRun = mergedRanges[2*irange+1];
1607  if ( isIndex ) {
1608  firstRun = GetRunNumber(firstRun);
1609  lastRun = GetRunNumber(lastRun);
1610  }
1611  UInt_t uFirstRun = (UInt_t)firstRun;
1612  UInt_t uLastRun = (UInt_t)lastRun;
1613 
1614  TString filename = "";
1615 
1616  TIter next(fOutputs);
1617  TFileMerger fileMerger;
1618  while ( (effHistoList = static_cast<TList*>(next()) ) ) {
1619  UInt_t run = effHistoList->GetUniqueID();
1620  if ( run < uFirstRun || run > uLastRun ) continue;
1621  filename = effHistoList->GetName();
1622  if ( firstRun == lastRun ) continue;
1623  fileMerger.AddFile(filename.Data(),kFALSE);
1624  }
1625 
1626  TString sRange = Form("%i_%i",firstRun,lastRun);
1627  TString mergedFilename = Form("mergedTrigEff_runs_%s.root",sRange.Data());
1628 
1629  if ( firstRun == lastRun ) TFile::Cp(filename.Data(),mergedFilename.Data(),kFALSE);
1630  else {
1631  fileMerger.OutputFile(mergedFilename.Data());
1632  fileMerger.Merge();
1633  }
1634  AliTrigChEffOutput* trigOut = new AliTrigChEffOutput(mergedFilename);
1635  trigOut->SetName(sRange.Data());
1636  fMergedOutputs->Add(trigOut);
1637  }
1638  return kTRUE;
1639 }
1640 
1641 //________________________________________________________________________
1642 TArrayI AliMTRChEffAnalysis::MergeRangesForStat ( TArrayI runRanges, Double_t averageStatError, Bool_t excludePeriphericBoards ) const
1643 {
1644  if ( averageStatError <= 0. || averageStatError >= 1. ) return runRanges;
1645 
1646  // FIXME: statstical error for binomial depends on the value of epsilon
1647  // for the moment let's assume an average efficiency of 0.9 (underestimated)
1648  // in the future one could maybe actually calculate it
1649  // (still we're working with average values...so the precision is limited)
1650  Double_t effForStatCalc = 0.9;
1651 
1652  Double_t averageStatNeeded = effForStatCalc*(1.-effForStatCalc)/(averageStatError*averageStatError);
1653 
1654  AliInfo(Form("Average statistics needed to reach precision of %g : %g",averageStatError,averageStatNeeded));
1655 
1656  Int_t nRanges = runRanges.GetSize()/2;
1657 
1658  TArrayD averageStat(nRanges);
1659  Double_t fullStat = 0.;
1660  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1661  averageStat[irange] = GetAverageStat(runRanges[2*irange],runRanges[2*irange+1],AliTrigChEffOutput::kHboardEff,excludePeriphericBoards);
1662  fullStat += averageStat[irange];
1663  }
1664 
1665  TArrayI mergedRanges(runRanges.GetSize());
1666  Int_t imerged = 0;
1667  mergedRanges[imerged++] = runRanges[0];
1668  Double_t mergedAverageStat = 0., remainingStat = fullStat;
1669  for ( Int_t irange=0; irange<nRanges; irange++ ) {
1670  Int_t istart = 2*irange;
1671  Int_t iend = istart+1;
1672  mergedAverageStat += averageStat[irange];
1673  remainingStat -= averageStat[irange];
1674 
1675  AliInfo(Form("%i - %i => stat %g",runRanges[2*irange],runRanges[2*irange+1],averageStat[irange]));
1676 
1677  if ( ( mergedAverageStat >= averageStatNeeded && remainingStat >= averageStatNeeded ) || iend == runRanges.GetSize()-1 ) {
1678  mergedRanges[imerged++] = runRanges[iend];
1679  AliInfo(Form(" merged range %i - %i => stat %g",mergedRanges[imerged-2],mergedRanges[imerged-1],mergedAverageStat));
1680  mergedAverageStat = 0.;
1681  Int_t nextRun = iend+1;
1682  if ( nextRun < runRanges.GetSize() ) mergedRanges[imerged++] = runRanges[nextRun];
1683  }
1684  }
1685  mergedRanges.Set(imerged);
1686  return mergedRanges;
1687 }
1688 
1689 //________________________________________________________________________
1690 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 )
1691 {
1693  if ( ! fConditions ) {
1694  fConditions = new TObjArray();
1695  fConditions->SetOwner();
1696  }
1697 
1698  TString sCondition = Form("%s;%s;%s;%i;%i;%i",physSel,trigClassName,centrality,itrackSel,imatch,imethod);
1699  TObjArray* foundCondition = static_cast<TObjArray*>(fConditions->FindObject(sCondition.Data()));
1700  TObjArray* basicCondition = static_cast<TObjArray*>(fConditions->At(0));
1701 
1702  TObjArray* addCondition = 0x0;
1703 
1704  if ( foundCondition ) {
1705  if ( isBasic ) {
1706  if ( foundCondition == basicCondition ) return kFALSE;
1707  else {
1708  fConditions->Remove(foundCondition);
1709  addCondition = foundCondition;
1710  }
1711  }
1712  else {
1713  AliInfo("Systematic condition already added");
1714  return kFALSE;
1715  }
1716  }
1717  else {
1718  addCondition = sCondition.Tokenize(";");
1719  addCondition->SetName(sCondition.Data());
1720  }
1721 
1722 
1723  if ( isBasic ) {
1724  if ( basicCondition ) {
1725  AliInfo(Form("Changing current eff. condition: %s\n",basicCondition->GetName()));
1726  fConditions->Remove(basicCondition);
1727  delete basicCondition;
1728  }
1729  fConditions->AddAt(addCondition,0);
1730  fConditions->Compress();
1731 
1732  // Update outputs
1733  if ( fOutputs ) {
1734  TObjArray* tmpOutputs = static_cast<TObjArray*>(fOutputs->Clone());
1735  fOutputs->Delete();
1736  TIter next(tmpOutputs);
1737  TList* effHistiList = 0x0;
1738  while ( (effHistiList = static_cast<TList*>(next())) ) {
1739  AddToList(effHistiList->GetName(), fOutputs->GetName());
1740  }
1741  delete tmpOutputs;
1742  }
1743  }
1744  else fConditions->Add(addCondition);
1745 
1746  return kTRUE;
1747 }
1748 
1749 //________________________________________________________________________
1751 {
1754 }
1755 
1756 
1757 //________________________________________________________________________
1758 Bool_t AliMTRChEffAnalysis::SetEffConditions ( const char* physSel, const char* trigClassName, const char* centrality, Int_t itrackSel, Int_t imatch, Int_t imethod )
1759 {
1761  return SetCondition(physSel, trigClassName, centrality, itrackSel, imatch, imethod, kTRUE);
1762 }
1763 
1764 //________________________________________________________________________
1765 Bool_t AliMTRChEffAnalysis::SetOutList ( const char *localFileList, const char *outputName )
1766 {
1768 
1770 
1771  TString filename(localFileList);
1772  gSystem->ExpandPathName(filename);
1773  if ( gSystem->AccessPathName(filename.Data()) ) {
1774  AliWarning(Form("Cannot find %s",filename.Data()));
1775  return kFALSE;
1776  }
1777  if ( filename.EndsWith(".root") ) return AddToList(filename.Data(),outputName);
1778 
1779  Bool_t isOk = kTRUE;
1780  ifstream inFile(filename.Data());
1781  TString currLine = "";
1782  while ( ! inFile.eof() ) {
1783  currLine.ReadLine(inFile);
1784  if ( currLine.IsNull() ) continue;
1785  if ( gSystem->AccessPathName(currLine.Data()) ) continue;
1786  if ( ! AddToList(currLine.Data(), outputName) ) isOk = kFALSE;
1787  }
1788  inFile.close();
1789 
1790  return isOk;
1791 }
1792 
1793 //________________________________________________________________________
1794 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 )
1795 {
1797  CopyLocally(runList,path,pattern,localFileList,outDir,directory);
1798  return SetOutList(localFileList,outputName);
1799 }
1800 
1801 //________________________________________________________________________
1802 Bool_t AliMTRChEffAnalysis::SetResultsFromWeb ( const char *runList, const char *path, const char* localFileList, const char* outDir, const char *directory, const char* outputName )
1803 {
1805  CopyLocally(runList,path,"",localFileList,outDir,directory);
1806  return SetOutList(localFileList,outputName);
1807 }
1808 
1809 //________________________________________________________________________
1810 Bool_t AliMTRChEffAnalysis::WriteMergedToOCDB ( const char* outputCDB ) const
1811 {
1813  if ( ! HasMergedResults() ) return kFALSE;
1814  TString outCDBdir(outputCDB);
1815  if ( outCDBdir.Contains("://") && ! outCDBdir.BeginsWith("local") ) {
1816  AliError("A local directory is required to store the information");
1817  return kFALSE;
1818  }
1819  outCDBdir.ReplaceAll("local://","");
1820  if ( gSystem->AccessPathName(outCDBdir.Data()) ) {
1821  if ( ! ExecCommand(Form("mkdir -p %s",outCDBdir.Data()), kTRUE) ) {
1822  AliError(Form("Failed to create directory %s",outCDBdir.Data()));
1823  return kFALSE;
1824  }
1825  }
1826  TString outCDB = outCDBdir;
1827  outCDB.Prepend("local://");
1828 
1829  AliCDBManager* mgr = AliCDBManager::Instance();
1830  if ( ! mgr->GetDefaultStorage() ) mgr->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
1831 
1832  TString trigEffCDBdir = "MUON/Calib/TriggerEfficiency";
1833 
1834  mgr->SetSpecificStorage(trigEffCDBdir.Data(),outCDB.Data());
1835 
1836  AliCDBStorage* specificStorage = mgr->GetSpecificStorage(trigEffCDBdir.Data());
1837 
1838  TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
1839 
1840  AliTrigChEffOutput* trigOut = 0x0;
1841  TIter next(fMergedOutputs);
1842  while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
1843  TList* effList = GetEffHistoList(trigOut,condition);
1844  TString runRange = trigOut->GetName();
1845 
1846 // // Write full merged output in file
1847 // TFile* file = TFile::Open(Form("mergedTrigEff_runs_%s.root",runRange.Data()),"RECREATE");
1848 // effList->Write("triggerChamberEff",TObject::kSingleKey);
1849 // trigOut->GetOutput()->Write(trigOut->GetOutput()->GetName(),TObject::kSingleKey);
1850 // file->Close();
1851 // delete file;
1852 
1853 
1854  // Write OCDB object
1855  TObjArray* currRange = runRange.Tokenize("_");
1856  Int_t firstRun = static_cast<TObjString*>(currRange->UncheckedAt(0))->String().Atoi();
1857  Int_t lastRun = static_cast<TObjString*>(currRange->UncheckedAt(1))->String().Atoi();
1858  delete currRange;
1859 
1860  // If an object is already there, ask to remove it or keep it
1861  for ( Int_t irun=0; irun<2; irun++ ) {
1862  Int_t runnr = ( irun == 0 ) ? firstRun : lastRun;
1863  specificStorage->QueryCDB(runnr);
1864  TObjArray* allIdsForRun = specificStorage->GetQueryCDBList();
1865  TIter nextId(allIdsForRun);
1866  AliCDBId* id = 0x0;
1867  while ((id = dynamic_cast<AliCDBId*>(nextId()))) {
1868  TString path(id->GetPath());
1869  Int_t foundFirst = id->GetFirstRun();
1870  Int_t foundLast = id->GetLastRun();
1871  Int_t version = id->GetVersion();
1872  Int_t subversion = id->GetSubVersion();
1873  TString locPath = Form("%s/%s/Run%d_%d_v%d_s%d.root",outCDBdir.Data(),path.Data(),foundFirst,foundLast,version,subversion);
1874  if ( ! gSystem->AccessPathName(locPath.Data()) ) {
1875  ExecCommand(Form("rm %s",locPath.Data()), kTRUE);
1876  }
1877  }
1878  }
1879 
1880  // Save the CDB object in the specific storage
1881  AliMUONTriggerEfficiencyCells* effMap = new AliMUONTriggerEfficiencyCells(effList);
1882  AliMUONCDB::WriteToCDB(effMap, "MUON/Calib/TriggerEfficiency", firstRun, lastRun, "Measured efficiencies");
1883  delete effMap; // CAVEAT: effMap is owner of efflist
1884  }
1885  return kTRUE;
1886 }
1887 
1888 //___________________________________________________________________________
1889 //Bool_t AliMTRChEffAnalysis::WriteMerged() const
1890 //{
1891 // /// Write merged efficiencies on file
1892 // if ( ! HasMergedResults ) return kFALSE;
1893 //
1894 // TObjArray* condition = static_cast<TObjArray*>(fConditions->At(0));
1895 //
1896 // AliTrigChEffOutput* trigOut = 0x0;
1897 // TIter next(fMergedOutputs);
1898 // while ( (trigOut = static_cast<AliTrigChEffOutput*>(next())) ) {
1899 // TFile* file = TFile::Open(Form("mergedTrigEff_runs_%s.root",trigOut->GetName()),"RECREATE");
1900 // TList* effList = GetEffHistoList(trigOut,condition);
1901 // effList->Write("triggerChamberEff",TObject::kSingleKey);
1902 // trigOut->GetOutput()->Write(trigOut->GetOutput()->GetName(),TObject::kSingleKey);
1903 // file->Close();
1904 // delete file;
1905 // delete effList;
1906 // }
1907 // return kTRUE;
1908 //}
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
Int_t CompareEfficiencyMethods(const char *source, const char *opt, const char *canvasNameSuffix="") const
Output for Trig chamber effieincy.
TArrayI GetHomogeneousRanges(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)
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)
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
Bool_t GetShortConditionTitles(AliTrigChEffOutput *trigOut, TObjArray &condTitles) 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