]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/AliRsnAnalysisSE.cxx
Update for Cascades from A.Maire
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / AliRsnAnalysisSE.cxx
1 //
2 // Class AliRsnAnalysisSE
3 //
4 // TODO
5 //
6 // authors: Martin Vala (martin.vala@cern.ch)
7 //          Alberto Pulvirenti (alberto.pulvirenti@ct.infn.it)
8 //
9
10 #include <Riostream.h>
11
12 #include <TSystem.h>
13 #include <TFile.h>
14 #include <TFolder.h>
15 #include <TROOT.h>
16
17 #include "AliLog.h"
18
19 #include "AliAnalysisManager.h"
20 #include "AliRsnPairMgr.h"
21 #include "AliRsnEventBuffer.h"
22
23 #include "AliMCEventHandler.h"
24 #include "AliESDEvent.h"
25
26 #include "AliRsnAnalysisSE.h"
27
28 ClassImp(AliRsnAnalysisSE)
29
30 //________________________________________________________________________
31 AliRsnAnalysisSE::AliRsnAnalysisSE(const char * name, Int_t bufferSize) :
32   AliRsnAnalysisTaskSEBase(name),
33   fDoesMixing(kFALSE),
34   fMixingNum(0),
35   fMixingCut(0x0),
36   fPairMgrs(0),
37   fOutList(0x0),
38   fBuffer(0x0),
39   fBufferSize(bufferSize)
40 {
41 //
42 // Default constructor
43 //
44
45   InitIOVars();
46   DefineOutput(1, TList::Class());
47 }
48
49 //________________________________________________________________________
50 AliRsnAnalysisSE::~AliRsnAnalysisSE()
51 {
52 //
53 // Destructor
54 //
55 }
56
57 //________________________________________________________________________
58 void AliRsnAnalysisSE::InitIOVars()
59 {
60 //
61 // Init input output values
62 //
63
64   AliRsnAnalysisTaskSEBase::InitIOVars();
65
66   fBuffer = 0;
67   fOutList = 0;
68 }
69
70 //________________________________________________________________________
71 void AliRsnAnalysisSE::UserCreateOutputObjects()
72 {
73 //
74 // UserCreateOutputObjects() of AliAnalysisTaskSE
75 //
76
77   OpenFile(0);
78   fOutList = new TList();
79   fOutList->SetOwner();
80   AliRsnPair *def=0;
81   AliRsnPairMgr *mgr=0;
82   TList *listTmp;
83   fDoesMixing = kFALSE;
84   for (Int_t iMgr=0 ;iMgr< fPairMgrs.GetEntries();iMgr++)
85   {
86     mgr = (AliRsnPairMgr *) fPairMgrs.At(iMgr);
87     if (!mgr) continue;
88     listTmp = new TList();
89     listTmp->SetName(mgr->GetName());
90     for (Int_t i=0;i< mgr->GetPairs()->GetEntriesFast();i++)
91     {
92       def = (AliRsnPair *) mgr->GetPairs()->At(i);
93       if (def)
94       {
95         listTmp->Add(def->GenerateHistograms(mgr->GetName()));
96         if (def->IsMixed()) fDoesMixing = kTRUE;
97       }
98     }
99     fOutList->Add(listTmp);
100   }
101
102   TH1I *hUsed = new TH1I("hRsnUsed", "skipped and used events in this analysis", 2, 0, 2);
103   fOutList->Add(hUsed);
104
105   fBuffer = new AliRsnEventBuffer(fBufferSize);
106 }
107
108 //________________________________________________________________________
109 void AliRsnAnalysisSE::UserExec(Option_t *)
110 {
111 //
112 // UserExec() of AliAnalysisTaskSE
113 //
114
115   static UInt_t eventID = 0;
116
117   if (fEntry++ % 100 == 0) cout << "[" << GetName() << "] : processing entry " << fEntry-1 << endl;
118
119   TH1I *h = (TH1I*)fOutList->FindObject("hRsnUsed");
120
121   AliRsnEvent *curEvent = GetRsnEventFromInputType();
122   if (curEvent) {
123     curEvent->SetUniqueID(eventID++);
124     ProcessEventAnalysis(curEvent);
125     PostEventProcess();
126     h->Fill(1);
127   }
128   else {
129     h->Fill(0);
130   }
131
132   PostData(1, fOutList);
133 }
134
135 //________________________________________________________________________
136 void AliRsnAnalysisSE::Terminate(Option_t *)
137 {
138 //
139 // Terminate() of AliAnalysisTask
140 //
141
142   fOutList = dynamic_cast<TList*>(GetOutputData(1));
143   if (!fOutList) { AliError("At end of analysis, output list is NULL"); return; }
144   //fOutList->Print();
145 }
146
147 //________________________________________________________________________
148 void AliRsnAnalysisSE::ProcessEventAnalysis(AliRsnEvent *curEvent)
149 {
150 //
151 // Process of one event
152 //
153
154   // Adds event to Event Buffer
155   fBuffer->AddEvent(curEvent);
156   Int_t index = fBuffer->GetEventsBufferIndex();
157
158   Int_t nmatches;
159   TArrayI matched(0);
160   if (fDoesMixing) matched = FindGoodMatches(index, nmatches);
161
162   // loop over all Pair managers
163   AliRsnPairMgr *mgr=0;
164   for (Int_t iMgr=0 ;iMgr< fPairMgrs.GetEntries();iMgr++)
165   {
166     mgr = (AliRsnPairMgr *) fPairMgrs.At(iMgr);
167     AliRsnPair *pair=0;
168     for (Int_t i=0;i< mgr->GetPairs()->GetEntriesFast();i++)
169     {
170       pair = (AliRsnPair *) mgr->GetPairs()->At(i);
171       if (!pair->IsMixed()) {
172         pair->ProcessPair(curEvent, 0);
173       }
174       else {
175         Int_t i, iev;
176         for (i = 0; i < matched.GetSize(); i++) {
177           iev = matched[i];
178           if (iev < 0) continue;
179           AliRsnEvent *evmatch = fBuffer->GetEvent(iev);
180           pair->ProcessPair(curEvent, evmatch);
181           if (!pair->IsPairEqual()) pair->ProcessPair(evmatch, curEvent);
182         }
183       }
184     }
185   }
186 }
187
188 //________________________________________________________________________
189 void AliRsnAnalysisSE::PostEventProcess(const Short_t & index)
190 {
191 //
192 // Post process of one event
193 //
194
195   if (fInputType[index] != kRSN) return;
196
197   if (fBuffer->GetDeleteBufferWhenReset() == kFALSE) {
198     fRSN[index] = (AliRsnEvent*) fBuffer->GetNextEvent();
199     SetBranchAddress(0 , "rsnEvents", &fRSN[index]);
200   }
201 }
202
203 //________________________________________________________________________
204 void  AliRsnAnalysisSE::AddPairMgr(AliRsnPairMgr * pairmgr)
205 {
206   fPairMgrs.Add(pairmgr);
207 }
208
209 //________________________________________________________________________
210 void AliRsnAnalysisSE::AddPairMgrFromConfig(TString configfile)
211 {
212   gROOT->LoadMacro(configfile.Data());
213
214   configfile.ReplaceAll(".C","");
215
216   AliRsnPairMgr *mgrRsn = (AliRsnPairMgr *) gROOT->ProcessLine(Form("%s();", configfile.Data()));
217   if (!mgrRsn) return;
218
219   fPairMgrs.Add(mgrRsn);
220 }
221
222 //________________________________________________________________________
223 TArrayI AliRsnAnalysisSE::FindGoodMatches(Int_t iRef, Int_t &foundMatches)
224 {
225   // initialize the output array to the size of required mixed events
226   // and initialize all members to -1
227   Int_t i;
228   TArrayI matched(fMixingNum);
229   for (i = 0; i < fMixingNum; i++) matched[i] = -1;
230   foundMatches = 0;
231
232   // starts from the position behind the reference index
233   // and goes backward; if it reaches the value 0, stops
234   AliRsnEvent *refEvent = fBuffer->GetEvent(iRef);
235   if (!refEvent) return matched;
236   AliRsnEvent *matchEvent = 0x0;
237   Int_t checkIndex;
238   for (checkIndex = iRef - 1; ; checkIndex--) {
239     if (checkIndex < 0) checkIndex = fBuffer->GetEventsBufferSize() - 1;
240     if (checkIndex == iRef) break;
241     matchEvent = fBuffer->GetEvent(checkIndex);
242     if (!matchEvent) continue;
243     if (fMixingCut) {
244       if (!fMixingCut->IsSelected(AliRsnCut::kMixEvent, refEvent, matchEvent)) continue;
245     }
246     // assign to current array slot the matched event
247     // and increment current slot and stops if it exceeds array size
248     matched[foundMatches++] = checkIndex;
249     if (foundMatches >= fMixingNum) break;
250   }
251
252   // returns the current index value,
253   // which is also the number of matched events found
254   return matched;
255 }