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