Extending the functionality of the physics selection. Instead of providing a yes...
[u/mrichter/AliRoot.git] / STEER / AliAODInputHandler.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //     Event handler for AOD input 
20 //     Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
22
23 #include <TTree.h>
24 #include <TList.h>
25 #include <TNamed.h>
26 #include <TFile.h>
27
28 #include "AliAODInputHandler.h"
29 #include "AliAODEvent.h"
30
31 ClassImp(AliAODInputHandler)
32
33 static Option_t *gAODDataType = "AOD";
34
35 //______________________________________________________________________________
36 AliAODInputHandler::AliAODInputHandler() :
37     AliInputEventHandler(),
38     fEvent(0),
39     fMCEvent(new AliMCEvent()),
40     fFriends(new TList()),
41     fMergeEvents(kFALSE),
42     fFileToMerge(0),
43     fTreeToMerge(0),
44     fAODEventToMerge(0),
45     fMergeOffset(0)
46 {
47   // Default constructor
48 }
49
50 //______________________________________________________________________________
51 AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
52   AliInputEventHandler(name, title),
53   fEvent(0),
54   fMCEvent(new AliMCEvent()),
55   fFriends(new TList()),
56   fMergeEvents(kFALSE),
57   fFileToMerge(0),
58   fTreeToMerge(0),
59   fAODEventToMerge(0),
60   fMergeOffset(0)
61 {
62     // Constructor
63 }
64
65 //______________________________________________________________________________
66 AliAODInputHandler::~AliAODInputHandler() 
67 {
68 // Destructor
69     fFriends->Delete();
70 }
71
72
73 Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* /*opt*/)
74 {
75     // Initialisation necessary for each new tree
76     if (!fMergeEvents) {
77         fTree = tree;
78         TIter next(fFriends);
79         TNamed* obj;
80         
81         if (!fTree) return kFALSE;
82         fTree->GetEntry(0);
83         TString aodTreeFName,aodFriendTreeFName;
84         
85         while((obj = (TNamed*)next())) {
86             if (fTree->GetTree()) {
87                 aodTreeFName = (fTree->GetTree()->GetCurrentFile())->GetName();
88                 aodFriendTreeFName = aodTreeFName;
89                 aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
90                 aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
91                 (fTree->GetTree())->AddFriend("aodTree", aodFriendTreeFName.Data());
92             } else {
93                 aodTreeFName = (fTree->GetCurrentFile())->GetName();
94                 aodFriendTreeFName = aodTreeFName;
95                 aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
96                 aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
97                 fTree->AddFriend("aodTree", aodFriendTreeFName.Data());
98             }
99         }
100     } else {
101         // Friends have to be merged
102         TNamed* filename = (TNamed*) (fFriends->At(0));
103         fFileToMerge = new TFile(filename->GetName());
104         if (fFileToMerge) {
105             fFileToMerge->GetObject("aodTree", fTreeToMerge);
106             if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
107             fAODEventToMerge->ReadFromTree(fTreeToMerge);
108         }
109     }
110     
111     
112  
113
114     SwitchOffBranches();
115     SwitchOnBranches();
116     
117     // Get pointer to AOD event
118     if (!fEvent) fEvent = new AliAODEvent();
119
120     fEvent->ReadFromTree(fTree);
121
122     return kTRUE;
123 }
124
125 Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
126 {
127     //
128     TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
129     if (mcParticles) fMCEvent->SetParticleArray(mcParticles);
130     if (fTreeToMerge) fTreeToMerge->GetEntry(entry + fMergeOffset);
131     
132     fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
133     
134     return kTRUE;
135 }
136
137 void AliAODInputHandler::AddFriend(char* filename)
138 {
139     // Add a friend tree 
140     TNamed* obj = new TNamed(filename, filename);
141     fFriends->Add(obj);
142 }
143
144 Option_t *AliAODInputHandler::GetDataType() const
145 {
146 // Returns handled data type.
147    return gAODDataType;
148 }