]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/MUON/lite/MakeTrend.C
Do not ask for physics selection in MC QA trending
[u/mrichter/AliRoot.git] / PWGPP / MUON / lite / MakeTrend.C
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
14 // Aliroot includes
15 #include "AliAnalysisManager.h"
16 #include "AliAnalysisAlien.h"
17 #include "AliESDInputHandler.h"
18
19 #define COMPILEMACRO
20
21 #endif
22
23
24 //_____________________________________________________________________________
25 void LoadLibs()
26 {
27   gSystem->Load("libTree");
28   gSystem->Load("libGeom");
29   gSystem->Load("libVMC");
30   gSystem->Load("libPhysics");
31   gSystem->Load("libProof");
32
33   gSystem->Load("libANALYSIS.so");
34   gSystem->Load("libOADB.so");
35   gSystem->Load("libANALYSISalice.so");
36   gSystem->Load("libCORRFW.so");
37   gSystem->Load("libPWGmuon.so");
38   gSystem->Load("libPWGPPMUONlite.so");
39 }
40
41
42 //_____________________________________________________________________________
43 AliAnalysisAlien* CreateAlienHandler()
44 {
45   AliAnalysisAlien *plugin = new AliAnalysisAlien();
46
47   // Set the run mode
48   plugin->SetRunMode("terminate");
49
50   // Declare all libraries
51   plugin->SetAdditionalLibs("libCORRFW.so libPWGHFbase.so libPWGmuon.so libPWGPPMUONlite.so");
52
53   plugin->SetAdditionalRootLibs("libXMLParser.so libGui.so libProofPlayer.so");
54
55   plugin->AddIncludePath("-I.");
56   plugin->AddIncludePath("-I$ALICE_ROOT/PWGPP/MUON/lite");
57
58   return plugin;
59 }
60
61 enum {
62   trackQA = 1 << 0,
63   trigQA  = 1 << 1
64 };
65
66 //_____________________________________________________________________________
67 void terminateQA ( TString outfilename = "QAresults.root", Bool_t isMC = kFALSE, UInt_t force = 0, UInt_t mask = (trackQA|trigQA) )
68 {
69   //
70   // Load common libraries
71   //
72
73   LoadLibs();
74
75   AliAnalysisAlien* alienHandler = CreateAlienHandler();
76
77   AliAnalysisManager* mgr = new AliAnalysisManager("testAnalysis");
78   mgr->SetCommonFileName(outfilename.Data());
79   mgr->SetGridHandler(alienHandler);
80
81   // Needed to the manager (but not used in terminate mode)
82   AliESDInputHandler* esdH = new AliESDInputHandler();
83   esdH->SetReadFriends(kFALSE);
84   mgr->SetInputEventHandler(esdH);
85
86   TString trigOutName = "trigChEff_ANY_Apt_allTrig.root";
87   if ( ( force & trigQA ) == 0 ) {
88     if ( gSystem->AccessPathName(trigOutName) == 0 ) {
89       printf("Terminate already done for trigger. Skip\n");
90       mask &= ~trigQA;
91     }
92   }
93   if ( ( force & trackQA ) == 0 ) {
94     TFile* file = TFile::Open(outfilename.Data());
95     TKey* key = file->FindKeyAny("general2");
96     if ( key ) {
97       printf("Terminate already done for tracker. Skip\n");
98       mask &= ~trackQA;
99     }
100     delete file;
101   }
102
103 #ifndef COMPILEMACRO
104
105   if ( mask & trigQA ) {
106     gROOT->LoadMacro("$ALICE_ROOT/PWGPP/macros/AddTaskMTRchamberEfficiency.C");
107     AliAnalysisTaskTrigChEff* trigChEffTask = AddTaskMTRchamberEfficiency(isMC);
108     TString physSelName = "PhysSelPass";
109     if ( isMC ) physSelName += ",PhysSelReject";
110     trigChEffTask->SetTerminateOptions(physSelName,"ANY","-5_105",Form("FORCEBATCH NoSelMatchApt FromTrg %s?%s?ANY?-5_105?NoSelMatchAptFromTrg",trigOutName.Data(),physSelName.Data()));
111   }
112   if ( mask & trackQA ) {
113     gROOT->LoadMacro("$ALICE_ROOT/PWGPP/PilotTrain/AddTaskMuonQA.C");
114     Bool_t selectPhysics = ( isMC ) ? kFALSE : kTRUE;
115     AliAnalysisTaskMuonQA* muonQATask = AddTaskMuonQA(selectPhysics);
116   }
117
118 #endif
119
120 //  // Check if terminate was already performed
121 //  if  ( ! force ) {
122 //    TObject* paramContainer = mgr->GetParamOutputs()->At(0);
123 //    if ( paramContainer ) {
124 //      TFile* file = TFile::Open(outfilename);
125 //      if ( file->FindObjectAny(paramContainer->GetName() ) ) {
126 //        printf("\nTerminate was already executed!\n");
127 //        printf("Nothing to be done\n");
128 //        file->Close();
129 //        return;
130 //      }
131 //      file->Close();
132 //    }
133 //  }
134
135
136   if ( ! mgr->InitAnalysis()) {
137     printf("Fatal: Cannot initialize analysis\n");
138     return;
139   }
140   mgr->PrintStatus();
141   mgr->StartAnalysis("grid terminate");
142 }
143
144
145 //_____________________________________________________________________________
146 TString GetFullPath ( TString filename )
147 {
148   if ( filename.BeginsWith("alien://") ) return filename;
149   TString dirName = gSystem->DirName(filename);
150   TString baseName = gSystem->BaseName(filename);
151   TString currDir = gSystem->pwd();
152   gSystem->cd(dirName);
153   TString fullDir = gSystem->pwd();
154   gSystem->cd(currDir);
155   TString fullPath = fullDir.Data();
156   if ( ! fullDir.EndsWith("/") ) fullPath.Append("/");
157   fullPath += baseName;
158   return fullPath;
159 }
160
161 //_____________________________________________________________________________
162 TString GetBaseName ( TString filename )
163 {
164   TString baseName = gSystem->BaseName(filename);
165   Int_t idx = baseName.Index("#");
166   if ( idx > 0 ) baseName.Remove(0,idx+1);
167   return baseName;
168 }
169
170
171 //_____________________________________________________________________________
172 void CopyDir(TDirectory *source) {
173   //copy all objects and subdirs of directory source as a subdir of the current directory
174   TDirectory *savdir = gDirectory;
175   TDirectory *adir = savdir->mkdir(source->GetName());
176   adir->cd();
177   //loop on all entries of this directory
178   TKey *key;
179   TIter nextkey(source->GetListOfKeys());
180   while ((key = (TKey*)nextkey())) {
181     const char *classname = key->GetClassName();
182     TClass *cl = gROOT->GetClass(classname);
183     if (!cl) continue;
184     if (cl->InheritsFrom(TDirectory::Class())) {
185       source->cd(key->GetName());
186       TDirectory *subdir = gDirectory;
187       adir->cd();
188       CopyDir(subdir);
189       adir->cd();
190     } else if (cl->InheritsFrom(TTree::Class())) {
191       TTree *T = (TTree*)source->Get(key->GetName());
192       adir->cd();
193       TTree *newT = T->CloneTree(-1,"fast");
194       newT->Write();
195     } else {
196       source->cd();
197       TObject *obj = key->ReadObj();
198       adir->cd();
199       obj->Write(obj->GetName(),TObject::kSingleKey);
200       delete obj;
201     }
202   }
203   adir->SaveSelf(kTRUE);
204   savdir->cd();
205 }
206
207
208 //_____________________________________________________________________________
209 Bool_t GetQAInfo ( const char* qaFileName, TString dirNames = "MUON_QA MTR_ChamberEffMap MUON.TrigEfficiencyMap MUON.TriggerEfficiencyMap" )
210 {
211   LoadLibs();
212
213   TString outFilename = GetBaseName(qaFileName);
214   TString inFullPath = GetFullPath(qaFileName);
215   TString outFullPath = GetFullPath(outFilename);
216   if ( inFullPath == outFullPath ) {
217     printf("Warning: input and output are same file!\n");
218     return kFALSE;
219   }
220
221   if ( inFullPath.BeginsWith("alien") && ! gGrid ) TGrid::Connect("alien://");
222
223   TObjArray* dirList = dirNames.Tokenize(" ");
224   TFile* outFile = TFile::Open(outFilename,"RECREATE");
225   TFile* inFile = TFile::Open(qaFileName);
226   for ( Int_t idir=0; idir<dirList->GetEntries(); idir++ ) {
227     inFile->cd();
228     TObject* obj = inFile->Get(dirList->At(idir)->GetName());
229     if ( ! obj ) continue;
230     outFile->cd();
231     CopyDir(static_cast<TDirectory*>(obj));
232   }
233   delete outFile;
234   delete inFile;
235   delete dirList;
236
237   return kTRUE;
238 }
239
240
241 //_____________________________________________________________________________
242 Bool_t AddTreeVariable ( TList& parList, const char* varName, char varType, Float_t val )
243 {
244   if ( varType == 'D' ) varType = 'F';
245   TString parName = Form("%s/%c",varName,varType);
246   if ( varType == 'F' ) {
247     parList.Add(new TParameter<float>(parName,val));
248   }
249   else if ( varType == 'I' ) {
250     parList.Add(new TParameter<int>(parName,(Int_t)val));
251   }
252   else {
253     printf("Error: variable type %c not accepted", varType);
254     return kFALSE;
255   }
256   return kTRUE;
257 }
258
259
260 //_____________________________________________________________________________
261 void FillTree ( TTree* tree, TList &parList )
262 {
263   Int_t nVars = parList.GetEntries();
264   TArrayI varInt(nVars);
265   TArrayF varFloat(nVars);
266   for ( Int_t ivar=0; ivar<nVars; ivar++ ) {
267     TObject* obj = parList.At(ivar);
268     TString varName = obj->GetName();
269     TString branchName = varName;
270     branchName.Remove(varName.Length()-2);
271     if ( varName.EndsWith("F") ) {
272       varFloat[ivar] = ((TParameter<float>*)obj)->GetVal();
273       tree->Branch(branchName.Data(),&varFloat[ivar],varName.Data());
274     }
275     else if ( varName.EndsWith("I") ) {
276       varInt[ivar] = (Int_t)((TParameter<int>*)obj)->GetVal();
277       tree->Branch(branchName.Data(),&varInt[ivar],varName.Data());
278     }
279   }
280   tree->Fill();
281 }
282
283
284 //_____________________________________________________________________________
285 void AddTrigVars ( TString filename, TList &parList )
286 {
287   TString trigOutName = "trigChEff_ANY_Apt_allTrig.root";
288   if ( gSystem->AccessPathName(trigOutName.Data()) ) trigOutName = filename;
289   TFile* file = TFile::Open(filename.Data());
290   TList* inList = (TList*)file->FindObjectAny("triggerChamberEff");
291   TString hChNames[] = {"bendPlaneCountChamber","nonBendPlaneCountChamber","allTracksCountChamber"};
292   Int_t nHistos = sizeof(hChNames)/sizeof(hChNames[0]);
293   for ( Int_t ihisto=0; ihisto<nHistos; ihisto++ ) {
294     TH1* histo = (TH1*)inList->FindObject(hChNames[ihisto].Data());
295     for ( Int_t ibin=1; ibin<=4; ibin++ ) {
296       Double_t currVal = ( histo ) ? histo->GetBinContent(ibin) : 0.;
297       AddTreeVariable(parList, Form("%s%i",hChNames[ihisto].Data(),ibin),'F',currVal);
298     }
299   }
300   delete file;
301 }
302
303 //_____________________________________________________________________________
304 void MakeTrend ( const char* qaFile, Int_t runNumber, Bool_t isMC = kFALSE, UInt_t force = trigQA, UInt_t mask = (trackQA|trigQA) )
305 {
306   Bool_t isOk = GetQAInfo(qaFile);
307   if ( ! isOk ) return;
308
309   TString inFilename = GetBaseName(qaFile);
310
311   terminateQA(inFilename,isMC,force,mask);
312
313   TList parList;
314   parList.SetOwner();
315   AddTreeVariable(parList, "run", 'I', runNumber);
316
317   // function for trigger
318   AddTrigVars(inFilename.Data(),parList);
319
320   TFile* outFile = TFile::Open("trending.root","RECREATE");
321   TTree* tree = new TTree("trending","trending");
322
323   FillTree(tree, parList);
324   tree->Write();
325   delete outFile;
326 }