+ if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
+ {
+ TClonesArray* mcParticles = (TClonesArray*) (aod->FindListObject("mcparticles"));
+ if( mcParticles )
+ new (fgAODMCParticles) TClonesArray(*mcParticles);
+ }
+
+ if ((handler->NeedsDimuonsBranchReplication() || (merging && aodH->GetMergeTracks())) && (fgAODDimuons))
+ {
+ fgAODDimuons->Clear();
+ TClonesArray& dimuons = *fgAODDimuons;
+ TClonesArray& tracksnew = *fgAODTracks;
+
+ Int_t nMuonTrack[100];
+ for(Int_t imuon = 0; imuon < 100; imuon++) nMuonTrack[imuon] = 0;
+ Int_t nMuons=0;
+ for(Int_t ii=0; ii < fgAODTracks->GetEntries(); ii++){
+ AliAODTrack *track = (AliAODTrack*) fgAODTracks->At(ii);
+ if(track->IsMuonTrack()) {
+ nMuonTrack[nMuons]= ii;
+ nMuons++;
+ }
+ }
+ Int_t jDimuons=0;
+ if(nMuons >= 2){
+ for(Int_t i = 0; i < nMuons; i++){
+ Int_t index0 = nMuonTrack[i];
+ for(Int_t j = i+1; j < nMuons; j++){
+ Int_t index1 = nMuonTrack[j];
+ tracksnew.At(index0)->ResetBit(kIsReferenced);
+ tracksnew.At(index0)->SetUniqueID(0);
+ tracksnew.At(index1)->ResetBit(kIsReferenced);
+ tracksnew.At(index1)->SetUniqueID(0);
+ new(dimuons[jDimuons++]) AliAODDimuon(tracksnew.At(index0),tracksnew.At(index1));
+ }
+ }
+ }
+ }
+ if ((handler->NeedsHMPIDBranchReplication()) && (fgAODHmpidRings))
+ {
+ TClonesArray* hmpidRings = aod->GetHMPIDrings();
+ new (fgAODHmpidRings) TClonesArray(*hmpidRings);
+ }
+
+
+
+ // Additional merging if needed
+ if (merging) {
+ Int_t nc;
+
+ // mcParticles
+ TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
+ if( mcparticles ){
+ Int_t npart = mcparticles->GetEntries();
+ nc = fgAODMCParticles->GetEntries();
+
+ for (Int_t i = 0; i < npart; i++) {
+ AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
+ new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
+ }
+ }
+
+ // tracks
+ TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
+ if(tracks && aodH->GetMergeTracks()){
+ Int_t ntr = tracks->GetEntries();
+ nc = fgAODTracks->GetEntries();
+ for (Int_t i = 0; i < ntr; i++) {
+ AliAODTrack* track = (AliAODTrack*) tracks->At(i);
+ AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
+ newtrack->SetLabel(newtrack->GetLabel() + fgAODMCParticles->GetEntries());
+ }
+
+ for (Int_t i = 0; i < nc; i++)
+ {
+ AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
+ track->ResetBit(kIsReferenced);
+ track->SetUniqueID(0);
+ }
+ }
+
+ // clusters
+ TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
+ if( clusters && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters())) {
+ Int_t ncl = clusters->GetEntries();
+ nc = fgAODCaloClusters->GetEntries();
+ for (Int_t i = 0; i < ncl; i++) {
+ AliAODCaloCluster* cluster = (AliAODCaloCluster*) clusters->At(i);
+ if(cluster->IsEMCAL() && !aodH->GetMergeEMCALClusters() ) continue;
+ if(cluster->IsPHOS() && !aodH->GetMergePHOSClusters() ) continue;
+ new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
+ }
+ }
+
+ // EMCAL cells
+ //*fgAODEmcalCells = *(aod->GetEMCALCells()); // This will be valid after 10.Mar.2011.
+ if(aodH->GetMergeEMCALCells())
+ {
+ AliAODCaloCells* copycells = aod->GetEMCALCells();
+ fgAODEmcalCells->CreateContainer(copycells->GetNumberOfCells());
+ nc = copycells->GetNumberOfCells();
+
+ while( nc-- ){ fgAODEmcalCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc),
+ copycells->GetTime(nc),copycells->GetMCLabel(nc),copycells->GetEFraction(nc)); }
+
+ AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
+ if( cellsA )
+ {
+ Int_t ncells = cellsA->GetNumberOfCells();
+ nc = fgAODEmcalCells->GetNumberOfCells();
+
+ for (Int_t i = 0; i < ncells; i++)
+ {
+ Int_t cn = cellsA->GetCellNumber(i);
+ Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
+
+ if (pos >= 0)
+ {
+ Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
+
+ //Check if it is MC, depending on that assing the mc lable, time and e fraction
+// Double_t time = 0;
+ Int_t mclabel =-1;
+ Double_t efrac = 0;
+ if(cellsA->GetMCLabel(i) >= 0 && fgAODEmcalCells->GetMCLabel(pos) < 0)
+ {
+ mclabel = cellsA->GetMCLabel(i) ;
+// time = fgAODEmcalCells->GetTime(pos) ; // Time from data
+ if(amp > 0) efrac = cellsA->GetAmplitude(i) / amp;
+ }
+ else if(fgAODEmcalCells->GetMCLabel(pos) >= 0 && cellsA->GetMCLabel(i) < 0)
+ {
+ mclabel = fgAODEmcalCells->GetMCLabel(pos) ;
+// time = cellsA->GetTime(i) ; // Time from data
+ if(amp > 0) efrac = fgAODEmcalCells->GetAmplitude(pos) / amp;
+ }
+ else
+ { // take all from input
+ mclabel = cellsA->GetMCLabel(i) ;
+// time = cellsA->GetTime(i) ;
+ if(amp > 0) efrac = cellsA->GetAmplitude(i) / amp;
+ }
+
+ fgAODEmcalCells->SetCell(pos, cn, amp,cellsA->GetTime(i),mclabel,efrac);
+
+ } else
+ {
+ AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODEmcalCells);
+ fgAODEmcalCells->CreateContainer(nc+1);
+ Int_t nn = copycells1->GetNumberOfCells();
+
+ while( nn-- ){ fgAODEmcalCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn),
+ copycells1->GetTime(nn),copycells1->GetMCLabel(nn),copycells1->GetEFraction(nn)); }
+
+ fgAODEmcalCells->SetCell(nc++,cn,cellsA->GetAmplitude(i),cellsA->GetTime(i), cellsA->GetMCLabel(i),1.);
+
+ delete copycells1;
+ }
+ }
+ fgAODEmcalCells->Sort();
+ }
+ } // merge emcal cells
+
+
+ // PHOS cells
+ //*fgAODPhosCells = *(aod->GetPHOSCells()); // This will be valid after 10.Mar.2011.
+ if(aodH->GetMergePHOSCells())
+ {
+ AliAODCaloCells* copycells = aod->GetPHOSCells();
+ fgAODPhosCells->CreateContainer(copycells->GetNumberOfCells());
+ nc = copycells->GetNumberOfCells();
+
+ while( nc-- ){ fgAODPhosCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc),
+ copycells->GetTime(nc),copycells->GetMCLabel(nc),copycells->GetEFraction(nc)); }
+
+ AliAODCaloCells* cellsP = aodH->GetEventToMerge()->GetPHOSCells();
+ if( cellsP )
+ {
+ Int_t ncellsP = cellsP->GetNumberOfCells();
+ nc = fgAODPhosCells->GetNumberOfCells();
+
+ for (Int_t i = 0; i < ncellsP; i++)
+ {
+ Int_t cn = cellsP->GetCellNumber(i);
+ Int_t pos = fgAODPhosCells->GetCellPosition(cn);
+
+ if (pos >= 0)
+ {
+ Double_t amp = cellsP->GetAmplitude(i) + fgAODPhosCells->GetAmplitude(pos);
+
+ //Check if it is MC, depending on that assing the mc lable, time and e fraction
+// Double_t time = 0;
+ Int_t mclabel =-1;
+ Double_t efrac = 0;
+ if(cellsP->GetMCLabel(i) >= 0 && fgAODPhosCells->GetMCLabel(pos) < 0)
+ {
+ mclabel = cellsP->GetMCLabel(i) ;
+// time = fgAODPhosCells->GetTime(pos) ; // Time from data
+ if(amp > 0) efrac = cellsP->GetAmplitude(i) / amp;
+ }
+ else if(fgAODPhosCells->GetMCLabel(pos) >= 0 && cellsP->GetMCLabel(i) < 0)
+ {
+ mclabel = fgAODPhosCells->GetMCLabel(pos) ;
+// time = cellsP->GetTime(i) ; // Time from data
+ if(amp > 0) efrac = fgAODPhosCells->GetAmplitude(pos) / amp;
+ }
+ else
+ { // take all from input
+ mclabel = cellsP->GetMCLabel(i) ;
+// time = cellsP->GetTime(i) ;
+ if(amp > 0) efrac = cellsP->GetAmplitude(i) / amp;
+ }
+
+ fgAODPhosCells->SetCell(pos, cn, amp,cellsP->GetTime(i),mclabel,efrac);
+
+ } else
+ {
+ AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODPhosCells);
+ fgAODPhosCells->CreateContainer(nc+1);
+ Int_t nn = copycells1->GetNumberOfCells();
+
+ while( nn-- ){ fgAODPhosCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn),
+ copycells1->GetTime(nn),copycells1->GetMCLabel(nn),copycells1->GetEFraction(nn)); }
+
+ fgAODPhosCells->SetCell(nc++,cn,cellsP->GetAmplitude(i),cellsP->GetTime(i), cellsP->GetMCLabel(i),1.);
+
+ delete copycells1;
+ }
+ }
+ fgAODPhosCells->Sort();
+ }
+ } // Merge PHOS Cells
+
+ if (aodH->GetMergeEMCALTrigger() && aod->GetCaloTrigger("EMCAL"))
+ {
+ Int_t tsEMCAL[48][64], px, py, ts;
+ Float_t foEMCAL[48][64], am;
+
+ for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++)
+ {
+ tsEMCAL[i][j] = 0;
+ foEMCAL[i][j] = 0.;
+ }
+
+ AliAODCaloTrigger& trg0 = *(aod->GetCaloTrigger("EMCAL"));
+ trg0.Reset();
+ while (trg0.Next())
+ {
+ trg0.GetPosition(px, py);
+
+ if (px > -1 && py > -1)
+ {
+ trg0.GetL1TimeSum(ts);
+ if (ts > -1) tsEMCAL[px][py] += ts;
+
+ trg0.GetAmplitude(am);
+ if (am > -1) foEMCAL[px][py] += am;
+ }
+ }
+
+ AliAODCaloTrigger& trg1 = *((aodH->GetEventToMerge())->GetCaloTrigger("EMCAL"));
+
+ trg1.Reset();
+ while (trg1.Next())
+ {
+ trg1.GetPosition(px, py);
+
+ if (px > -1 && py > -1)
+ {
+ trg1.GetL1TimeSum(ts);
+ if (ts > -1) tsEMCAL[px][py] += ts;
+
+ trg1.GetAmplitude(am);
+ if (am > -1) foEMCAL[px][py] += am;
+ }
+ }
+
+ int nEntries = 0;
+ for (Int_t i = 0; i < 48; i++)
+ for (Int_t j = 0; j < 64; j++)
+ if (tsEMCAL[i][j] || foEMCAL[i][j]) nEntries++;
+
+ fgAODEMCALTrigger->Allocate(nEntries);
+ Int_t timesL0[10]; for (int i = 0; i < 10; i++) timesL0[i] = -1;
+
+ for (Int_t i = 0; i < 48; i++)
+ for (Int_t j = 0; j < 64; j++)
+ if (tsEMCAL[i][j] || foEMCAL[i][j])
+ fgAODEMCALTrigger->Add(i, j, foEMCAL[i][j], -1., timesL0, 0, tsEMCAL[i][j], 0);
+ }
+
+ if (aodH->GetMergePHOSTrigger())
+ {
+ // To be implemented by PHOS
+ }
+ } // merging
+
+ handler->SetAODIsReplicated();