+ if(fgDigitsArr) fgDigitsArr->Clear("C");
+
+ TClonesArray *digitsTrg = new TClonesArray("AliEMCALTriggerRawDigit", 32 * 96);
+
+ Int_t bufsize = 32000;
+ digitsTree->Branch("EMCAL", &fgDigitsArr, bufsize);
+ digitsTree->Branch("EMTRG", &digitsTrg, bufsize);
+
+ //Skip calibration events do the rest
+ Bool_t doFit = kTRUE;
+ if ( !(GetRecParam()->FitLEDEvents()) && GetRecParam()->GetEventSpecie()==AliRecoParam::kCalib) doFit = kFALSE;
+ if (doFit){
+ //must be done here because, in constructor, option is not yet known
+ fgRawUtils->SetOption(GetOption());
+
+ fgRawUtils->SetRawFormatHighLowGainFactor(GetRecParam()->GetHighLowGainFactor());
+ fgRawUtils->SetRawFormatOrder(GetRecParam()->GetOrderParameter());
+ fgRawUtils->SetRawFormatTau(GetRecParam()->GetTau());
+ fgRawUtils->SetNoiseThreshold(GetRecParam()->GetNoiseThreshold());
+ fgRawUtils->SetNPedSamples(GetRecParam()->GetNPedSamples());
+ fgRawUtils->SetRemoveBadChannels(GetRecParam()->GetRemoveBadChannels());
+ fgRawUtils->SetFittingAlgorithm(GetRecParam()->GetFittingAlgorithm());
+ fgRawUtils->SetFALTROUsage(GetRecParam()->UseFALTRO());
+ fgRawUtils->SetTimeMin(GetRecParam()->GetTimeMin());
+ fgRawUtils->SetTimeMax(GetRecParam()->GetTimeMax());
+
+ fgRawUtils->Raw2Digits(rawReader,fgDigitsArr,fPedestalData,digitsTrg,fTriggerData);
+ }//skip calibration event
+ else{
+ AliDebug(1," Calibration Event, skip!");
+ }
+
+ digitsTree->Fill();
+ digitsTrg->Delete();
+ delete digitsTrg;
+
+}
+
+
+//____________________________________________________________________________
+void AliEMCALReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree,
+ AliESDEvent* esd) const
+{
+ // Called by AliReconstruct after Reconstruct() and global tracking and vertexing
+ // and V0
+ // Works on the current event
+ // printf(" ## AliEMCALReconstructor::FillESD() is started ### \n ");
+ //return;
+
+ //########################################
+ // Trigger
+ //########################################
+
+ Int_t v0M[2] = {0, 0};
+
+ AliESDVZERO* esdV0 = esd->GetVZEROData();
+
+ if (esdV0)
+ {
+ for (Int_t i = 0; i < 32; i++)
+ {
+ v0M[0] += (Int_t)esdV0->GetAdcV0C(i);
+ v0M[1] += (Int_t)esdV0->GetAdcV0A(i);
+ }
+ }
+ else
+ {
+ AliWarning("Cannot retrieve V0 ESD! Run w/ null V0 charges");
+ }
+
+ TClonesArray *trgDigits = new TClonesArray("AliEMCALTriggerRawDigit",1000);
+
+ TBranch *branchtrg = digitsTree->GetBranch("EMTRG");
+
+ if (!branchtrg)
+ {
+ AliError("Can't get the branch with the EMCAL trigger digits!");
+ return;
+ }
+
+ branchtrg->SetAddress(&trgDigits);
+ branchtrg->GetEntry(0);
+
+ // Note: fgTriggerProcessor reset done at the end of this method
+ fgTriggerProcessor->Digits2Trigger(trgDigits, v0M, fTriggerData);
+
+ // Fill ESD
+ AliESDCaloTrigger* trgESD = esd->GetCaloTrigger("EMCAL");
+
+ if (trgESD)
+ {
+ trgESD->Allocate(trgDigits->GetEntriesFast());
+
+ for (Int_t i = 0; i < trgDigits->GetEntriesFast(); i++)
+ {
+ AliEMCALTriggerRawDigit* rdig = (AliEMCALTriggerRawDigit*)trgDigits->At(i);
+
+ Int_t px, py;
+ if (fGeom->GetPositionInEMCALFromAbsFastORIndex(rdig->GetId(), px, py))
+ {
+ Int_t a = -1, t = -1, times[10];
+
+ rdig->GetMaximum(a, t);
+ rdig->GetL0Times(times);
+
+ trgESD->Add(px, py, a, t, times, rdig->GetNL0Times(), rdig->GetL1TimeSum());
+ }
+ }
+
+ trgESD->SetL1Threshold(0, fTriggerData->GetL1GammaThreshold());
+
+ trgESD->SetL1Threshold(1, fTriggerData->GetL1JetThreshold() );
+
+ for (Int_t i = 0; i < kTriggerTypeEnd; i++)
+ {
+ for (Int_t j = 0; j < 2; j++)
+ {
+ TClonesArray* patches = fTriggerData->GetPatches((TriggerType_t)i, j);
+
+ TIter NextPatch(patches);
+ while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)NextPatch())
+ {
+ TVector2 pos; p->Position(pos);
+ trgESD->SetTriggerBits((Int_t) pos.X(), (Int_t) pos.Y(), i, j);
+ }
+ }
+ }
+ }
+
+ // Resetting
+ fTriggerData->Reset();
+
+ //########################################
+ //##############Fill CaloCells###############
+ //########################################
+ ReadDigitsArrayFromTree(digitsTree);
+
+// TClonesArray *digits = new TClonesArray("AliEMCALDigit",1000);
+// TBranch *branchdig = digitsTree->GetBranch("EMCAL");
+// if (!branchdig) {
+// AliError("can't get the branch with the EMCAL digits !");
+// return;
+// }
+// branchdig->SetAddress(&digits);
+// digitsTree->GetEvent(0);
+ Int_t nDigits = fgDigitsArr->GetEntries(), idignew = 0 ;
+ AliDebug(1,Form("%d digits",nDigits));
+
+ AliESDCaloCells &emcCells = *(esd->GetEMCALCells());
+ emcCells.CreateContainer(nDigits);
+ emcCells.SetType(AliVCaloCells::kEMCALCell);
+ Float_t energy = 0;
+ for (Int_t idig = 0 ; idig < nDigits ; idig++) {
+ const AliEMCALDigit * dig = (const AliEMCALDigit*)fgDigitsArr->At(idig);
+ if(dig->GetAmplitude() > 0 ){
+ energy = fgClusterizer->Calibrate(dig->GetAmplitude(),dig->GetTime(),dig->GetId()); //TimeR or Time?
+ if(energy > 0){ //Digits tagged as bad (dead, hot, not alive) are set to 0 in calibrate, remove them
+ emcCells.SetCell(idignew,dig->GetId(),energy, dig->GetTime());
+ idignew++;
+ }
+ }
+ }
+ emcCells.SetNumberOfCells(idignew);
+ emcCells.Sort();
+
+ //------------------------------------------------------------
+ //-----------------CLUSTERS-----------------------------
+ //------------------------------------------------------------
+ clustersTree->SetBranchStatus("*",0); //disable all branches
+ clustersTree->SetBranchStatus("EMCALECARP",1); //Enable only the branch we need
+ if(fgClustersArr) fgClustersArr->Clear();
+ TBranch *branch = clustersTree->GetBranch("EMCALECARP");
+ branch->SetAddress(&fgClustersArr);
+ branch->GetEntry(0);
+ //clustersTree->GetEvent(0);
+
+ Int_t nClusters = fgClustersArr->GetEntries(), nClustersNew=0;
+ AliDebug(1,Form("%d clusters",nClusters));
+
+ //######################################################
+ //#######################TRACK MATCHING###############
+ //######################################################
+ //Fill list of integers, each one is index of track to which the cluster belongs.
+
+ // step 1 - initialize array of matched track indexes
+ Int_t *matchedTrack = new Int_t[nClusters];
+ for (Int_t iclus = 0; iclus < nClusters; iclus++)
+ matchedTrack[iclus] = -1; // neg. index --> no matched track
+
+ // step 2, change the flag for all matched clusters found in tracks
+ Int_t iemcalMatch = -1;
+ Int_t endtpc = esd->GetNumberOfTracks();
+ for (Int_t itrack = 0; itrack < endtpc; itrack++) {
+ AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
+ iemcalMatch = track->GetEMCALcluster();
+ if(iemcalMatch >= 0) matchedTrack[iemcalMatch] = itrack;
+ }
+
+ //########################################
+ //##############Fill CaloClusters#############
+ //########################################
+ for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
+ const AliEMCALRecPoint * clust = (const AliEMCALRecPoint*)fgClustersArr->At(iClust);
+ //if(clust->GetClusterType()== AliVCluster::kEMCALClusterv1) nRP++; else nPC++;