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