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