]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliAODTagCreator.cxx
- Some restructering of the code
[u/mrichter/AliRoot.git] / STEER / AliAODTagCreator.cxx
1 /**************************************************************************
2  * Author: Panos Christakoglou.                                           *
3  * Contributors are mentioned in the code where appropriate.              *
4  *                                                                        *
5  * Permission to use, copy, modify and distribute this software and its   *
6  * documentation strictly for non-commercial purposes is hereby granted   *
7  * without fee, provided that the above copyright notice appears in all   *
8  * copies and that both the copyright notice and this permission notice   *
9  * appear in the supporting documentation. The authors make no claims     *
10  * about the suitability of this software for any purpose. It is          *
11  * provided "as is" without express or implied warranty.                  *
12  **************************************************************************/
13
14 /* $Id$ */
15
16 //-----------------------------------------------------------------
17 //           AliAODTagCreator class
18 //   This is the class to deal with the tag creation (post process)
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <Riostream.h>
24 #include <TFile.h>
25 #include <TString.h>
26 #include <TTree.h>
27 #include <TSystem.h>
28 #include <TChain.h>
29 #include <TLorentzVector.h>
30
31 //ROOT-AliEn
32 #include <TGrid.h>
33 #include <TGridResult.h>
34
35 //AliRoot
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliPID.h"
39 #include "AliAODEvent.h"
40 #include "AliAODVertex.h"
41 #include "AliLog.h"
42
43 #include "AliAODTagCreator.h"
44
45
46 ClassImp(AliAODTagCreator)
47
48
49 //______________________________________________________________________________
50   AliAODTagCreator::AliAODTagCreator() :
51     AliTagCreator(), 
52     fChain(0), 
53     fAODEvent(0), 
54     fTreeT(0), 
55     fRunTag(0),
56     fTreeTEsd(0), 
57     fRunTagEsd(0)  
58 {
59   //==============Default constructor for a AliAODTagCreator================
60 }
61
62 //______________________________________________________________________________
63 AliAODTagCreator::~AliAODTagCreator() {
64 //================Default destructor for a AliAODTagCreator===================
65   delete fChain;
66   delete fAODEvent;
67 }
68
69 //______________________________________________________________________________
70 Bool_t AliAODTagCreator::ReadGridCollection(TGridResult *fresult) {
71   // Reads the entry of the TGridResult and creates the tags
72   Int_t nEntries = fresult->GetEntries();
73
74   TString alienUrl;
75   const char* guid;
76   const char* md5;
77   const char* turl;
78   Long64_t size = -1;
79
80   fChain = new TChain("aodTree");
81  
82   for(Int_t i = 0; i < nEntries; i++) {
83     alienUrl = fresult->GetKey(i,"turl");
84     guid = fresult->GetKey(i,"guid");
85     if(fresult->GetKey(i,"size")) size = atol (fresult->GetKey(i,"size"));
86     md5 = fresult->GetKey(i,"md5");
87     turl = fresult->GetKey(i,"turl");
88     if(md5 && !strlen(guid)) md5 = 0;
89     if(guid && !strlen(guid)) guid = 0;
90     
91     fChain->Add(alienUrl);
92   }//grid result loop
93   
94   AliInfo(Form("AOD chain created......."));    
95   AliInfo(Form("Chain entries: %d",fChain->GetEntries()));      
96
97   CreateTag(fChain, "grid");
98   
99   return kTRUE;
100 }
101
102 //______________________________________________________________________________
103 Bool_t AliAODTagCreator::ReadLocalCollection(const char *localpath) {
104   // Checks the different subdirs of the given local path and in the
105   // case where it finds an AliAODs.root file it creates the tags
106   
107   void *dira =  gSystem->OpenDirectory(localpath);
108   Char_t fPath[256];
109   const char * dirname = 0x0;
110   const char * filename = 0x0;
111   const char * pattern = "AliAODs.root"; 
112
113   fChain = new TChain("aodTree");
114
115   while((dirname = gSystem->GetDirEntry(dira))) {
116     sprintf(fPath,"%s/%s",localpath,dirname);
117     void *dirb =  gSystem->OpenDirectory(fPath);
118     while((filename = gSystem->GetDirEntry(dirb))) {
119       if(strstr(filename,pattern)) {
120         TString aodFileName;
121         aodFileName = fPath;
122         aodFileName += "/";
123         aodFileName += pattern;
124         fChain->Add(aodFileName);
125       }//pattern check
126     }//child directory's entry loop
127   }//parent directory's entry loop
128
129   AliInfo(Form("AOD chain created......."));    
130   AliInfo(Form("Chain entries: %d",fChain->GetEntries()));      
131
132   CreateTag(fChain, "local");
133
134   return kTRUE;
135 }
136
137 //______________________________________________________________________________
138 Bool_t AliAODTagCreator::ReadCAFCollection(const char *filename) {
139   // Temporary solution for CAF: Takes as an input the ascii file that
140   // lists the AODs stored in the SE of the CAF and creates the tags.
141
142   // Open the input stream
143   ifstream in;
144   in.open(filename);
145
146   TString aodfile;
147
148   fChain = new TChain("aodTree");
149
150   // Read the input list of files and add them to the chain
151   while(in.good()) {
152     in >> aodfile;
153     if (!aodfile.Contains("root")) continue; // protection
154     fChain->Add(aodfile);
155   }
156
157   AliInfo(Form("AOD chain created......."));    
158   AliInfo(Form("Chain entries: %d",fChain->GetEntries()));      
159
160   CreateTag(fChain, "proof");
161
162   return kTRUE;
163 }
164
165 //__________________________________________________________________________
166 void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent, TList */*grpList*/) {
167   // Creates tag files for AODs
168
169     AliInfo(Form("Creating the AOD tags......."));      
170     
171     TFile *file = TFile::Open("AliAOD.root");
172     if (!file || !file->IsOpen()) {
173         AliError(Form("opening failed"));
174         delete file;
175         return ;
176     }
177
178     fChain = new TChain("aodTree");
179     fChain->Add("AliAOD.root");
180     
181     fAODEvent = new AliAODEvent();
182     fAODEvent->ReadFromTree(fChain);
183     
184     Int_t lastEvent = 0;  
185     if(fLastEvent == -1) lastEvent = (Int_t)fChain->GetEntries();
186     else lastEvent = fLastEvent;
187
188
189     char fileName[256];
190     sprintf(fileName, "Run%d.Event%d_%d.AOD.tag.root", 
191             fAODEvent->GetRunNumber(), fFirstEvent, lastEvent );
192     AliInfo(Form("writing tags to file %s", fileName));
193     AliDebug(1, Form("writing tags to file %s", fileName));
194  
195     TFile* ftag = TFile::Open(fileName, "recreate");
196
197     AliRunTag *fRunTag = new AliRunTag();
198     fTreeT = new TTree("T","A Tree with event tags");
199     TBranch * btag = fTreeT->Branch("AliTAG", &fRunTag);
200     btag->SetCompressionLevel(9);
201
202     CreateTags();
203
204     ftag->cd();
205     fTreeT->Fill();
206     fRunTag->Clear();
207     fTreeT->Write();
208     ftag->Close();
209     file->cd();
210     file->Close();
211 }
212
213 //_____________________________________________________________________________
214 void AliAODTagCreator::CreateTag(TChain* chain, const char *type) {
215     
216     // Private method that creates tag files
217     //
218
219
220     //reading the esd tag file                                                  
221     fTreeTEsd = new TChain("T");
222     const char * tagPattern = "ESD.tag";
223     // Open the working directory
224     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
225     const char * name = 0x0;
226     // Add all files matching *pattern* to the chain
227     while((name = gSystem->GetDirEntry(dirp))) {
228         if (strstr(name,tagPattern)) fTreeTEsd->Add(name);
229     }//directory loop
230     AliInfo(Form("Chained tag files: %d", fTreeTEsd->GetEntries()));
231   
232     
233     fChain = chain;
234     
235     TString fSession = type;
236     TString fguid, fmd5, fturl;
237     fAODEvent = new AliAODEvent();
238     fAODEvent->ReadFromTree(fChain);
239
240     Int_t firstEvent = 0;
241     
242     TString localFileName = "Run"; 
243     localFileName += fAODEvent->GetRunNumber(); 
244     localFileName += ".Event"; 
245     localFileName += firstEvent; 
246     localFileName += "_"; 
247     localFileName += chain->GetEntries(); //localFileName += "."; localFileName += Counter;
248     localFileName += ".AOD.tag.root";
249     
250     TString fileName;
251     
252     if(fStorage == 0) {
253         fileName = localFileName.Data();      
254         AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
255     }
256     else if(fStorage == 1) {
257         TString alienLocation = "/alien";
258         alienLocation += gGrid->Pwd();
259         alienLocation += fgridpath.Data();
260         alienLocation += "/";
261         alienLocation +=  localFileName;
262         alienLocation += "?se=";
263         alienLocation += fSE.Data();
264         fileName = alienLocation.Data();
265         AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
266     }
267     
268     TFile* ftag = TFile::Open(fileName, "recreate");
269     
270     fRunTag = new AliRunTag();
271     fTreeT  = new TTree("T", "A Tree with event tags");
272     TBranch * btag = fTreeT->Branch("AliTAG", &fRunTag);
273     btag->SetCompressionLevel(9);
274     
275     // Access information from esd tag
276     fRunTagEsd = new AliRunTag();
277     fTreeTEsd->SetBranchAddress("AliTAG",&fRunTagEsd);
278     // Creating new information of aod
279     AliInfo(Form("Creating the AOD tags......."));
280     CreateTags(type);
281     ftag->cd();
282     fRunTag->Clear();
283     fTreeT->Write();
284     ftag->Close();
285 }
286
287 void AliAODTagCreator::CreateTags(const char* type)
288 {
289     // Event loop for tag creation
290     TString fturl;
291     TString fguid;
292     Int_t   oldRun = -1;
293     fChain->GetEntry(0);
294     TFile *f = fChain->GetFile();
295     TString ftempGuid = f->GetUUID().AsString();
296     // Loop over events 
297     Int_t nEvents = fChain->GetEntries();
298     Int_t ntags    = 0;
299     Int_t tagentry = 0;
300     const TClonesArray *evTagList = 0;
301
302     for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
303         // Copy old tag information
304         if (iEventNumber >= ntags) {
305             fTreeTEsd->GetEntry(tagentry++);
306             fRunTag->CopyStandardContent(fRunTagEsd);
307             evTagList = fRunTagEsd->GetEventTags();
308             ntags += evTagList->GetEntries();
309         }
310
311         // Create a new Tag
312         AliEventTag* evTag = new AliEventTag();
313         // Read event
314         fChain->GetEntry(iEventNumber);
315         if (iEventNumber == 0) oldRun = fAODEvent->GetRunNumber();
316         // Reference to the input file
317         TFile *file = fChain->GetFile();
318         const TUrl *url = file->GetEndpointUrl();
319         fguid = file->GetUUID().AsString();
320
321         if (type == "grid") {
322             TString fturltemp = "alien://"; fturltemp += url->GetFile();
323             fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
324         } else {
325             fturl = url->GetFile();
326         }
327         
328         fAODEvent->GetStdContent();
329         
330         // Fill the event tag from the aod informatiom
331         FillEventTag(fAODEvent, evTag);
332         // Set the event and input file references
333         evTag->SetEventId(iEventNumber+1);
334         evTag->SetGUID(fguid);
335         if(type == "grid") {
336             evTag->SetMD5(0);
337             evTag->SetTURL(fturl);
338             evTag->SetSize(0);
339             }
340         else evTag->SetPath(fturl);
341
342         // Check if a new run has to be created
343         // File has changed
344         if(fguid != ftempGuid) {
345             ftempGuid = fguid;
346             fTreeT->Fill();
347             fRunTag->Clear("");
348         }
349         // Run# has changed
350         if (oldRun != (fAODEvent->GetRunNumber()))
351         {
352             oldRun = fAODEvent->GetRunNumber();
353             fTreeT->Fill();
354             fRunTag->Clear("");
355         }
356         
357         // Add the event tag
358         fRunTag->AddEventTag(*evTag);
359         delete evTag;
360         // Last event
361         if(iEventNumber+1 == fChain->GetEntries()) {
362             fTreeT->Fill();
363             fRunTag->Clear("");
364         }      
365     }//event loop
366 }
367
368
369
370 void AliAODTagCreator::FillEventTag(AliAODEvent* aod, AliEventTag* evTag)
371 {
372 // 
373 // Fill the event tag information       
374     //
375     fAODEvent = aod;
376     
377     //
378     Float_t fLowPtCut      =  1.0;
379     Float_t fHighPtCut     =  3.0;
380     Float_t fVeryHighPtCut = 10.0;
381     ////////////                            
382     Double_t partFrac[10] = {0.01, 0.01, 0.85, 0.10, 0.05, 0., 0., 0., 0., 0.};
383     
384     // Creates the tags for all the events in a given AOD file
385     Int_t   ntrack = 0;
386     Int_t   nPos = 0, nNeg = 0, nNeutr =0;
387     Int_t   nKinks = 0, nV0s = 0, nCascades = 0;
388     Int_t   nK0s = 0, nNeutrons = 0, nPi0s = 0, nGamas = 0;
389     Int_t   nProtons = 0,  nKaons = 0, nPions = 0, nMuons = 0, nElectrons = 0;
390     Int_t   nCh1GeV = 0, nCh3GeV = 0, nCh10GeV = 0;
391     Int_t   nMu1GeV = 0, nMu3GeV = 0, nMu10GeV = 0;
392     Int_t   nEl1GeV = 0, nEl3GeV = 0, nEl10GeV = 0;
393     Float_t maxPt =  .0, meanPt = .0, totalP =  .0;
394
395     Int_t nTracks = fAODEvent->GetNTracks();
396     // loop over vertices 
397     Int_t nVtxs = fAODEvent->GetNumberOfVertices();
398     for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
399         AliAODVertex *vertex = fAODEvent->GetVertex(nVtx);
400         if(vertex->GetType() == 1) nKinks    += 1;
401         if(vertex->GetType() == 2) nV0s      += 1;
402         if(vertex->GetType() == 3) nCascades += 1;
403     }
404     for (Int_t nTr = 0; nTr < nTracks; nTr++) {
405         AliAODTrack *track = fAODEvent->GetTrack(nTr);
406         
407         Double_t fPt = track->Pt();
408         if(fPt > maxPt) maxPt = fPt;
409         if(track->Charge() > 0) {
410             nPos++;
411             if(fPt > fLowPtCut)      nCh1GeV++;
412             if(fPt > fHighPtCut)     nCh3GeV++;
413             if(fPt > fVeryHighPtCut) nCh10GeV++;
414         }
415         if(track->Charge() < 0) {
416             nNeg++;
417             if(fPt > fLowPtCut)      nCh1GeV++;
418             if(fPt > fHighPtCut)     nCh3GeV++;
419             if(fPt > fVeryHighPtCut) nCh10GeV++;
420             }
421         if(track->Charge() == 0) nNeutr++;
422         //PID
423         const Double32_t *prob = track->PID();
424         Double_t rcc = 0.0;
425         for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
426         if(rcc == 0.0) continue;
427         //Bayes' formula  
428         Double_t w[10];
429         for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
430         
431         //protons
432         if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
433         //kaons
434         if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
435         //pions
436         if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++;
437         //muons 
438         if ((w[1]>w[4])&&(w[1]>w[3])&&(w[1]>w[2])&&(w[1]>w[0])) {
439             nMuons++;
440             if(fPt > fLowPtCut)      nMu1GeV++;
441             if(fPt > fHighPtCut)     nMu3GeV++;
442             if(fPt > fVeryHighPtCut) nMu10GeV++;
443         }
444         //electrons  
445         if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
446             nElectrons++;
447             if(fPt > fLowPtCut) nEl1GeV++;
448             if(fPt > fHighPtCut) nEl3GeV++;
449             if(fPt > fVeryHighPtCut) nEl10GeV++;
450         }
451             
452         totalP += track->P();
453         meanPt += fPt;
454         ntrack++;
455     }//track loop
456     //
457     // Fill the event tags  
458     if(ntrack != 0)
459         meanPt = meanPt/ntrack;
460     
461     
462     evTag->SetNumOfTracks(nTracks);
463     evTag->SetNumOfPosTracks(nPos);
464     evTag->SetNumOfNegTracks(nNeg);
465     evTag->SetNumOfNeutrTracks(nNeutr);
466     
467     evTag->SetNumOfV0s(nV0s);
468     evTag->SetNumOfCascades(nCascades);
469     evTag->SetNumOfKinks(nKinks);
470     
471     evTag->SetNumOfProtons(nProtons);
472     evTag->SetNumOfKaons(nKaons);
473     evTag->SetNumOfPions(nPions);
474     evTag->SetNumOfMuons(nMuons);
475     evTag->SetNumOfElectrons(nElectrons);
476     evTag->SetNumOfPhotons(nGamas);
477     evTag->SetNumOfPi0s(nPi0s);
478     evTag->SetNumOfNeutrons(nNeutrons);
479     evTag->SetNumOfKaon0s(nK0s);
480     
481     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
482     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
483     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
484     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
485     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
486     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
487     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
488     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
489     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
490         
491     evTag->SetTotalMomentum(totalP);
492     evTag->SetMeanPt(meanPt);
493     evTag->SetMaxPt(maxPt);
494 }