]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliMultiEventInputHandler.cxx
Corrections for reconnection of event in case of event mixing.
[u/mrichter/AliRoot.git] / ANALYSIS / AliMultiEventInputHandler.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 multiple VEvent input.
20 //     This class handles multiple inputs for event mixing. 
21 //     Author: Andreas Morsch, CERN
22 //-------------------------------------------------------------------------
23
24 #include "AliMultiEventInputHandler.h"
25 #include "AliVEvent.h"
26 #include "AliAODEvent.h"
27 #include "AliESDEvent.h"
28 #include "AliVEventPool.h"
29 #include "AliLog.h"
30 #include <TObjArray.h>
31 #include <TTree.h>
32 #include <TList.h>
33 #include <TEntryList.h>
34
35
36 ClassImp(AliMultiEventInputHandler)
37
38 AliMultiEventInputHandler::AliMultiEventInputHandler() :
39     AliInputEventHandler(),
40     fBufferSize(0),
41     fFormat(1),
42     fNBuffered(0),
43     fIndex(0),
44     fCurrentBin(0),
45     fCurrentEvt(0),
46     fInit(0),
47     fEventPool(0),
48     fEventBuffer(0)
49 {
50   // Default constructor
51 }
52
53 //______________________________________________________________________________
54 AliMultiEventInputHandler::AliMultiEventInputHandler(Int_t size, Int_t format) :
55     AliInputEventHandler(),
56     fBufferSize(size),
57     fFormat(format),
58     fNBuffered(0),
59     fIndex(0),
60     fCurrentBin(0),
61     fCurrentEvt(0),
62     fInit(0),
63     fEventPool(0),
64     fEventBuffer(0)
65 {
66   // constructor
67 }
68
69 //______________________________________________________________________________
70 AliMultiEventInputHandler::AliMultiEventInputHandler(const char* name, const char* title, Int_t size, Int_t format):
71     AliInputEventHandler(name, title),
72     fBufferSize(size),
73     fFormat(format),
74     fNBuffered(0),
75     fIndex(0),
76     fCurrentBin(0),
77     fCurrentEvt(0),
78     fInit(0),
79     fEventPool(0),
80     fEventBuffer(0)
81 {
82     // Constructor
83
84 }
85
86 //______________________________________________________________________________
87 AliMultiEventInputHandler::~AliMultiEventInputHandler() 
88 {
89 // Destructor
90 }
91
92 Bool_t AliMultiEventInputHandler::Init(TTree* tree, Option_t* /*opt*/)
93 {
94     // Initialisation necessary for each new tree
95     if (!fEventBuffer) {
96         fEventBuffer = new AliVEvent*[fBufferSize];
97         
98         for (Int_t i = 0; i < fBufferSize; i++) 
99             if (fFormat == 1) {
100                 fEventBuffer[i] = new AliAODEvent();
101             } else if (fFormat == 0) {
102                 fEventBuffer[i] = new AliESDEvent();
103             } else{
104                 AliWarning(Form("Unknown Format %5d", fFormat));
105             }
106     }
107     
108
109     fTree = tree;
110     fInit = 1;
111     
112     if (!fTree) return kFALSE;
113     for (Int_t i = 0; i < fBufferSize; i++) 
114         fEventBuffer[i]->Clear();
115     fIndex     = 0;
116     fNBuffered = 1;
117     return kTRUE;
118 }
119
120
121 Bool_t AliMultiEventInputHandler::Notify(const char */*path*/)
122 {
123     // Connect to new tree
124  
125     TList* connectedList = (TList*) (fTree->GetUserInfo()->FindObject("AODObjectsConnectedToTree"));   
126     if (connectedList && !fInit) {
127         fEventBuffer[0]->ReadFromTree(fTree, "reconnect");
128     } else {
129         if (fInit) fEventBuffer[0]->ReadFromTree(fTree, "");
130     }
131     
132     fCurrentEvt = 0;
133     fInit = 0;
134     
135     return (kTRUE);
136 }
137
138 Bool_t AliMultiEventInputHandler::BeginEvent(Long64_t /*entry*/)
139 {
140     // Actions before analysis of each event 
141     //
142     // Reset the number of events buffered for this bin to 0
143     if (fCurrentBin != (fEventPool->BinNumber())) {
144         fCurrentBin = fEventPool->BinNumber();
145         fNBuffered = 0;
146     }
147     return kTRUE;
148 }
149
150 Bool_t AliMultiEventInputHandler::FinishEvent()
151 {
152     // 
153     // Connect the next event in the buffer to the tree
154     fIndex++;
155     
156     fIndex %= fBufferSize;
157     AliInfo(Form("Connecting buffer entry %5d", fIndex));
158     fEventBuffer[fIndex]->Clear();
159     fCurrentEvt++;
160     if (fEventBuffer[fIndex]->GetList() && fCurrentEvt > (fBufferSize - 1))
161         fEventBuffer[fIndex]->GetList()->Delete();
162
163     fEventBuffer[fIndex]->ReadFromTree(fTree, "reconnect");
164
165     fNBuffered++;
166     if (fNBuffered > fBufferSize) fNBuffered = fBufferSize;
167
168     Int_t nmax = fTree->GetEntries();
169     if (fTree->GetEntryList()) {
170         nmax = (fTree->GetEntryList()->GetN());
171     } else {
172         if (fTree->GetTree()) nmax = fTree->GetTree()->GetEntries();
173     }
174     
175     if (fCurrentEvt == nmax)
176     {
177         for (Int_t i = 0; i < fBufferSize; i++) {
178             fEventBuffer[i]->Clear();
179         }
180     }
181     
182     return (kTRUE);
183 }
184
185 AliVEvent* AliMultiEventInputHandler::GetEvent(Int_t iev) const
186 {
187     // Get event number iev from buffer
188     if ((iev < 0) || (iev >= fBufferSize))
189     {
190         AliWarning(Form("Event number out of range: %10d", iev));
191         return 0;
192     }
193         
194     iev = fIndex - (fBufferSize - 1 - iev);
195     if (iev < 0) iev += fBufferSize;
196     AliInfo(Form("Event index in buffer is %5d", iev));
197     return (fEventBuffer[iev]);
198 }
199