]> git.uio.no Git - u/mrichter/AliRoot.git/blob - JETAN/AliJFMixEvent.cxx
Version of the jet analysis module from v4-01-Release
[u/mrichter/AliRoot.git] / JETAN / AliJFMixEvent.cxx
1 // $Id$
2
3 #include <Riostream.h>
4
5 #include <TParticle.h>
6 #include <TTree.h>
7 #include <TFile.h>
8 #include <TClonesArray.h>
9 #include <TChain.h>
10
11 #include "AliJFMixEvent.h"
12
13 AliJFMixEvent::AliJFMixEvent(Char_t *file1, Char_t *file2) : 
14   fStatus(-1),fEvent1(-1),fEvent2(-1),fMaxEvent1(-1),fMaxEvent2(-1),
15   fMarkPythia(0),fTree1(0),fTree2(0),fFile1(0),
16   fFile2(0),fBranch1(0),fBranch2(0),
17   fParticles1(0),fParticles2(0),fMixedParticles(0)
18 {
19   Init(file1,file2);
20 }
21
22 AliJFMixEvent::AliJFMixEvent(Char_t *files1, Char_t *tname1, Char_t *files2=0, Char_t *tname2=0) :
23   fStatus(-1),fEvent1(-1),fEvent2(-1),fMaxEvent1(-1),fMaxEvent2(-1),
24   fMarkPythia(0),fTree1(0),fTree2(0),fFile1(0),
25   fFile2(0),fBranch1(0),fBranch2(0),
26   fParticles1(0),fParticles2(0),fMixedParticles(0)
27 {
28   InitChains(files1,tname1,files2,tname2);
29 }
30
31 AliJFMixEvent::~AliJFMixEvent() 
32 {
33   Clean();
34 }
35
36 void AliJFMixEvent::Init(Char_t *file1, Char_t *file2)
37 {
38   Clean();
39
40   if (!file1) {
41     cerr << "Error AliJFMixEvent:: Must give at least one filename!" << endl;
42     return;
43   }
44   
45   fFile1 = NULL;
46   fFile1 = new TFile(file1,"READ");
47   if (!fFile1 || !(fFile1->IsOpen())) {
48     cerr << "Error AliJFMixEvent:: Couldn't open input file " << file1 << endl;
49     return;
50   }
51
52   fFile2 = NULL;
53   if(file2!=0){
54     fFile2 = new TFile(file2,"READ");
55     if (!fFile2 || !(fFile2->IsOpen())) {
56       cerr << "Error AliJFMixEvent:: Couldn't open input file " << file2 << endl;
57       return;
58     }
59   }
60
61   Int_t prealloc1=100000;
62   fTree1 = NULL;
63   fTree1 = (TTree *)fFile1->Get("hijing");
64   if (!fTree1){
65     fTree1 = (TTree *)fFile1->Get("pythia");
66     prealloc1=10000;
67   }
68   if (!fTree1) {
69     cerr << "Error AliJFMixEvent:: Didn't find a TParticle tree for " << file1 << endl;
70     return;
71   }
72   fMaxEvent1=(Int_t)fTree1->GetEntries();
73
74   Int_t prealloc2=0;
75   fTree2 = NULL;
76   if(file2!=0){
77     prealloc2=100000;
78     fTree2 = (TTree *)fFile2->Get("hijing");
79     if (!fTree2){
80       fTree2 = (TTree *)fFile2->Get("pythia");
81       prealloc2=10000;
82     }
83     if (!fTree2) {
84       cerr << "Error AliJFMixEvent:: Didn't find a TParticle tree for " << file2 << endl;
85       return;
86     }
87     fMaxEvent2=(Int_t)fTree2->GetEntries();
88   }
89
90   fParticles1=new TClonesArray("TParticle",prealloc1);
91   if(file2!=0) fParticles2=new TClonesArray("TParticle",prealloc2);
92   fMixedParticles=new TClonesArray("TParticle",prealloc1+prealloc2);
93
94   fBranch1=fTree1->GetBranch("particles");
95   fBranch1->SetAddress(&fParticles1);
96
97   if(file2!=0){
98     fBranch2=fTree2->GetBranch("particles");
99     fBranch2->SetAddress(&fParticles2);
100   }
101
102   fStatus=0;
103 }
104
105 void AliJFMixEvent::InitChains(Char_t *files1, Char_t *tname1, Char_t *files2, Char_t *tname2)
106 {
107   Clean();
108
109   if (!files1 || !tname1) {
110     cerr << "Error AliJFMixEvent:: Must give at least one file/tree name!" << endl;
111     return;
112   }
113   
114   Int_t prealloc1=100000;
115   Int_t prealloc2=0;
116
117   fTree1=new TChain(tname1);
118   ((TChain*)fTree1)->Add(files1);
119   fParticles1=new TClonesArray("TParticle",prealloc1);
120   fTree1->SetBranchAddress("particles",&fParticles1);
121   fMaxEvent1=(Int_t)fTree1->GetEntries();
122
123   if (files2 && tname2) {
124     prealloc2=100000;
125     fTree2=new TChain(tname2);
126     ((TChain*)fTree2)->Add(files2);
127     fParticles2=new TClonesArray("TParticle",prealloc2);
128     fTree2->SetBranchAddress("particles",&fParticles2);
129     fMaxEvent2=(Int_t)fTree2->GetEntries();
130   }
131
132   fMixedParticles=new TClonesArray("TParticle",prealloc1+prealloc2);
133   fStatus=0;
134 }
135
136 void AliJFMixEvent::Clean()
137 {
138   fStatus=-1; //not initialized!
139
140   fEvent1=-1;
141   fEvent2=-1;
142   fMaxEvent1=-1;
143   fMaxEvent2=-1;
144
145   if(fParticles1) delete fParticles1;
146   fParticles1=NULL;
147
148   if(fParticles2) delete fParticles2;
149   fParticles2=NULL;
150
151   if(fMixedParticles) delete fMixedParticles;
152   fMixedParticles=NULL;
153
154   if(fTree1) delete fTree1;
155   fTree1=NULL;
156
157   if(fTree2) delete fTree2;
158   fTree2=NULL;
159
160   if(fFile1) delete fFile1;
161   fFile1=NULL;
162
163   if(fFile2) delete fFile2;
164   fFile2=NULL;
165 }
166
167 Int_t AliJFMixEvent::CreateMixedEvent(Int_t i,Int_t j)
168 {
169   if(fStatus<0) return -1;
170   if(i<0 || j<0) return -1;
171   if((i>=fMaxEvent1)||((fTree2)&&(j>=fMaxEvent2))) return -1;
172
173   if(fEvent1!=i){
174     fTree1->GetEvent(i);
175     fEvent1=i;
176   }
177
178   if(fTree2){
179     if(fEvent2!=j){
180       fTree2->GetEvent(j);
181       fEvent2=j;
182     }
183   }
184   return MixEvent();
185 }
186
187 Int_t AliJFMixEvent::CreateNextMixedEvent()
188 {
189   if(fStatus<0) return -1;
190
191   if(fStatus==0) { //load first data samples
192     fEvent1=0;
193     fTree1->GetEvent(0);
194     fEvent2=0;
195     if(fTree2) fTree2->GetEvent(0);
196   } else { //continue loading
197     fEvent1++;
198     if(fEvent1==fMaxEvent1){
199       fEvent1=0;
200
201       if(fTree2){
202         fEvent2++;
203         if(fEvent2==fMaxEvent2) 
204           fEvent2=0;
205         fTree2->GetEvent(fEvent2);
206       }
207     }
208
209     fTree1->GetEvent(fEvent1);
210   }
211
212   return MixEvent();
213 }
214
215 Int_t AliJFMixEvent::MixEvent()
216 {
217   Int_t n1=fParticles1->GetEntries();
218   Int_t n2=0;
219   if(fTree2) n2=fParticles2->GetEntries();
220
221   fMixedParticles->ExpandCreateFast(n1+n2);
222   Int_t n=0;
223   TParticle *particle;
224   TIterator *i=fParticles1->MakeIterator();
225   while ((particle = (TParticle *) i->Next()) != NULL) {
226     if(fMarkPythia){
227       particle->SetWeight(-123); //mark pythia particles
228     }
229     new ((*fMixedParticles)[n]) TParticle(*particle);
230     n++;
231   }
232   if(fTree2){
233     i=fParticles2->MakeIterator();
234     while ((particle = (TParticle *) i->Next()) != NULL) {
235       new((*fMixedParticles)[n]) TParticle(*particle);
236       n++;
237     }
238   }
239   fStatus++; 
240
241   return n;
242 }
243
244 void AliJFMixEvent::Debug()
245 {
246   if(!fMixedParticles) return;
247
248   TParticle *p;
249   TIterator *iter=fMixedParticles->MakeIterator();
250   while ((p = (TParticle *) iter->Next()) != NULL) {
251     p->Print();
252   }
253 }