Fix for event mixing when running in local mode. Mix event inputhandler was calling...
[u/mrichter/AliRoot.git] / ANALYSIS / EventMixing / AliMixInputHandlerInfo.cxx
CommitLineData
c5e33610 1//
2// Class AliMixInputHandlerInfo
3//
b425275c 4// AliMixInputHandlerInfo is interface with mixed
c5e33610 5// input handlers
6//
b425275c 7// author:
c5e33610 8// Martin Vala (martin.vala@cern.ch)
9//
10#include <TTree.h>
11#include <TChain.h>
12#include <TFile.h>
13#include <TChainElement.h>
14
15#include "AliLog.h"
16#include "AliInputEventHandler.h"
17
18#include "AliMixInputHandlerInfo.h"
19
20ClassImp(AliMixInputHandlerInfo)
21
22//_____________________________________________________________________________
b425275c 23AliMixInputHandlerInfo::AliMixInputHandlerInfo(const char *name, const char *title): TNamed(name, title),
24 fChain(0),
25 fChainEntriesArray(),
26 fZeroEntryNumber(0),
27 fNeedNotify(kFALSE)
28{
29 //
30 // Default constructor.
31 //
c5e33610 32}
33//_____________________________________________________________________________
b425275c 34AliMixInputHandlerInfo::~AliMixInputHandlerInfo()
35{
36 //
37 // Destructor
38 //
39 if (fChain) delete fChain;
c5e33610 40}
41
42//_____________________________________________________________________________
b425275c 43TChain *AliMixInputHandlerInfo::GetChain()
44{
45 //
46 // Returns curren chain. When chain is null it will create it
47 //
48 if (!fChain) fChain = new TChain(GetName());
49 return fChain;
c5e33610 50}
51
52//_____________________________________________________________________________
b425275c 53void AliMixInputHandlerInfo::AddChain(TChain *chain)
54{
55 //
56 // Add chain
57 //
58 AliDebug(AliLog::kDebug + 5, "<-");
59 if (!chain) return;
60 if (fChain) delete fChain;
61 fChain = new TChain(GetName());
62 fChain->Add(chain);
63 AliDebug(AliLog::kDebug + 5, "->");
c5e33610 64}
65
66//_____________________________________________________________________________
b425275c 67void AliMixInputHandlerInfo::AddTreeToChain(const char *path)
68{
69 //
70 // Adds tree in to chain
71 //
72 AliDebug(AliLog::kDebug + 5, Form("<- %s", path));
73 GetChain();
74 fChain->AddFile(path);
75 AliDebug(AliLog::kDebug + 3, Form("Num files in fChain %d", fChain->GetListOfFiles()->GetEntries()));
76 Long64_t sumTree = fZeroEntryNumber;
77 for (Int_t i = 0; i < fChainEntriesArray.GetSize() ; i++) sumTree += fChainEntriesArray.At(i);
78 fChain->LoadTree(sumTree);
79 Int_t lastIndex = fChain->GetListOfFiles()->GetEntries();
80 AliDebug(AliLog::kDebug + 3, Form("Num files in fChain %d", lastIndex));
81 fChainEntriesArray.Set(lastIndex);
82 AliDebug(AliLog::kDebug + 3, Form("Adding %lld to id %d", fChain->GetTree()->GetEntries(), lastIndex - 1));
83 fChainEntriesArray.AddAt((Int_t)fChain->GetTree()->GetEntries(), (Int_t)lastIndex - 1);
84 AliDebug(AliLog::kDebug + 5, Form("-> %s", path));
c5e33610 85}
86
87//_____________________________________________________________________________
b425275c 88TChainElement *AliMixInputHandlerInfo::GetEntryInTree(Long64_t &entry)
89{
90 //
91 // Get entry in current tree
92 //
93 AliDebug(AliLog::kDebug + 5, Form("<- %lld", entry));
94 fZeroEntryNumber = 0;
95 if (entry < fZeroEntryNumber) {
96 AliError(Form("Num %lld is less then ZeroEntryNumber(%lld)", entry, fZeroEntryNumber));
97 entry = -1;
98 AliDebug(AliLog::kDebug + 5, "->");
99 return 0;
100 }
101 Long64_t sumTree = fZeroEntryNumber;
102 for (Int_t i = 0; i < fChainEntriesArray.GetSize() ; i++) {
103 sumTree += fChainEntriesArray.At(i);
104 if (sumTree > entry) {
105 sumTree = entry - sumTree + fChainEntriesArray.At(i);
106 AliDebug(AliLog::kDebug + 1, Form("Entry in current tree num is %lld with i=%d", sumTree, i));
107 entry = sumTree;
108 AliDebug(AliLog::kDebug + 5, "->");
109 return (TChainElement *) fChain->GetListOfFiles()->At(i);
110 }
111 }
112 entry = -1;
113 AliDebug(AliLog::kDebug + 5, "->");
114 return 0;
c5e33610 115}
116
117//_____________________________________________________________________________
b425275c 118void AliMixInputHandlerInfo::PrepareEntry(TChainElement *te, Long64_t entry, AliInputEventHandler *eh, Option_t *opt)
119{
120 //
121 // Prepare Entry
122 //
123 AliDebug(AliLog::kDebug + 5, Form("<- %lld", entry));
124 if (!te) {
125 AliDebug(AliLog::kDebug + 5, "-> te is null");
126 return;
127 }
128 if (entry < 0) {
c5e33610 129 AliDebug(AliLog::kDebug, Form("We are creating new chain from file %s ...", te->GetTitle()));
130 if (!fChain) {
b425275c 131 fChain = new TChain(te->GetName());
132 fChain->AddFile(te->GetTitle());
133 fChain->GetEntry(0);
4a67a9fc 134 eh->Init(opt);
b425275c 135 eh->Init(fChain->GetTree(), opt);
c5e33610 136 }
137 fNeedNotify = kTRUE;
b425275c 138 AliDebug(AliLog::kDebug + 5, "->");
c5e33610 139 return;
b425275c 140 }
141 if (fChain) {
142 AliDebug(AliLog::kDebug, Form("Filename is %s", fChain->GetTree()->GetCurrentFile()->GetName()));
143 TString fn = fChain->GetTree()->GetCurrentFile()->GetName();
144 if (fn.CompareTo(te->GetTitle())) {
145 AliDebug(AliLog::kDebug, Form("Filename %s is NOT same ...", te->GetTitle()));
146 AliDebug(AliLog::kDebug, Form("We are changing to file %s ...", te->GetTitle()));
147 // change file
148 delete fChain;
149 fChain = new TChain(te->GetName());
150 fChain->AddFile(te->GetTitle());
151 fChain->GetEntry(0);
4a67a9fc 152 eh->Init(opt);
b425275c 153 eh->Init(fChain->GetTree(), opt);
154 eh->Notify(te->GetTitle());
b425275c 155 fChain->GetEntry(entry);
cc354357 156 eh->BeginEvent(entry);
b425275c 157 fNeedNotify = kFALSE;
158 } else {
159 AliDebug(AliLog::kDebug, Form("We are reusing file %s ...", te->GetTitle()));
160 if (fNeedNotify) eh->Notify(te->GetTitle());
161 fNeedNotify = kFALSE;
162 AliDebug(AliLog::kDebug, Form("Entry is %lld fChain->GetEntries %lld ...", entry, fChain->GetEntries()));
b425275c 163 fChain->GetEntry(entry);
cc354357 164 eh->BeginEvent(entry);
b425275c 165 // file is in tree fChain already
166 }
167 }
168 AliDebug(AliLog::kDebug, Form("We are USING file %s ...", te->GetTitle()));
169 AliDebug(AliLog::kDebug, Form("We are USING file from fChain->GetTree() %s ...", fChain->GetTree()->GetCurrentFile()->GetName()));
170 // here we have correct chain with 1 tree only
171 AliDebug(AliLog::kDebug + 5, "->");
172}
c5e33610 173
b425275c 174//_____________________________________________________________________________
175Long64_t AliMixInputHandlerInfo::GetEntries()
176{
177 //
178 // Returns number of entries
179 //
180 if (fChain) return fChain->GetEntries();
181 return -1;
c5e33610 182}