AliPhysics  vAN-20151012 (2287573)
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros
MakeTrend.C
Go to the documentation of this file.
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 // ROOT includes
3 #include "TFile.h"
4 #include "TGrid.h"
5 #include "TString.h"
6 #include "TObjArray.h"
7 #include "TObjString.h"
8 #include "TSystem.h"
9 #include "TROOT.h"
10 #include "TKey.h"
11 #include "TTree.h"
12 #include "TParameter.h"
13 #include "TFileMerger.h"
14 #include "THashList.h"
15 
16 // Aliroot includes
17 #include "AliAnalysisManager.h"
18 #include "AliAnalysisAlien.h"
19 #include "AliESDInputHandler.h"
20 #include "AliCounterCollection.h"
21 
22 #define COMPILEMACRO
23 
24 #endif
25 
26 
27 //_____________________________________________________________________________
28 void LoadLibs()
29 {
30 // gSystem->Load("libTree");
31 // gSystem->Load("libGeom");
32 // gSystem->Load("libVMC");
33 // gSystem->Load("libPhysics");
34 // gSystem->Load("libProof");
35 //
36 // gSystem->Load("libANALYSIS");
37 // gSystem->Load("libOADB");
38 // gSystem->Load("libANALYSISalice");
39 // gSystem->Load("libCORRFW");
40 // gSystem->Load("libPWGmuon");
41  TString libName = "libPWGPPMUONlite";
42  TString getLib = gSystem->GetLibraries(libName.Data(),"",kFALSE);
43  if ( getLib.IsNull() ) gSystem->Load(libName.Data());
44 }
45 
46 
47 //_____________________________________________________________________________
48 AliAnalysisAlien* CreateAlienHandler()
49 {
50  AliAnalysisAlien *plugin = new AliAnalysisAlien();
51 
52  // Set the run mode
53  plugin->SetRunMode("terminate");
54 
55  // Declare all libraries
56  plugin->SetAdditionalLibs("libCORRFW.so libPWGHFbase.so libPWGmuon.so libPWGPPMUONlite.so");
57 
58  plugin->SetAdditionalRootLibs("libXMLParser.so libGui.so libProofPlayer.so");
59 
60  plugin->AddIncludePath("-I.");
61  plugin->AddIncludePath("-I$ALICE_PHYSICS/PWGPP/MUON/lite");
62 
63  return plugin;
64 }
65 
66 enum {
67  trackQA = 1 << 0,
68  trigQA = 1 << 1
69 };
70 
71 //_____________________________________________________________________________
72 void terminateQA ( TString outfilename = "QAresults.root", Bool_t isMC = kFALSE, Bool_t usePhysicsSelection = kTRUE, UInt_t mask = (trackQA|trigQA), UInt_t force = (trackQA|trigQA) )
73 {
74  //
75  // Run terminate on QA output
76  // Terminate is skipped if it was already run during the production
77  // Unless the "force" option is specified
78  //
79 
80  LoadLibs();
81 
82  AliAnalysisAlien* alienHandler = CreateAlienHandler();
83 
84  AliAnalysisManager* mgr = new AliAnalysisManager("testAnalysis");
85  mgr->SetCommonFileName(outfilename.Data());
86  mgr->SetGridHandler(alienHandler);
87 
88  // Needed to the manager (but not used in terminate mode)
89  AliESDInputHandler* esdH = new AliESDInputHandler();
90  esdH->SetReadFriends(kFALSE);
91  mgr->SetInputEventHandler(esdH);
92 
93  TString trigOutName = "trigChEff_ANY_Apt_allTrig.root";
94  if ( ( force & trigQA ) == 0 ) {
95  if ( gSystem->AccessPathName(trigOutName) == 0 ) {
96  printf("Terminate already done for trigger. Skip\n");
97  mask &= ~trigQA;
98  }
99  }
100  if ( ( force & trackQA ) == 0 ) {
101  TFile* file = TFile::Open(outfilename.Data());
102  TKey* key = file->FindKeyAny("general2");
103  if ( key ) {
104  printf("Terminate already done for tracker. Skip\n");
105  mask &= ~trackQA;
106  }
107  delete file;
108  }
109 
110 #ifndef COMPILEMACRO
111 
112  if ( mask & trigQA ) {
113  gROOT->LoadMacro("$ALICE_PHYSICS/PWGPP/macros/AddTaskMTRchamberEfficiency.C");
114  AliAnalysisTaskTrigChEff* trigChEffTask = AddTaskMTRchamberEfficiency(isMC);
115  TString physSelName = "PhysSelPass";
116  if ( ! usePhysicsSelection ) physSelName += ",PhysSelReject";
117  trigChEffTask->SetTerminateOptions(physSelName,"ANY","-5_105",Form("FORCEBATCH NoSelMatchApt FromTrg %s?%s?ANY?-5_105?NoSelMatchAptFromTrg",trigOutName.Data(),physSelName.Data()));
118  }
119  if ( mask & trackQA ) {
120  gROOT->LoadMacro("$ALICE_PHYSICS/PWGPP/PilotTrain/AddTaskMuonQA.C");
121  AliAnalysisTaskMuonQA* muonQATask = AddTaskMuonQA(usePhysicsSelection);
122  }
123 
124 #endif
125 
126 // // Check if terminate was already performed
127 // if ( ! force ) {
128 // TObject* paramContainer = mgr->GetParamOutputs()->At(0);
129 // if ( paramContainer ) {
130 // TFile* file = TFile::Open(outfilename);
131 // if ( file->FindObjectAny(paramContainer->GetName() ) ) {
132 // printf("\nTerminate was already executed!\n");
133 // printf("Nothing to be done\n");
134 // file->Close();
135 // return;
136 // }
137 // file->Close();
138 // }
139 // }
140 
141 
142  if ( ! mgr->InitAnalysis()) {
143  printf("Fatal: Cannot initialize analysis\n");
144  return;
145  }
146  mgr->PrintStatus();
147  mgr->StartAnalysis("grid terminate");
148 }
149 
150 
151 //_____________________________________________________________________________
152 TString GetFullPath ( TString filename )
153 {
154  if ( filename.BeginsWith("alien://") ) return filename;
155  TString dirName = gSystem->DirName(filename);
156  TString baseName = gSystem->BaseName(filename);
157  TString currDir = gSystem->pwd();
158  gSystem->cd(dirName);
159  TString fullDir = gSystem->pwd();
160  gSystem->cd(currDir);
161  TString fullPath = fullDir.Data();
162  if ( ! fullDir.EndsWith("/") ) fullPath.Append("/");
163  fullPath += baseName;
164  return fullPath;
165 }
166 
167 //_____________________________________________________________________________
168 TString GetBaseName ( TString filename )
169 {
170  TString baseName = gSystem->BaseName(filename);
171  Int_t idx = baseName.Index("#");
172  if ( idx > 0 ) baseName.Remove(0,idx+1);
173  return baseName;
174 }
175 
176 
177 //_____________________________________________________________________________
178 void CopyDir(TDirectory *source) {
179  //copy all objects and subdirs of directory source as a subdir of the current directory
180  TDirectory *savdir = gDirectory;
181  TDirectory *adir = savdir->mkdir(source->GetName());
182  adir->cd();
183  //loop on all entries of this directory
184  TKey *key;
185  TIter nextkey(source->GetListOfKeys());
186  while ((key = (TKey*)nextkey())) {
187  const char *classname = key->GetClassName();
188  TClass *cl = gROOT->GetClass(classname);
189  if (!cl) continue;
190  if (cl->InheritsFrom(TDirectory::Class())) {
191  source->cd(key->GetName());
192  TDirectory *subdir = gDirectory;
193  adir->cd();
194  CopyDir(subdir);
195  adir->cd();
196  } else if (cl->InheritsFrom(TTree::Class())) {
197  TTree *T = (TTree*)source->Get(key->GetName());
198  adir->cd();
199  TTree *newT = T->CloneTree(-1,"fast");
200  newT->Write();
201  } else {
202  source->cd();
203  TObject *obj = key->ReadObj();
204  adir->cd();
205  obj->Write(obj->GetName(),TObject::kSingleKey);
206  delete obj;
207  }
208  }
209  adir->SaveSelf(kTRUE);
210  savdir->cd();
211 }
212 
213 
214 //_____________________________________________________________________________
215 Bool_t GetQAInfo ( const char* qaFileName, TString dirNames = "MUON_QA MTR_ChamberEffMap MUON.TrigEfficiencyMap MUON.TriggerEfficiencyMap" )
216 {
217  LoadLibs();
218 
219  TString outFilename = GetBaseName(qaFileName);
220  TString inFullPath = GetFullPath(qaFileName);
221  TString outFullPath = GetFullPath(outFilename);
222  if ( inFullPath == outFullPath ) {
223  printf("Warning: input and output are same file!\n");
224  return kFALSE;
225  }
226 
227  if ( inFullPath.BeginsWith("alien") && ! gGrid ) TGrid::Connect("alien://");
228 
229  TObjArray* dirList = dirNames.Tokenize(" ");
230  TFile* outFile = TFile::Open(outFilename,"RECREATE");
231  TFile* inFile = TFile::Open(qaFileName);
232  for ( Int_t idir=0; idir<dirList->GetEntries(); idir++ ) {
233  inFile->cd();
234  TObject* obj = inFile->Get(dirList->At(idir)->GetName());
235  if ( ! obj ) continue;
236  outFile->cd();
237  CopyDir(static_cast<TDirectory*>(obj));
238  }
239  delete outFile;
240  delete inFile;
241  delete dirList;
242 
243  return kTRUE;
244 }
245 
246 
247 //_____________________________________________________________________________
248 Bool_t CheckMergedOverlap ( TString fileList )
249 {
250  LoadLibs();
251  TObjArray* arr = fileList.Tokenize(" ");
252  THashList triggerList;
253  triggerList.SetOwner();
254  Bool_t hasOverlap = kFALSE;
255  for ( Int_t iarr=0; iarr<arr->GetEntries(); iarr++ ) {
256  TFile* file = TFile::Open(arr->At(iarr)->GetName());
257  AliCounterCollection* eventCounters = (AliCounterCollection*)file->FindObjectAny("eventCounters");
258  if ( eventCounters ) {
259  TString listFromContainer = eventCounters->GetKeyWords("trigger");
260  TObjArray* trigArr = listFromContainer.Tokenize(",");
261  for ( Int_t itrig=0; itrig<trigArr->GetEntries(); itrig++ ) {
262  TString currTrig = trigArr->At(itrig)->GetName();
263  if ( triggerList.FindObject(currTrig.Data()) ) {
264  if ( currTrig != "ANY" ) {
265  printf("Warning: duplicated trigger %s\n", currTrig.Data());
266  hasOverlap = kTRUE;
267  }
268  }
269  else triggerList.Add(new TObjString(currTrig));
270  }
271  delete trigArr;
272  }
273  delete file;
274  }
275  delete arr;
276 
277  return hasOverlap;
278 }
279 
280 //_____________________________________________________________________________
281 Bool_t GetMergedQAInfo ( TString fileList, TString outFilename = "QAresults.root" )
282 {
283  LoadLibs();
284  TObjArray* arr = fileList.Tokenize(" ");
285  TFileMerger fm;
286  fm.OutputFile(outFilename.Data());
287  for ( Int_t iarr=0; iarr<arr->GetEntries(); iarr++ ) {
288  fm.AddFile(arr->At(iarr)->GetName());
289  }
290  delete arr;
291  return fm.Merge();
292 }
293 
294 //_____________________________________________________________________________
295 Bool_t AddTreeVariable ( TList& parList, const char* varName, char varType, Float_t val )
296 {
297  if ( varType == 'D' ) varType = 'F';
298  TString parName = Form("%s/%c",varName,varType);
299  if ( varType == 'F' ) {
300  parList.Add(new TParameter<float>(parName,val));
301  }
302  else if ( varType == 'I' ) {
303  parList.Add(new TParameter<int>(parName,(Int_t)val));
304  }
305  else {
306  printf("Error: variable type %c not accepted", varType);
307  return kFALSE;
308  }
309  return kTRUE;
310 }
311 
312 
313 //_____________________________________________________________________________
314 void FillTree ( TTree* tree, TList &parList )
315 {
316  Int_t nVars = parList.GetEntries();
317  TArrayI varInt(nVars);
318  TArrayF varFloat(nVars);
319  for ( Int_t ivar=0; ivar<nVars; ivar++ ) {
320  TObject* obj = parList.At(ivar);
321  TString varName = obj->GetName();
322  TString branchName = varName;
323  branchName.Remove(varName.Length()-2);
324  if ( varName.EndsWith("F") ) {
325  varFloat[ivar] = ((TParameter<float>*)obj)->GetVal();
326  tree->Branch(branchName.Data(),&varFloat[ivar],varName.Data());
327  }
328  else if ( varName.EndsWith("I") ) {
329  varInt[ivar] = (Int_t)((TParameter<int>*)obj)->GetVal();
330  tree->Branch(branchName.Data(),&varInt[ivar],varName.Data());
331  }
332  }
333  tree->Fill();
334 }
335 
336 
337 //_____________________________________________________________________________
338 void AddTrigVars ( TString filename, TList &parList )
339 {
340  TString trigOutName = "trigChEff_ANY_Apt_allTrig.root";
341  if ( gSystem->AccessPathName(trigOutName.Data()) ) trigOutName = filename;
342  TFile* file = TFile::Open(filename.Data());
343  TList* inList = (TList*)file->FindObjectAny("triggerChamberEff");
344  TString hChNames[] = {"bendPlaneCountChamber","nonBendPlaneCountChamber","allTracksCountChamber"};
345  Int_t nHistos = sizeof(hChNames)/sizeof(hChNames[0]);
346  for ( Int_t ihisto=0; ihisto<nHistos; ihisto++ ) {
347  TH1* histo = (TH1*)inList->FindObject(hChNames[ihisto].Data());
348  for ( Int_t ibin=1; ibin<=4; ibin++ ) {
349  Double_t currVal = ( histo ) ? histo->GetBinContent(ibin) : 0.;
350  AddTreeVariable(parList, Form("%s%i",hChNames[ihisto].Data(),ibin),'F',currVal);
351  }
352  }
353  delete file;
354 }
355 
356 //_____________________________________________________________________________
357 void MakeTrend ( const char* qaFile, Int_t runNumber, Bool_t isMC = kFALSE, Bool_t usePhysicsSelection = kTRUE, UInt_t mask = (trackQA|trigQA) )
358 {
359  Bool_t isOk = GetQAInfo(qaFile);
360  if ( ! isOk ) return;
361 
362  TString inFilename = GetBaseName(qaFile);
363 
364  UInt_t forceTerminate = 0;
365  if ( inFilename.Contains("barrel") ) {
366  TString outerInFilename(qaFile);
367  outerInFilename.ReplaceAll("barrel","outer");
368  isOk = GetQAInfo(outerInFilename);
369  if ( isOk ) {
370  // Merge outer and barrel
371  TString fileList = GetBaseName(outerInFilename);
372  fileList += " " + inFilename;
373  Bool_t isMergedOk = GetMergedQAInfo(fileList);
374  if ( isMergedOk ) {
375  inFilename = "QAresults.root";
376  printf("Merged files: %s => %s\n",fileList.Data(),inFilename.Data());
377  CheckMergedOverlap(fileList);
378  gSystem->Exec(Form("rm %s",fileList.Data())); // Remove QAresults_barrel and outer
379  forceTerminate = (trackQA|trigQA); // Re-do terminate when merging barrel and outer
380  }
381  }
382  }
383 
384  terminateQA(inFilename,isMC,usePhysicsSelection,mask,forceTerminate);
385 
386  TList parList;
387  parList.SetOwner();
388  AddTreeVariable(parList, "run", 'I', runNumber);
389 
390  // function for trigger
391  AddTrigVars(inFilename.Data(),parList);
392 
393  TFile* outFile = TFile::Open("trending.root","RECREATE");
394  TTree* tree = new TTree("trending","trending");
395 
396  FillTree(tree, parList);
397  tree->Write();
398  delete outFile;
399 }
TString GetFullPath(TString filename)
Definition: MakeTrend.C:152
void AddTrigVars(TString filename, TList &parList)
Definition: MakeTrend.C:338
void CopyDir(TDirectory *source)
Definition: MakeTrend.C:178
void terminateQA(TString outfilename="QAresults.root", Bool_t isMC=kFALSE, Bool_t usePhysicsSelection=kTRUE, UInt_t mask=(trackQA|trigQA), UInt_t force=(trackQA|trigQA))
Definition: MakeTrend.C:72
void LoadLibs()
Definition: MakeTrend.C:28
TSystem * gSystem
Quality assurance of MUON ESDs.
void FillTree(TTree *tree, TList &parList)
Definition: MakeTrend.C:314
TString GetBaseName(TString filename)
Definition: MakeTrend.C:168
AliAnalysisAlien * CreateAlienHandler()
Definition: MakeTrend.C:48
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)
Bool_t CheckMergedOverlap(TString fileList)
Definition: MakeTrend.C:248
Bool_t isMC
TFile * file
Bool_t AddTreeVariable(TList &parList, const char *varName, char varType, Float_t val)
Definition: MakeTrend.C:295
Bool_t GetMergedQAInfo(TString fileList, TString outFilename="QAresults.root")
Definition: MakeTrend.C:281
void MakeTrend(const char *qaFile, Int_t runNumber, Bool_t isMC=kFALSE, Bool_t usePhysicsSelection=kTRUE, UInt_t mask=(trackQA|trigQA))
Definition: MakeTrend.C:357
Bool_t GetQAInfo(const char *qaFileName, TString dirNames="MUON_QA MTR_ChamberEffMap MUON.TrigEfficiencyMap MUON.TriggerEfficiencyMap")
Definition: MakeTrend.C:215