+ Int_t ievent;
+ for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
+ rl->GetEvent(ievent);
+ TTree * treeS = emcalLoader->TreeS();
+ if ( !treeS ) {
+ emcalLoader->MakeSDigitsContainer();
+ treeS = emcalLoader->TreeS();
+ }
+
+ TClonesArray * sdigits = emcalLoader->SDigits() ;
+ sdigits->Clear("C");
+
+ Int_t nSdigits = 0 ;
+ Int_t iHit, iTrack, iSDigit;
+
+ AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
+
+ TTree *treeH = emcalLoader->TreeH();
+ if (treeH) {
+ Int_t nTrack = treeH->GetEntries(); // TreeH has array of hits for every primary
+ TBranch * branchH = treeH->GetBranch("EMCAL");
+ //if(fHits)fHits->Clear();
+ branchH->SetAddress(&fHits);
+ for (iTrack = 0; iTrack < nTrack; iTrack++) {
+ branchH->GetEntry(iTrack);
+
+ if(fHits){
+
+ Int_t nHit = fHits->GetEntriesFast();
+ for(iHit = 0; iHit< nHit;iHit++){
+
+ AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(fHits->At(iHit)) ;
+ AliEMCALDigit * curSDigit = 0 ;
+ AliEMCALDigit * sdigit = 0 ;
+ Bool_t newsdigit = kTRUE;
+
+ // hit->GetId() - Absolute Id number EMCAL segment
+ if(hit){
+ if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
+ energy = hit->GetEnergy() * fSampling; // 23-nov-04
+ if(energy > fECPrimThreshold )
+ // Assign primary number only if deposited energy is significant
+ curSDigit = new AliEMCALDigit(hit->GetPrimary(),
+ hit->GetIparent(), hit->GetId(),
+ Digitize(energy), hit->GetTime(),kFALSE,
+ -1, 0,0,energy ) ;
+ else
+ curSDigit = new AliEMCALDigit(-1,
+ -1,
+ hit->GetId(),
+ Digitize(energy), hit->GetTime(),kFALSE,
+ -1, 0,0,energy ) ;
+ } else {
+ Warning("Digitize"," abs id %i is bad \n", hit->GetId());
+ newsdigit = kFALSE;
+ curSDigit = 0;
+ }
+
+ if(curSDigit != 0){
+ for(Int_t check= 0; check < nSdigits ; check++) {
+ sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
+ if(sdigit){
+ if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
+ *sdigit = *sdigit + *curSDigit;
+ newsdigit = kFALSE;
+ }
+ }// sdigit exists
+ else {
+ AliWarning("Sdigit do not exist");
+ newsdigit = kFALSE;
+ }// sdigit does not exist
+ }//sdigit loop
+ }// currsdigit exists
+
+ if (newsdigit) {
+ new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
+ nSdigits++ ;
+ }
+ delete curSDigit ;
+
+ }// hit exists
+ else AliFatal("Hit is NULL!");
+
+ } // loop over all hits (hit = deposited energy/entering particle)
+
+ }//fHits is not NULL
+ else AliFatal("fHit is NULL!");
+
+ sdigits->Sort() ;
+
+ nSdigits = sdigits->GetEntriesFast() ;
+ fSDigitsInRun += nSdigits ;
+
+ for (iSDigit = 0 ; iSDigit < sdigits->GetEntriesFast() ; iSDigit++) {
+ AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(iSDigit)) ;
+ if(sdigit)sdigit->SetIndexInList(iSDigit) ;
+ else AliFatal("sdigit is NULL!");
+ }
+ if(fHits)fHits->Clear();
+ }//track loop
+ }// tree exists
+
+ // Now write SDigits
+
+ Int_t bufferSize = 32000 ;
+ TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
+ if (sdigitsBranch)
+ sdigitsBranch->SetAddress(&sdigits);
+ else
+ treeS->Branch("EMCAL",&sdigits,bufferSize);
+
+ treeS->Fill();
+
+ emcalLoader->WriteSDigits("OVERWRITE");
+
+ //NEXT - SDigitizer
+ //emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
+
+ if(strstr(option,"deb"))
+ PrintSDigits(option) ;
+ }
+
+ Unload();
+
+ if(strstr(option,"tim")){
+ gBenchmark->Stop("EMCALSDigitizer");
+ printf("\n Digitize: took %f seconds for SDigitizing %f seconds per event\n",
+ gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
+ }