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