write only the complement in the case of hybrid tracks
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 #include <TROOT.h>
19 #include <TSystem.h>
20 #include <TInterpreter.h>
21 #include <TChain.h>
22 #include <TFile.h>
23 #include <TList.h>
24
25 #include "AliAnalysisTaskSE.h"
26 #include "AliAnalysisManager.h"
27 #include "AliAnalysisCuts.h"
28 #include "AliAnalysisDataSlot.h"
29 #include "AliAnalysisDataContainer.h"
30
31 #include "AliESDEvent.h"
32 #include "AliESDfriend.h"
33 #include "AliESD.h"
34 #include "AliAODEvent.h"
35 #include "AliAODHeader.h"
36 #include "AliAODTracklets.h"
37 #include "AliAODCaloCells.h"
38 #include "AliAODCaloTrigger.h"
39 #include "AliAODMCParticle.h"
40 #include "AliVEvent.h"
41 #include "AliAODHandler.h"
42 #include "AliAODInputHandler.h"
43 #include "AliMCEventHandler.h"
44 #include "AliInputEventHandler.h"
45 #include "AliMultiInputEventHandler.h"
46 #include "AliESDInputHandler.h"
47 #include "AliMCEvent.h"
48 #include "AliStack.h"
49 #include "AliLog.h"
50 #include "AliAODDimuon.h"
51
52
53 ClassImp(AliAnalysisTaskSE)
54
55 ////////////////////////////////////////////////////////////////////////
56 AliAODHeader*    AliAnalysisTaskSE::fgAODHeader         = NULL;
57 TClonesArray*    AliAnalysisTaskSE::fgAODTracks         = NULL;
58 TClonesArray*    AliAnalysisTaskSE::fgAODVertices       = NULL;
59 TClonesArray*    AliAnalysisTaskSE::fgAODV0s            = NULL;
60 TClonesArray*    AliAnalysisTaskSE::fgAODPMDClusters    = NULL;
61 TClonesArray*    AliAnalysisTaskSE::fgAODJets           = NULL;
62 TClonesArray*    AliAnalysisTaskSE::fgAODFMDClusters    = NULL;
63 TClonesArray*    AliAnalysisTaskSE::fgAODCaloClusters   = NULL;
64 TClonesArray*    AliAnalysisTaskSE::fgAODMCParticles    = NULL;
65 AliAODTracklets* AliAnalysisTaskSE::fgAODTracklets      = NULL;
66 AliAODCaloCells* AliAnalysisTaskSE::fgAODEmcalCells     = NULL;
67 AliAODCaloCells* AliAnalysisTaskSE::fgAODPhosCells      = NULL;
68 AliAODCaloTrigger* AliAnalysisTaskSE::fgAODEMCALTrigger = NULL;
69 AliAODCaloTrigger* AliAnalysisTaskSE::fgAODPHOSTrigger  = NULL;
70 TClonesArray*    AliAnalysisTaskSE::fgAODDimuons        = NULL;
71
72 AliAnalysisTaskSE::AliAnalysisTaskSE():
73     AliAnalysisTask(),
74     fDebug(0),
75     fEntry(0),
76     fInputEvent(0x0),
77     fESDfriend(0x0),
78     fInputHandler(0x0),
79     fOutputAOD(0x0),
80     fMCEvent(0x0),
81     fTreeA(0x0),
82     fCurrentRunNumber(-1),
83     fHistosQA(0x0),
84     fOfflineTriggerMask(0),
85     fMultiInputHandler(0),
86     fMCEventHandler(0)
87 {
88   // Default constructor
89 }
90
91 AliAnalysisTaskSE::AliAnalysisTaskSE(const char* name):
92     AliAnalysisTask(name, "AnalysisTaskSE"),
93     fDebug(0),
94     fEntry(0),
95     fInputEvent(0x0),
96     fESDfriend(0x0),
97     fInputHandler(0x0),
98     fOutputAOD(0x0),
99     fMCEvent(0x0),
100     fTreeA(0x0),
101     fCurrentRunNumber(-1),
102     fHistosQA(0x0),
103     fOfflineTriggerMask(0),
104     fMultiInputHandler(0),
105     fMCEventHandler(0)
106 {
107   // Default constructor
108     DefineInput (0, TChain::Class());
109     DefineOutput(0,  TTree::Class());
110 }
111
112 AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
113     AliAnalysisTask(obj),
114     fDebug(0),
115     fEntry(0),
116     fInputEvent(0x0),
117     fESDfriend(0x0),
118     fInputHandler(0x0),
119     fOutputAOD(0x0),
120     fMCEvent(0x0),
121     fTreeA(0x0),
122     fCurrentRunNumber(-1),
123     fHistosQA(0x0),
124     fOfflineTriggerMask(0),
125     fMultiInputHandler(obj.fMultiInputHandler),
126     fMCEventHandler(obj.fMCEventHandler)
127 {
128 // Copy constructor
129     fDebug            = obj.fDebug;
130     fEntry            = obj.fEntry;
131     fInputEvent       = obj.fInputEvent;
132     fESDfriend        = obj.fESDfriend;
133     fInputHandler     = obj.fInputHandler;
134     fOutputAOD        = obj.fOutputAOD;
135     fMCEvent          = obj.fMCEvent;
136     fTreeA            = obj.fTreeA;    
137     fCurrentRunNumber = obj.fCurrentRunNumber;
138     fHistosQA         = obj.fHistosQA;
139
140 }
141
142
143 AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
144 {
145 // Assignment
146   if(&other == this) return *this;
147   AliAnalysisTask::operator=(other);
148
149     AliAnalysisTask::operator=(other);
150     fDebug            = other.fDebug;
151     fEntry            = other.fEntry;
152     fInputEvent       = other.fInputEvent;
153     fESDfriend        = other.fESDfriend;
154     fInputHandler     = other.fInputHandler;
155     fOutputAOD        = other.fOutputAOD;
156     fMCEvent          = other.fMCEvent;
157     fTreeA            = other.fTreeA;    
158     fCurrentRunNumber = other.fCurrentRunNumber;
159     fHistosQA         = other.fHistosQA;
160     fOfflineTriggerMask = other.fOfflineTriggerMask;
161     fMultiInputHandler  = other.fMultiInputHandler;
162     fMCEventHandler     = other.fMCEventHandler;
163     return *this;
164 }
165
166 //______________________________________________________________________________
167 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
168 {
169 // Connect the input data
170     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
171
172    // Connect input handlers (multi input handler is handled)
173     ConnectMultiHandler();
174     
175     if (fInputHandler) {
176         if ((fInputHandler->GetTree())->GetBranch("ESDfriend."))
177             fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
178
179         fInputEvent = fInputHandler->GetEvent();
180     } else if( fMCEvent ) {
181          AliWarning("No Input Event Handler connected, only MC Truth Event Handler") ; 
182     } else {
183          AliError("No Input Event Handler connected") ; 
184          return ; 
185     }
186     // Disconnect multi handler
187     DisconnectMultiHandler();
188 }
189
190 void AliAnalysisTaskSE::CreateOutputObjects()
191 {
192 // Create the output container
193 //
194 //  Default AOD
195     if (fDebug > 1) printf("AnalysisTaskSE::CreateOutPutData() \n");
196
197     AliAODHandler* handler = dynamic_cast<AliAODHandler*> 
198          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
199     
200     Bool_t merging = kFALSE;
201     AliAODInputHandler* aodIH = static_cast<AliAODInputHandler*>((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
202     if (aodIH) {
203         if (aodIH->GetMergeEvents()) merging = kTRUE;
204     }
205
206     // Check if AOD replication has been required
207     if (handler) {
208         fOutputAOD   = handler->GetAOD();
209         fTreeA = handler->GetTree();
210         if (fOutputAOD && !(handler->IsStandard())) {
211             if ((handler->NeedsHeaderReplication() || merging) && !(fgAODHeader)) 
212                 {
213                  if (fDebug > 1) AliInfo("Replicating header");
214                  fgAODHeader = new AliAODHeader;
215                  handler->AddBranch("AliAODHeader", &fgAODHeader);
216                 }
217             if ((handler->NeedsTracksBranchReplication() || merging) && !(fgAODTracks))      
218             {   
219                 if (fDebug > 1) AliInfo("Replicating track branch\n");
220                 fgAODTracks = new TClonesArray("AliAODTrack",500);
221                 fgAODTracks->SetName("tracks");
222                 handler->AddBranch("TClonesArray", &fgAODTracks);
223             }    
224             if ((handler->NeedsVerticesBranchReplication() || merging) && !(fgAODVertices))
225             {
226                 if (fDebug > 1) AliInfo("Replicating vertices branch\n");
227                 fgAODVertices = new TClonesArray("AliAODVertex",500);
228                 fgAODVertices->SetName("vertices");
229                 handler->AddBranch("TClonesArray", &fgAODVertices);
230             }   
231             if ((handler->NeedsV0sBranchReplication()) && !(fgAODV0s))    
232             {   
233                 if (fDebug > 1) AliInfo("Replicating V0s branch\n");
234                 fgAODV0s = new TClonesArray("AliAODv0",500);
235                 fgAODV0s->SetName("v0s");
236                 handler->AddBranch("TClonesArray", &fgAODV0s);
237             }
238             if ((handler->NeedsTrackletsBranchReplication()) && !(fgAODTracklets))        
239             {   
240                 if (fDebug > 1) AliInfo("Replicating Tracklets branch\n");
241                 fgAODTracklets = new AliAODTracklets("tracklets","tracklets");
242                 handler->AddBranch("AliAODTracklets", &fgAODTracklets);
243             }
244             if ((handler->NeedsPMDClustersBranchReplication()) && !(fgAODPMDClusters))    
245             {   
246                 if (fDebug > 1) AliInfo("Replicating PMDClusters branch\n");
247                 fgAODPMDClusters = new TClonesArray("AliAODPmdCluster",500);
248                 fgAODPMDClusters->SetName("pmdClusters");
249                 handler->AddBranch("TClonesArray", &fgAODPMDClusters);
250             }
251             if ((handler->NeedsJetsBranchReplication() || merging) && !(fgAODJets))       
252             {   
253                 if (fDebug > 1) AliInfo("Replicating Jets branch\n");
254                 fgAODJets = new TClonesArray("AliAODJet",500);
255                 fgAODJets->SetName("jets");
256                 handler->AddBranch("TClonesArray", &fgAODJets);
257             }
258             if ((handler->NeedsFMDClustersBranchReplication()) && !(fgAODFMDClusters))    
259             {   
260                 AliInfo("Replicating FMDClusters branch\n");
261                 fgAODFMDClusters = new TClonesArray("AliAODFmdCluster",500);
262                 fgAODFMDClusters->SetName("fmdClusters");
263                 handler->AddBranch("TClonesArray", &fgAODFMDClusters);
264             }
265             if ((handler->NeedsCaloClustersBranchReplication() || merging) && !(fgAODCaloClusters))       
266             {   
267                 if (fDebug > 1) AliInfo("Replicating CaloClusters branch\n");
268                 fgAODCaloClusters = new TClonesArray("AliAODCaloCluster",500);
269                 fgAODCaloClusters->SetName("caloClusters");
270                 handler->AddBranch("TClonesArray", &fgAODCaloClusters);
271
272                 fgAODEmcalCells = new AliAODCaloCells("emcalCells","emcalCells",AliVCaloCells::kEMCALCell);
273                 handler->AddBranch("AliAODCaloCells", &fgAODEmcalCells);
274
275                 fgAODPhosCells = new AliAODCaloCells("phosCells","phosCells",AliVCaloCells::kPHOSCell);
276                 handler->AddBranch("AliAODCaloCells", &fgAODPhosCells);
277                 }
278             if ((handler->NeedsCaloTriggerBranchReplication() || merging) && !(fgAODEMCALTrigger))        
279             {   
280                 if (fDebug > 1) AliInfo("Replicating EMCAL Calo Trigger branches\n");
281                 fgAODEMCALTrigger = new AliAODCaloTrigger("emcalTrigger","emcalTrigger");
282                 handler->AddBranch("AliAODCaloTrigger", &fgAODEMCALTrigger);
283                 }
284                 if ((handler->NeedsCaloTriggerBranchReplication() || merging) && !(fgAODPHOSTrigger))     
285                 {   
286                 if (fDebug > 1) AliInfo("Replicating PHOS Calo Trigger branches\n");
287                 fgAODPHOSTrigger = new AliAODCaloTrigger("phosTrigger","phosTrigger");
288                 handler->AddBranch("AliAODCaloTrigger", &fgAODPHOSTrigger);
289             }
290             if ((handler->NeedsMCParticlesBranchReplication() || merging) && !(fgAODMCParticles))         
291             {   
292                 if (fDebug > 1) AliInfo("Replicating MCParticles branch\n");
293                 fgAODMCParticles = new TClonesArray("AliAODMCParticle",500);
294                 fgAODMCParticles->SetName("mcparticles");
295                 handler->AddBranch("TClonesArray", &fgAODMCParticles);
296             }
297             if ((handler->NeedsDimuonsBranchReplication() || merging) && !(fgAODDimuons))      
298             {   
299                 if (fDebug > 1) AliInfo("Replicating dimuon branch\n");
300                 fgAODDimuons = new TClonesArray("AliAODDimuon",0);
301                 fgAODDimuons->SetName("dimuons");
302                 handler->AddBranch("TClonesArray", &fgAODDimuons);
303             }    
304
305             // cache the pointerd in the AODEvent
306             fOutputAOD->GetStdContent();
307         }
308     }
309     ConnectMultiHandler();
310     UserCreateOutputObjects();
311     DisconnectMultiHandler();
312 }
313
314 void AliAnalysisTaskSE::Exec(Option_t* option)
315 {
316 //
317 // Exec analysis of one event
318
319     ConnectMultiHandler();
320
321     if ( fDebug >= 10)
322       printf("Task is active %5d\n", IsActive());
323     
324     if (fDebug > 1) AliInfo("AliAnalysisTaskSE::Exec() \n");
325 //
326     AliAODHandler* handler = dynamic_cast<AliAODHandler*> 
327         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
328
329     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
330 //
331 // Was event selected ? If no event selection mechanism, the event SHOULD be selected (AG)
332     UInt_t isSelected = AliVEvent::kAny;
333     if( fInputHandler && (fInputHandler->GetEventSelection() || aodH)) {
334       // Get the actual offline trigger mask for the event and AND it with the
335       // requested mask. If no mask requested select by default the event.
336       if (fOfflineTriggerMask)
337          isSelected = fOfflineTriggerMask & fInputHandler->IsEventSelected();
338     }
339 //  Functionality below moved in the filter tasks (AG)
340 //    if (handler) handler->SetFillAOD(isSelected);
341
342     if( fInputHandler ) {
343         fEntry = fInputHandler->GetReadEntry();
344         fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
345     }
346     
347
348 // Notify the change of run number
349     if (InputEvent() && (InputEvent()->GetRunNumber() != fCurrentRunNumber)) {
350         fCurrentRunNumber = InputEvent()->GetRunNumber();
351         NotifyRun();
352     }    
353            
354     else if( fMCEvent )
355        fEntry = fMCEvent->Header()->GetEvent(); 
356     if ( !((Entry()-1)%100) && fDebug > 0) 
357          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
358
359     
360     
361
362     if (handler && aodH) {
363         fMCEvent = aodH->MCEvent();
364         Bool_t merging = aodH->GetMergeEvents();
365       
366   // Do not analyze merged events if last embedded file has less events than normal event, 
367   // skip analysis after last embeded event 
368   if(merging){
369     if(aodH->GetReadEntry() + aodH->GetMergeOffset() >= aodH->GetTreeToMerge()->GetEntriesFast()){
370       //printf("Skip Entry %lld, Offset %d, Tree Entries %d\n",aodH->GetReadEntry(),aodH->GetMergeOffset(), aodH->GetTreeToMerge()->GetEntries());
371           
372       // Do I need to add the lines before the return?
373       // Added protection in case the derived task is not an AOD producer.
374       AliAnalysisDataSlot *out0 = GetOutputSlot(0);
375       if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
376           
377       DisconnectMultiHandler();
378           
379       return;
380     }
381     //else   printf("MERGE Entry %lld, Offset %d, Tree Entries %d\n",aodH->GetReadEntry(),aodH->GetMergeOffset(), aodH->GetTreeToMerge()->GetEntries());
382   }
383       
384         AliAODEvent* aod = dynamic_cast<AliAODEvent*>(InputEvent());
385
386         if (aod && !(handler->IsStandard()) && !(handler->AODIsReplicated())) {
387             if ((handler->NeedsHeaderReplication() || merging) && (fgAODHeader))
388             {
389               // copy the contents by assigment
390               *fgAODHeader =  *(aod->GetHeader());
391             }
392             if ((handler->NeedsTracksBranchReplication() || (merging &&  aodH->GetMergeTracks())) && (fgAODTracks))
393             {
394                 TClonesArray* tracks = aod->GetTracks();
395                 new (fgAODTracks) TClonesArray(*tracks);
396             }
397             if ((handler->NeedsVerticesBranchReplication() || merging) && (fgAODVertices))
398             {
399                 TClonesArray* vertices = aod->GetVertices();
400                 new (fgAODVertices) TClonesArray(*vertices);
401             }
402             if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
403             {
404                 TClonesArray* v0s = aod->GetV0s();
405                 new (fgAODV0s) TClonesArray(*v0s);
406             }
407             if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
408             {
409               *fgAODTracklets = *aod->GetTracklets();
410             }
411             if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
412             {
413                 TClonesArray* pmdClusters = aod->GetPmdClusters();
414                 new (fgAODPMDClusters) TClonesArray(*pmdClusters);
415             }
416             if ((handler->NeedsJetsBranchReplication() || (merging &&aodH->GetMergeTracks())) && (fgAODJets))
417             {
418                 TClonesArray* jets = aod->GetJets();
419                 new (fgAODJets) TClonesArray(*jets);
420             }
421             if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
422             {
423                 TClonesArray* fmdClusters = aod->GetFmdClusters();
424                 new (fgAODFMDClusters) TClonesArray(*fmdClusters);
425             }
426             if ((handler->NeedsCaloClustersBranchReplication() || 
427                  (merging && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters()))) 
428                 && (fgAODCaloClusters))
429             {
430                 TClonesArray* caloClusters = aod->GetCaloClusters();
431                 new (fgAODCaloClusters) TClonesArray(*caloClusters);
432             }
433
434             if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
435             {
436                 TClonesArray* mcParticles = (TClonesArray*) (aod->FindListObject("mcparticles"));
437                 if( mcParticles )
438                   new (fgAODMCParticles) TClonesArray(*mcParticles);
439             }
440             
441             if ((handler->NeedsDimuonsBranchReplication() || (merging && aodH->GetMergeTracks())) && (fgAODDimuons))
442             {
443                 fgAODDimuons->Clear();
444                 TClonesArray& dimuons = *fgAODDimuons;
445                 TClonesArray& tracksnew = *fgAODTracks;
446                 
447                 Int_t nMuonTrack[100]; 
448                 for(Int_t imuon = 0; imuon < 100; imuon++) nMuonTrack[imuon] = 0;
449                 Int_t nMuons=0;
450                 for(Int_t ii=0; ii < fgAODTracks->GetEntries(); ii++){
451                     AliAODTrack *track = (AliAODTrack*) fgAODTracks->At(ii);
452                     if(track->IsMuonTrack()) {
453                         nMuonTrack[nMuons]= ii;
454                         nMuons++;
455                     }  
456                 }
457                 Int_t jDimuons=0;
458                 if(nMuons >= 2){
459                     for(Int_t i = 0; i < nMuons; i++){
460                         Int_t index0 = nMuonTrack[i];
461                         for(Int_t j = i+1; j < nMuons; j++){
462                             Int_t index1 = nMuonTrack[j];
463                             tracksnew.At(index0)->ResetBit(kIsReferenced);
464                             tracksnew.At(index0)->SetUniqueID(0); 
465                             tracksnew.At(index1)->ResetBit(kIsReferenced);
466                             tracksnew.At(index1)->SetUniqueID(0);
467                             new(dimuons[jDimuons++]) AliAODDimuon(tracksnew.At(index0),tracksnew.At(index1));
468                         }
469                     }    
470                 }
471             }
472             // Additional merging if needed
473             if (merging) {
474               Int_t nc;
475
476                 // mcParticles
477                 TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
478                 if( mcparticles ){
479                   Int_t npart = mcparticles->GetEntries();
480                   nc = fgAODMCParticles->GetEntries();
481                   
482                   for (Int_t i = 0; i < npart; i++) {
483                     AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
484                     new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
485                   }
486                 }
487
488                 // tracks
489                 TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
490                 if(tracks && aodH->GetMergeTracks()){
491                   Int_t ntr = tracks->GetEntries();
492                   nc  = fgAODTracks->GetEntries();      
493                   for (Int_t i = 0; i < ntr; i++) {
494                     AliAODTrack*    track = (AliAODTrack*) tracks->At(i);
495                     AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
496                     newtrack->SetLabel(newtrack->GetLabel() + fgAODMCParticles->GetEntries());
497                   }
498                   
499                   for (Int_t i = 0; i < nc; i++) 
500                     {
501                       AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
502                       track->ResetBit(kIsReferenced);
503                       track->SetUniqueID(0);
504                     }
505                 }
506                 
507                 // clusters
508                 TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
509                 if( clusters  && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters())) {
510                   Int_t ncl  = clusters->GetEntries();
511                   nc         =  fgAODCaloClusters->GetEntries();
512                   for (Int_t i = 0; i < ncl; i++) {
513                     AliAODCaloCluster*    cluster = (AliAODCaloCluster*) clusters->At(i);
514                     if(cluster->IsEMCAL() && !aodH->GetMergeEMCALClusters() ) continue;
515                     if(cluster->IsPHOS()  && !aodH->GetMergePHOSClusters()  ) continue;   
516                     new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
517                   }
518                 }
519
520                 // EMCAL cells
521                 //*fgAODEmcalCells =  *(aod->GetEMCALCells()); // This will be valid after 10.Mar.2011.
522                 if(aodH->GetMergeEMCALCells()) {
523                     AliAODCaloCells* copycells = aod->GetEMCALCells();
524                     fgAODEmcalCells->CreateContainer(copycells->GetNumberOfCells());
525                     nc  = copycells->GetNumberOfCells();
526                     while( nc-- ){ fgAODEmcalCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc)); }
527
528                     AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
529                     if( cellsA ){
530                         Int_t ncells  = cellsA->GetNumberOfCells();
531                         nc = fgAODEmcalCells->GetNumberOfCells();
532                         for (Int_t i  = 0; i < ncells; i++) {
533                             Int_t cn  = cellsA->GetCellNumber(i);
534                             Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
535                             if (pos >= 0) {
536                                 Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
537                                 fgAODEmcalCells->SetCell(pos, cn, amp);
538                             } else {
539                                 AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODEmcalCells);
540                                 fgAODEmcalCells->CreateContainer(nc+1);
541                                 Int_t nn = copycells1->GetNumberOfCells();
542                                 while( nn-- ){ fgAODEmcalCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn)); }
543                                 fgAODEmcalCells->SetCell(nc++,cn,cellsA->GetAmplitude(i));
544                                 delete copycells1;
545                             }
546                         }
547                         fgAODEmcalCells->Sort();
548                     }
549                 } // merge emcal cells
550                 
551                 
552                 // PHOS cells
553                 //*fgAODPhosCells =  *(aod->GetPHOSCells()); // This will be valid after 10.Mar.2011.
554                 if(aodH->GetMergePHOSCells()) {
555                     AliAODCaloCells* copycells = aod->GetPHOSCells();
556                     fgAODPhosCells->CreateContainer(copycells->GetNumberOfCells());
557                     nc  = copycells->GetNumberOfCells();
558                     while( nc-- ){ fgAODPhosCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc)); }
559                     AliAODCaloCells* cellsP = aodH->GetEventToMerge()->GetPHOSCells();
560                     if( cellsP ){
561                         Int_t ncellsP  = cellsP->GetNumberOfCells();
562                         nc = fgAODPhosCells->GetNumberOfCells();
563                         
564                         for (Int_t i  = 0; i < ncellsP; i++) {
565                             Int_t cn  = cellsP->GetCellNumber(i);
566                             Int_t pos = fgAODPhosCells->GetCellPosition(cn);
567                             if (pos >= 0) {
568                                 Double_t amp = cellsP->GetAmplitude(i) + fgAODPhosCells->GetAmplitude(pos);
569                                 fgAODPhosCells->SetCell(pos, cn, amp);
570                             } else {
571                                 AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODPhosCells);
572                                 fgAODPhosCells->CreateContainer(nc+1);
573                                 Int_t nn = copycells1->GetNumberOfCells();
574                                 while( nn-- ){ fgAODPhosCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn)); }
575                                 fgAODPhosCells->SetCell(nc++,cn,cellsP->GetAmplitude(i));
576                                 delete copycells1;
577                             }
578                         }
579                         fgAODPhosCells->Sort();
580                     }
581                 } // Merge PHOS Cells
582                 
583                 if (aodH->GetMergeEMCALTrigger()) 
584                 {
585                         Int_t   EMCALts[48][64], px, py, ts;
586                         Float_t EMCALfo[48][64], am;
587                         
588                         for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++) 
589                         {
590                                 EMCALts[i][j] = 0;
591                                 EMCALfo[i][j] = 0.;
592                         }
593                         
594                         AliAODCaloTrigger& trg0 = *(aod->GetCaloTrigger("EMCAL"));
595                         
596                         trg0.Reset();
597                         while (trg0.Next())
598                         {
599                                 trg0.GetPosition(px, py);
600                                 
601                                 if (px > -1 && py > -1) 
602                                 {
603                                         trg0.GetL1TimeSum(ts);
604                                         if (ts > -1) EMCALts[px][py] += ts;
605                                         
606                                         trg0.GetAmplitude(am);
607                                         if (am > -1) EMCALfo[px][py] += am;
608                                 }
609                         }
610                         
611                         AliAODCaloTrigger& trg1 = *((aodH->GetEventToMerge())->GetCaloTrigger("EMCAL"));
612                         
613                         trg1.Reset();
614                         while (trg1.Next())
615                         {
616                                 trg1.GetPosition(px, py);
617                                 
618                                 if (px > -1 && py > -1) 
619                                 {
620                                         trg1.GetL1TimeSum(ts);
621                                         if (ts > -1) EMCALts[px][py] += ts;
622                                         
623                                         trg1.GetAmplitude(am);
624                                         if (am > -1) EMCALfo[px][py] += am;
625                                 }
626                         }
627                         
628                         int nEntries = 0;
629                         for (Int_t i = 0; i < 48; i++) 
630                                 for (Int_t j = 0; j < 64; j++) 
631                                         if (EMCALts[i][j] || EMCALfo[i][j]) nEntries++;
632                 
633                         fgAODEMCALTrigger->Allocate(nEntries);
634                         Int_t L0times[10]; for (int i = 0; i < 10; i++) L0times[i] = -1;
635                 
636                         for (Int_t i = 0; i < 48; i++) 
637                                 for (Int_t j = 0; j < 64; j++) 
638                                         if (EMCALts[i][j] || EMCALfo[i][j]) 
639                                                 fgAODEMCALTrigger->Add(i, j, EMCALfo[i][j], -1., L0times, 0, EMCALts[i][j], 0);
640                 }
641                 
642                 if (aodH->GetMergePHOSTrigger()) 
643                 {
644                         // To be implemented by PHOS
645                 }
646             } // merging
647             
648             handler->SetAODIsReplicated();
649         }
650     }
651
652
653 // Call the user analysis    
654     if (!fMCEventHandler) {
655         if (isSelected) 
656             UserExec(option);
657     } else {
658         if (isSelected && (fMCEventHandler->InitOk())) 
659             UserExec(option);
660     }
661     
662 // Added protection in case the derived task is not an AOD producer.
663     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
664     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
665
666     DisconnectMultiHandler();
667 }
668
669 const char* AliAnalysisTaskSE::CurrentFileName()
670 {
671 // Returns the current file name    
672     if( fInputHandler )
673       return fInputHandler->GetTree()->GetCurrentFile()->GetName();
674     else if( fMCEvent )
675       return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
676     else return "";
677 }
678
679 void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj, const char *fname)
680 {
681     // Add a new branch to the aod tree
682     AliAODHandler* handler = dynamic_cast<AliAODHandler*> 
683         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
684     if (handler) {
685         handler->AddBranch(cname, addobj, fname);
686     }
687 }
688
689 Bool_t AliAnalysisTaskSE::IsStandardAOD() const
690 {
691 // Check if the output AOD handler is configured for standard or delta AOD.
692 // Users should first check that AODEvent() returns non-null.
693     AliAODHandler* handler = dynamic_cast<AliAODHandler*> 
694          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
695     if (!handler) {
696        Error("IsStandardAOD", "No AOD handler. Please use AODEvent() to check this first");
697        return kTRUE;
698     }
699     return handler->IsStandard();   
700 }
701
702 Bool_t AliAnalysisTaskSE::Notify()
703 {
704     return (UserNotify());
705 }
706
707 const AliEventTag *AliAnalysisTaskSE::EventTag() const
708 {
709 // Returns tag for the current event, if any. The return value should always be checked by the user.
710    if (!fInputHandler) {
711       Error("EventTag", "Input handler not yet available. Call this in UserExec");
712       return NULL;
713    }
714    return fInputHandler->GetEventTag();
715 }
716
717 void AliAnalysisTaskSE::LoadBranches() const
718 {
719 // Load all branches declared in fBranchNames data member of the parent class.
720 // Should be called in UserExec.
721   if (!fInputHandler) {
722      Error("LoadBranches", "Input handler not available yet. Call this in UserExec");
723      return;
724   }
725   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
726   if (mgr->GetAutoBranchLoading()) return;
727   TString taskbranches;
728   GetBranches(fInputHandler->GetDataType(), taskbranches);
729   if (taskbranches.IsNull()) return;
730   TObjArray *arr = taskbranches.Tokenize(",");
731   TIter next(arr);
732   TObject *obj;
733   while ((obj=next())) mgr->LoadBranch(obj->GetName());
734 }
735
736
737 //_________________________________________________________________________________________________
738 void AliAnalysisTaskSE::ConnectMultiHandler()
739 {
740    //
741    // Connect MultiHandler
742    //
743    fInputHandler = (AliInputEventHandler *)((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
744    fMultiInputHandler = dynamic_cast<AliMultiInputEventHandler *>(fInputHandler);
745    if (fMultiInputHandler) {
746       fInputHandler = dynamic_cast<AliInputEventHandler *>(fMultiInputHandler->GetFirstInputEventHandler());
747       fMCEventHandler = dynamic_cast<AliMCEventHandler *>(fMultiInputHandler->GetFirstMCEventHandler());
748    } else { 
749       fMCEventHandler = dynamic_cast<AliMCEventHandler *>((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
750    }
751    if (fMCEventHandler) fMCEvent = fMCEventHandler->MCEvent();
752 }
753
754 //_________________________________________________________________________________________________
755 void AliAnalysisTaskSE::DisconnectMultiHandler()
756 {
757    //
758    // Disconnect MultiHandler
759    //
760    if (fMultiInputHandler) fInputHandler = fMultiInputHandler;
761 }