new design: derived from TTask
[u/mrichter/AliRoot.git] / PHOS / AliPHOSTrackSegmentMakerv1.cxx
CommitLineData
d15a28e7 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 **************************************************************************/
b2a60966 15/* $Id$ */
d15a28e7 16//_________________________________________________________________________
b2a60966 17// Implementation version 1 of algorithm class to construct PHOS track segments
18// Associates EMC and PPSD clusters
19// Unfolds the EMC cluster
20//
21//*-- Author: Dmitri Peressounko (RRC Ki & SUBATECH)
22//
d15a28e7 23
24// --- ROOT system ---
25
26#include "TObjArray.h"
27#include "TClonesArray.h"
9f616d61 28#include "TObjectTable.h"
d15a28e7 29
30// --- Standard library ---
31
de9ec31b 32#include <iostream.h>
d15a28e7 33
34// --- AliRoot header files ---
35
36#include "AliPHOSTrackSegmentMakerv1.h"
98cbd830 37#include "AliPHOSIndexToObject.h"
d15a28e7 38#include "AliPHOSTrackSegment.h"
2aca7d46 39#include "AliPHOSCpvRecPoint.h"
d15a28e7 40#include "AliPHOSLink.h"
41#include "AliPHOSv0.h"
42#include "AliRun.h"
43
92862013 44extern void UnfoldingChiSquare(Int_t &nPar, Double_t *Grad, Double_t & fret, Double_t *x, Int_t iflag) ;
9f616d61 45
d15a28e7 46ClassImp( AliPHOSTrackSegmentMakerv1)
47
48
49//____________________________________________________________________________
b2a60966 50 AliPHOSTrackSegmentMakerv1::AliPHOSTrackSegmentMakerv1() : AliPHOSTrackSegmentMaker()
d15a28e7 51{
9f616d61 52 // ctor
b2a60966 53
28c3a259 54 fR0 = 10. ;
d15a28e7 55 //clusters are sorted in "rows" and "columns" of width geom->GetCrystalSize(0),
98cbd830 56 fDelta = fR0 + fGeom->GetCrystalSize(0) ;
69183710 57 if(!gMinuit) gMinuit = new TMinuit(100) ;
6727be7e 58 fUnfoldFlag = kTRUE ;
d15a28e7 59}
60
9f616d61 61//____________________________________________________________________________
62 AliPHOSTrackSegmentMakerv1::~AliPHOSTrackSegmentMakerv1()
63{
64 // dtor
9f616d61 65}
b2a60966 66
d15a28e7 67//____________________________________________________________________________
68Bool_t AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * maxAt, Float_t * maxAtEnergy,
92862013 69 Int_t nPar, Float_t * fitparameters)
9f616d61 70{
b2a60966 71 // Calls TMinuit to fit the energy distribution of a cluster with several maxima
6ad0bfa0 72
2aca7d46 73 gMinuit->mncler(); // Reset Minuit list of paramters
9f616d61 74 gMinuit->SetPrintLevel(-1) ; // No Printout
75 gMinuit->SetFCN(UnfoldingChiSquare) ; // To set the address of the minimization function
76 gMinuit->SetObjectFit(emcRP) ; // To tranfer pointer to UnfoldingChiSquare
77
78 // filling initial values for fit parameters
d15a28e7 79 AliPHOSDigit * digit ;
9f616d61 80
81 Int_t ierflg = 0;
82 Int_t index = 0 ;
92862013 83 Int_t nDigits = (Int_t) nPar / 3 ;
9f616d61 84
d15a28e7 85 Int_t iDigit ;
9f616d61 86
87
92862013 88 for(iDigit = 0; iDigit < nDigits; iDigit++){
d15a28e7 89 digit = (AliPHOSDigit *) maxAt[iDigit];
90
92862013 91 Int_t relid[4] ;
d15a28e7 92 Float_t x ;
93 Float_t z ;
98cbd830 94 fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
95 fGeom->RelPosInModule(relid, x, z) ;
d15a28e7 96
92862013 97 Float_t energy = maxAtEnergy[iDigit] ;
d15a28e7 98
9f616d61 99 gMinuit->mnparm(index, "x", x, 0.1, 0, 0, ierflg) ;
100 index++ ;
d15a28e7 101 if(ierflg != 0){
102 cout << "PHOS Unfolding> Unable to set initial value for fit procedure : x = " << x << endl ;
103 return kFALSE;
104 }
9f616d61 105 gMinuit->mnparm(index, "z", z, 0.1, 0, 0, ierflg) ;
106 index++ ;
d15a28e7 107 if(ierflg != 0){
108 cout << "PHOS Unfolding> Unable to set initial value for fit procedure : z = " << z << endl ;
109 return kFALSE;
110 }
92862013 111 gMinuit->mnparm(index, "Energy", energy , 0.05*energy, 0., 4.*energy, ierflg) ;
9f616d61 112 index++ ;
d15a28e7 113 if(ierflg != 0){
92862013 114 cout << "PHOS Unfolding> Unable to set initial value for fit procedure : energy = " << energy << endl ;
d15a28e7 115 return kFALSE;
6ad0bfa0 116 }
d15a28e7 117 }
118
9f616d61 119 Double_t p0 = 0.1 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
120 // depends on it.
121 Double_t p1 = 1.0 ;
122 Double_t p2 = 0.0 ;
123
69183710 124 gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ; // force TMinuit to reduce function calls
9f616d61 125 gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ; // force TMinuit to use my gradient
d15a28e7 126 gMinuit->SetMaxIterations(5);
9f616d61 127 gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ; // No Warnings
98cbd830 128
9f616d61 129 gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ; // minimize
98cbd830 130
9f616d61 131 if(ierflg == 4){ // Minimum not found
132 cout << "PHOS Unfolding> Fit not converged, cluster abandoned "<< endl ;
d15a28e7 133 return kFALSE ;
134 }
92862013 135 for(index = 0; index < nPar; index++){
d15a28e7 136 Double_t err ;
137 Double_t val ;
9f616d61 138 gMinuit->GetParameter(index, val, err) ; // Returns value and error of parameter index
92862013 139 fitparameters[index] = val ;
d15a28e7 140 }
98cbd830 141
d15a28e7 142 return kTRUE;
9f616d61 143
d15a28e7 144}
9f616d61 145
d15a28e7 146//____________________________________________________________________________
98cbd830 147void AliPHOSTrackSegmentMakerv1::FillOneModule(AliPHOSRecPoint::RecPointsList * emcIn,
28c3a259 148 TArrayI * emcOut,
88714635 149 AliPHOSRecPoint::RecPointsList * ppsdIn,
28c3a259 150 TArrayI * ppsdOutUp,
151 TArrayI * ppsdOutLow,
88714635 152 Int_t & phosmod,
153 Int_t & emcStopedAt,
154 Int_t & ppsdStopedAt)
9f616d61 155{
69183710 156 // Fill xxxOut arrays with clusters from one PHOS module
9f616d61 157
158 AliPHOSEmcRecPoint * emcRecPoint ;
d15a28e7 159 AliPHOSPpsdRecPoint * ppsdRecPoint ;
160 Int_t index ;
6ad0bfa0 161
92862013 162 Int_t nEmcUnfolded = emcIn->GetEntries() ;
28c3a259 163 emcOut->Set(nEmcUnfolded);
164 Int_t inEmcOut = 0 ;
92862013 165 for(index = emcStopedAt; index < nEmcUnfolded; index++){
6ad0bfa0 166
98cbd830 167 emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
9f616d61 168
98cbd830 169 if(emcRecPoint->GetPHOSMod() != phosmod )
170 break ;
31aa6d6c 171
28c3a259 172 emcOut->AddAt(emcRecPoint->GetIndexInList(),inEmcOut) ;
173 inEmcOut++ ;
d15a28e7 174 }
28c3a259 175 emcOut->Set(inEmcOut) ;
176
d15a28e7 177 emcStopedAt = index ;
178
28c3a259 179 ppsdOutLow->Set(ppsdIn->GetEntries()) ;
180 ppsdOutUp->Set(ppsdIn->GetEntries()) ;
181 Int_t inPpsdLow = 0;
182 Int_t inPpsdUp = 0;
d15a28e7 183 for(index = ppsdStopedAt; index < ppsdIn->GetEntries(); index++){
83974468 184 ppsdRecPoint = (AliPHOSPpsdRecPoint *) ppsdIn->At(index) ;
92862013 185 if(ppsdRecPoint->GetPHOSMod() != phosmod )
9f616d61 186 break ;
fad3e5b9 187 if(phosmod <= fGeom->GetNCPVModules()) //in CPV
28c3a259 188 ppsdOutUp->AddAt(index,inPpsdUp++) ;
fad3e5b9 189 else{ //in PPSD
190 if(ppsdRecPoint->GetUp() )
191 ppsdOutUp->AddAt(index,inPpsdUp++) ;
192 else
193 ppsdOutLow->AddAt(index,inPpsdLow++) ;
194 }
d15a28e7 195 }
28c3a259 196 ppsdOutLow->Set(inPpsdLow);
197 ppsdOutUp->Set(inPpsdUp);
d15a28e7 198 ppsdStopedAt = index ;
6ad0bfa0 199
d15a28e7 200}
201//____________________________________________________________________________
92862013 202Float_t AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * emcclu,AliPHOSPpsdRecPoint * PpsdClu, Bool_t &toofar)
d15a28e7 203{
b2a60966 204 // Calculates the distance between the EMC RecPoint and the PPSD RecPoint
205
92862013 206 Float_t r = fR0 ;
d15a28e7 207
208 TVector3 vecEmc ;
209 TVector3 vecPpsd ;
210
92862013 211 emcclu->GetLocalPosition(vecEmc) ;
d15a28e7 212 PpsdClu->GetLocalPosition(vecPpsd) ;
92862013 213 if(emcclu->GetPHOSMod() == PpsdClu->GetPHOSMod()){
7956ec10 214 // if(vecPpsd.X() >= vecEmc.X() - fDelta ){
215 // if(vecPpsd.Z() >= vecEmc.Z() - fDelta ){
6ad0bfa0 216 // Correct to difference in CPV and EMC position due to different distance to center.
217 // we assume, that particle moves from center
98cbd830 218 Float_t dCPV = fGeom->GetIPtoOuterCoverDistance();
219 Float_t dEMC = fGeom->GetIPtoCrystalSurface() ;
92862013 220 dEMC = dEMC / dCPV ;
221 vecPpsd = dEMC * vecPpsd - vecEmc ;
222 r = vecPpsd.Mag() ;
7956ec10 223 // } // if zPpsd >= zEmc - fDelta
92862013 224 toofar = kFALSE ;
7956ec10 225 //} // if xPpsd >= xEmc - fDelta
226 // else
227 //toofar = kTRUE ;
d15a28e7 228 }
229 else
92862013 230 toofar = kTRUE ;
7956ec10 231
232 //toofar = kFALSE ;
233
d15a28e7 234
92862013 235 return r ;
d15a28e7 236}
237
238//____________________________________________________________________________
28c3a259 239void AliPHOSTrackSegmentMakerv1::MakeLinks(TArrayI * emcRecPoints, TArrayI * ppsdRecPointsUp,
240 TArrayI * ppsdRecPointsLow, TClonesArray * linklowArray,
92862013 241 TClonesArray *linkupArray)
d15a28e7 242{
b2a60966 243 // Finds distances (links) between all EMC and PPSD clusters, which are not further apart from each other than fR0
244
d15a28e7 245
92862013 246 AliPHOSPpsdRecPoint * ppsdlow ;
247 AliPHOSPpsdRecPoint * ppsdup ;
248 AliPHOSEmcRecPoint * emcclu ;
28c3a259 249
d15a28e7 250 Int_t iLinkLow = 0 ;
251 Int_t iLinkUp = 0 ;
252
28c3a259 253 Int_t iEmcRP;
254
255 for(iEmcRP = 0; iEmcRP < emcRecPoints->GetSize(); iEmcRP++ ) {
256 emcclu = (AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emcRecPoints->At(iEmcRP),"emc") ;
92862013 257 Bool_t toofar ;
d15a28e7 258
28c3a259 259 Int_t iPpsdLow ;
260
261 for(iPpsdLow = 0; iPpsdLow < ppsdRecPointsLow->GetSize();iPpsdLow++ ) {
262
263 ppsdlow = (AliPHOSPpsdRecPoint *) fPlease->GimeRecPoint(ppsdRecPointsLow->At(iPpsdLow),"ppsd") ;
92862013 264 Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdlow, toofar) ;
d15a28e7 265
92862013 266 if(toofar)
d15a28e7 267 break ;
83974468 268 if(r < fR0){
28c3a259 269 new( (*linklowArray)[iLinkLow++]) AliPHOSLink(r, iEmcRP, iPpsdLow) ;
270 }
d15a28e7 271 }
272
28c3a259 273 Int_t iPpsdUp = 0 ;
274 for(iPpsdUp = 0; iPpsdUp < ppsdRecPointsUp->GetSize();iPpsdUp++ ) {
275
276 ppsdup = (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdRecPointsUp->At(iPpsdUp),"ppsd") ;
92862013 277 Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdup, toofar) ;
d15a28e7 278
92862013 279 if(toofar)
d15a28e7 280 break ;
83974468 281 if(r < fR0) {
28c3a259 282 new( (*linkupArray)[iLinkUp++]) AliPHOSLink(r, iEmcRP, iPpsdUp) ;
283 }
d15a28e7 284 }
28c3a259 285 }
d15a28e7 286
92862013 287 linklowArray->Sort() ; //first links with smallest distances
288 linkupArray->Sort() ;
d15a28e7 289}
28c3a259 290
d15a28e7 291//____________________________________________________________________________
28c3a259 292void AliPHOSTrackSegmentMakerv1::MakePairs(TArrayI * emcRecPoints,
293 TArrayI * ppsdRecPointsUp,
294 TArrayI * ppsdRecPointsLow,
88714635 295 TClonesArray * linklowArray,
296 TClonesArray * linkupArray,
297 AliPHOSTrackSegment::TrackSegmentsList * trsl)
6ad0bfa0 298{
299
300 // Finds the smallest links and makes pairs of PPSD and EMC clusters with smallest distance
301
92862013 302 TIter nextLow(linklowArray) ;
303 TIter nextUp(linkupArray) ;
d15a28e7 304
305 AliPHOSLink * linkLow ;
306 AliPHOSLink * linkUp ;
307
28c3a259 308 Int_t emc ;
309 Int_t ppsdLow ;
310 Int_t ppsdUp ;
d15a28e7 311
28c3a259 312 AliPHOSPpsdRecPoint * nullpointer = 0 ;
313 ppsdUp = 0 ;
9f616d61 314
d15a28e7 315 while ( (linkLow = (AliPHOSLink *)nextLow() ) ){
28c3a259 316
317 emc = emcRecPoints->At(linkLow->GetEmc()) ;
318 ppsdLow = ppsdRecPointsLow->At(linkLow->GetPpsd()) ;
28c3a259 319
7956ec10 320 if( (emc >= 0) && (ppsdLow >= 0) ){ // RecPoints not removed yet
321
28c3a259 322 new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *)fPlease->GimeRecPoint(emc,"emc"),
323 nullpointer,
324 (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdLow,"ppsd") ) ;
325 ((AliPHOSTrackSegment* )trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
326 //replace index of emc to negative and shifted index of TS
327 emcRecPoints->AddAt(-2 - fNTrackSegments,linkLow->GetEmc()) ;
7956ec10 328 //replace index of PPSD Low to negative and shifted index of TS
28c3a259 329 ppsdRecPointsLow->AddAt(-2 - fNTrackSegments,linkLow->GetPpsd()) ;
330 fNTrackSegments++ ;
331
6ad0bfa0 332 }
d15a28e7 333 }
28c3a259 334
335 while ( (linkUp = (AliPHOSLink *)nextUp() ) ){
336 emc = emcRecPoints->At(linkUp->GetEmc()) ;
337 if(emc != -1){ //without ppsd Up yet
d15a28e7 338
28c3a259 339 ppsdUp = ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
7956ec10 340 if(ppsdUp >= 0){ //ppsdUp still exist
341
342 if(emc >= 0){ //without ppsd Low => create new TS
343
344 fNTrackSegments = trsl->GetEntries() ;
345 new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"),
346 (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"),
347 nullpointer) ;
348 ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
349 fNTrackSegments++ ;
350 }
351 else{ // append ppsd Up to existing TS
352 ((AliPHOSTrackSegment *)trsl->At(-2-emc))->SetPpsdUpRecPoint((AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"));
353 }
354
355 emcRecPoints->AddAt(-1,linkUp->GetEmc()) ; //Mark that PPSD Up found
356 //replace index of PPSD Up to negative and shifted index of TS
357 ppsdRecPointsUp->AddAt(-2 - fNTrackSegments,linkUp->GetPpsd()) ;
358 } //if ppsdUp still exist
28c3a259 359 }
360 }
361
362 Int_t iEmcRP ;
363 for(iEmcRP = 0; iEmcRP <emcRecPoints->GetSize() ; iEmcRP++ ){
364 emc = emcRecPoints->At(iEmcRP) ;
365 if(emc >=0 ){
366 ppsdUp = 0;
367 ppsdLow = 0;
368 new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"),
369 nullpointer, nullpointer ) ;
370 ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
371 fNTrackSegments++;
d15a28e7 372 }
b2a60966 373
d15a28e7 374 }
28c3a259 375
d15a28e7 376}
377
378//____________________________________________________________________________
88714635 379void AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * dl,
380 AliPHOSRecPoint::RecPointsList * emcl,
381 AliPHOSRecPoint::RecPointsList * ppsdl,
382 AliPHOSTrackSegment::TrackSegmentsList * trsl)
d15a28e7 383{
b2a60966 384 // Makes the track segments out of the list of EMC and PPSD Recpoints and stores them in a list
98cbd830 385
9f616d61 386 Int_t emcStopedAt = 0 ;
d15a28e7 387 Int_t ppsdStopedAt = 0 ;
28c3a259 388
389 fNTrackSegments = 0 ;
d15a28e7 390
28c3a259 391 TArrayI * emcRecPoints = new TArrayI(1000) ; // these arrays keep indexes
392 TArrayI * ppsdRecPointsUp = new TArrayI(1000) ; // of RecPoints, which are
b73f246d 393 TArrayI * ppsdRecPointsLow = new TArrayI(1000) ; // kept in TClonesArray's emcl, ppsdl, cpv
d15a28e7 394
7956ec10 395 TClonesArray * linklowArray = new TClonesArray("AliPHOSLink", 1000);
396 TClonesArray * linkupArray = new TClonesArray("AliPHOSLink", 1000);
6ad0bfa0 397
98cbd830 398 if(fUnfoldFlag){
69183710 399 UnfoldAll(dl, emcl) ; // Unfolds all EMC clusters
fad3e5b9 400 UnfoldAll(dl, ppsdl) ; // Unfolds all CPV clusters
98cbd830 401 }
d15a28e7 402
fad3e5b9 403 Int_t phosmod = 1 ;
98cbd830 404 while(phosmod <= fGeom->GetNModules() ){
405
406 FillOneModule(emcl, emcRecPoints, ppsdl, ppsdRecPointsUp, ppsdRecPointsLow, phosmod, emcStopedAt, ppsdStopedAt) ;
407
408 MakeLinks(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray) ;
409
92862013 410 MakePairs(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray, trsl) ;
98cbd830 411
28c3a259 412 emcRecPoints->Reset() ;
98cbd830 413
28c3a259 414 ppsdRecPointsUp->Reset() ;
98cbd830 415
28c3a259 416 ppsdRecPointsLow->Reset() ;
98cbd830 417
92862013 418 linkupArray->Clear() ;
98cbd830 419
92862013 420 linklowArray->Clear() ;
98cbd830 421
92862013 422 phosmod++ ;
d15a28e7 423 }
28c3a259 424
92862013 425 delete emcRecPoints ;
426 emcRecPoints = 0 ;
98cbd830 427
92862013 428 delete ppsdRecPointsUp ;
429 ppsdRecPointsUp = 0 ;
9f616d61 430
92862013 431 delete ppsdRecPointsLow ;
432 ppsdRecPointsLow = 0 ;
9f616d61 433
92862013 434 delete linkupArray ;
435 linkupArray = 0 ;
9f616d61 436
92862013 437 delete linklowArray ;
438 linklowArray = 0 ;
d15a28e7 439}
440
441//____________________________________________________________________________
9f616d61 442Double_t AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
443{
b2a60966 444 // Shape of the shower (see PHOS TDR)
445 // If you change this function, change also the gradien evaluation in ChiSquare()
446
6ad0bfa0 447 Double_t r4 = r*r*r*r ;
448 Double_t r295 = TMath::Power(r, 2.95) ;
d15a28e7 449 Double_t shape = TMath::Exp( -r4 * (1. / (2.32 + 0.26 * r4) + 0.0316 / (1 + 0.0652 * r295) ) ) ;
450 return shape ;
451}
452
98cbd830 453//____________________________________________________________________________
454void AliPHOSTrackSegmentMakerv1::UnfoldAll(DigitsList * dl, AliPHOSRecPoint::RecPointsList * emcIn)
455{
fad3e5b9 456 // Performs unfolding of all EMC/CPV but NOT ppsd clusters, sorts them and resets indexes in RecPoints
98cbd830 457
458 AliPHOSEmcRecPoint * emcRecPoint ;
459 Int_t index ;
460 Int_t nEmcUnfolded = emcIn->GetEntries() ;
98cbd830 461
fad3e5b9 462 Int_t nModulesToUnfold ;
98cbd830 463
fad3e5b9 464 if(emcIn->GetEntries() > 0){
98cbd830 465
2aca7d46 466 if(((AliPHOSRecPoint *)emcIn->At(0))->IsEmc()){
fad3e5b9 467 nModulesToUnfold = fGeom->GetNModules() ;
2aca7d46 468 }
469 else{
fad3e5b9 470 nModulesToUnfold = fGeom->GetNCPVModules() ;
2aca7d46 471 }
fad3e5b9 472
473 for(index = 0 ; index < nEmcUnfolded; index++){
474
475 emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
476 if(emcRecPoint->GetPHOSMod()> nModulesToUnfold)
477 break ;
478
479 Int_t nMultipl = emcRecPoint->GetMultiplicity() ;
480 Int_t * maxAt = new Int_t[nMultipl] ;
481 Float_t * maxAtEnergy = new Float_t[nMultipl] ;
482 Int_t nMax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
483
484 if( nMax > 1 ) { // if cluster is very flat (no pronounced maximum) then nMax = 0
485 UnfoldClusters(dl, emcIn, emcRecPoint, nMax, maxAt, maxAtEnergy) ;
486 emcIn->Remove(emcRecPoint);
487 emcIn->Compress() ;
488 index-- ;
489 nEmcUnfolded-- ;
490 }
491
492 delete[] maxAt ;
493 delete[] maxAtEnergy ;
494 } //Unfolding finished
2aca7d46 495
fad3e5b9 496 emcIn->Sort() ;
69183710 497
fad3e5b9 498 // to set index to new and correct index of old RecPoints
2aca7d46 499 for( index = 0 ; index < emcIn->GetEntries() ; index++)
fad3e5b9 500 ((AliPHOSEmcRecPoint *) emcIn->At(index))->SetIndexInList(index) ;
fad3e5b9 501
98cbd830 502 }
503
504}
d15a28e7 505//____________________________________________________________________________
88714635 506void AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl,
507 AliPHOSRecPoint::RecPointsList * emcIn,
508 AliPHOSEmcRecPoint * iniEmc,
509 Int_t nMax,
510 int * maxAt,
98cbd830 511 Float_t * maxAtEnergy)
d15a28e7 512{
b2a60966 513 // Performs the unfolding of a cluster with nMax overlapping showers
514 // This is time consuming (use the (Un)SetUnfolFlag() )
515
92862013 516 Int_t nPar = 3 * nMax ;
31aa6d6c 517 Float_t * fitparameters = new Float_t[nPar] ;
98cbd830 518
9f616d61 519
92862013 520 Bool_t rv = FindFit(iniEmc, maxAt, maxAtEnergy, nPar, fitparameters) ;
31aa6d6c 521 if( !rv ) {
522 // Fit failed, return and remove cluster
523 delete[] fitparameters ;
d15a28e7 524 return ;
31aa6d6c 525 }
98cbd830 526
d15a28e7 527 Float_t xDigit ;
528 Float_t zDigit ;
92862013 529 Int_t relid[4] ;
d15a28e7 530
92862013 531 Int_t nDigits = iniEmc->GetMultiplicity() ;
d15a28e7 532 Float_t xpar ;
533 Float_t zpar ;
92862013 534 Float_t epar ;
535 Float_t distance ;
536 Float_t ratio ;
31aa6d6c 537 Float_t * efit = new Float_t[nDigits] ;
d15a28e7 538 Int_t iparam ;
539 Int_t iDigit ;
540
541 AliPHOSDigit * digit ;
6ad0bfa0 542 Int_t * emcDigits = iniEmc->GetDigitsList() ;
d15a28e7 543 Float_t * emcEnergies = iniEmc->GetEnergiesList() ;
544
545 Int_t iRecPoint = emcIn->GetEntries() ;
546
92862013 547 for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
98cbd830 548 digit = fPlease->GimeDigit( emcDigits[iDigit] ) ;
549 fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
550 fGeom->RelPosInModule(relid, xDigit, zDigit) ;
92862013 551 efit[iDigit] = 0;
d15a28e7 552 iparam = 0 ;
553
92862013 554 while(iparam < nPar ){
555 xpar = fitparameters[iparam] ;
556 zpar = fitparameters[iparam+1] ;
557 epar = fitparameters[iparam+2] ;
d15a28e7 558 iparam += 3 ;
92862013 559 distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) ;
560 distance = TMath::Sqrt(distance) ;
b2a60966 561 efit[iDigit] += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
d15a28e7 562 }
d15a28e7 563 }
564
565 iparam = 0 ;
566 Float_t eDigit ;
567
98cbd830 568
92862013 569 while(iparam < nPar ){
570 xpar = fitparameters[iparam] ;
571 zpar = fitparameters[iparam+1] ;
572 epar = fitparameters[iparam+2] ;
d15a28e7 573 iparam += 3 ;
98cbd830 574
28c3a259 575 if(iRecPoint >= emcIn->GetSize())
576 emcIn->Expand(2*iRecPoint) ;
d15a28e7 577
2aca7d46 578 if(iniEmc->IsEmc()){
579 (*emcIn)[iRecPoint] = new AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
580
581 AliPHOSEmcRecPoint * emcRP ;
582 emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint);
583 iRecPoint++ ;
584
585 for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
586 digit = fPlease->GimeDigit( emcDigits[iDigit] ) ;
587 fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
588 fGeom->RelPosInModule(relid, xDigit, zDigit) ;
589 distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) ;
590 distance = TMath::Sqrt(distance) ;
591 ratio = epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) / efit[iDigit] ;
592 eDigit = emcEnergies[iDigit] * ratio ;
593 emcRP->AddDigit( *digit, eDigit ) ;
594 }
ad8cfaf4 595 emcRP->EvalAll() ;
d15a28e7 596 }
2aca7d46 597 else{
598 (*emcIn)[iRecPoint] = new AliPHOSCpvRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
599
600 AliPHOSCpvRecPoint * cpvRP ;
601 cpvRP = (AliPHOSCpvRecPoint *) emcIn->At(iRecPoint);
d15a28e7 602
2aca7d46 603 iRecPoint++ ;
604
605 for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
606 digit = fPlease->GimeDigit( emcDigits[iDigit] ) ;
607 fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
608 fGeom->RelPosInModule(relid, xDigit, zDigit) ;
609 distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) ;
610 distance = TMath::Sqrt(distance) ;
611 ratio = epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) / efit[iDigit] ;
612 eDigit = emcEnergies[iDigit] * ratio ;
613 cpvRP->AddDigit( *digit, eDigit ) ;
614 }
ad8cfaf4 615 cpvRP->EvalAll() ;
2aca7d46 616 }
617
d15a28e7 618 }
31aa6d6c 619
620 delete[] fitparameters ;
621 delete[] efit ;
622
d15a28e7 623}
6ad0bfa0 624
d15a28e7 625//______________________________________________________________________________
92862013 626void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
d15a28e7 627{
b2a60966 628 // Calculates th Chi square for the cluster unfolding minimization
629 // Number of parameters, Gradient, Chi squared, parameters, what to do
98cbd830 630
9f616d61 631 AliPHOSEmcRecPoint * emcRP = (AliPHOSEmcRecPoint *) gMinuit->GetObjectFit() ; // EmcRecPoint to fit
98cbd830 632
6ad0bfa0 633 Int_t * emcDigits = emcRP->GetDigitsList() ;
98cbd830 634
635 Int_t nOfDigits = emcRP->GetDigitsMultiplicity() ;
636
d15a28e7 637 Float_t * emcEnergies = emcRP->GetEnergiesList() ;
98cbd830 638
639 AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
640
641 AliPHOSIndexToObject * please = AliPHOSIndexToObject::GetInstance() ;
642
d15a28e7 643 fret = 0. ;
644 Int_t iparam ;
645
9f616d61 646 if(iflag == 2)
92862013 647 for(iparam = 0 ; iparam < nPar ; iparam++)
9f616d61 648 Grad[iparam] = 0 ; // Will evaluate gradient
649
98cbd830 650 Double_t efit ;
651
d15a28e7 652 AliPHOSDigit * digit ;
98cbd830 653 Int_t iDigit ;
654
655 for( iDigit = 0 ; iDigit < nOfDigits ; iDigit++) {
656
657 digit = please->GimeDigit( emcDigits[iDigit] ) ;
9f616d61 658
92862013 659 Int_t relid[4] ;
d15a28e7 660 Float_t xDigit ;
661 Float_t zDigit ;
98cbd830 662
92862013 663 geom->AbsToRelNumbering(digit->GetId(), relid) ;
98cbd830 664
92862013 665 geom->RelPosInModule(relid, xDigit, zDigit) ;
98cbd830 666
9f616d61 667 if(iflag == 2){ // calculate gradient
668 Int_t iParam = 0 ;
92862013 669 efit = 0 ;
670 while(iParam < nPar ){
671 Double_t distance = (xDigit - x[iParam]) * (xDigit - x[iParam]) ;
9f616d61 672 iParam++ ;
92862013 673 distance += (zDigit - x[iParam]) * (zDigit - x[iParam]) ;
674 distance = TMath::Sqrt( distance ) ;
9f616d61 675 iParam++ ;
92862013 676 efit += x[iParam] * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
9f616d61 677 iParam++ ;
678 }
92862013 679 Double_t sum = 2. * (efit - emcEnergies[iDigit]) / emcEnergies[iDigit] ; // Here we assume, that sigma = sqrt(E)
9f616d61 680 iParam = 0 ;
92862013 681 while(iParam < nPar ){
9f616d61 682 Double_t xpar = x[iParam] ;
683 Double_t zpar = x[iParam+1] ;
92862013 684 Double_t epar = x[iParam+2] ;
9f616d61 685 Double_t dr = TMath::Sqrt( (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) );
686 Double_t shape = sum * AliPHOSTrackSegmentMakerv1::ShowerShape(dr) ;
687 Double_t r4 = dr*dr*dr*dr ;
688 Double_t r295 = TMath::Power(dr,2.95) ;
689 Double_t deriv =-4. * dr*dr * ( 2.32 / ( (2.32 + 0.26 * r4) * (2.32 + 0.26 * r4) ) +
690 0.0316 * (1. + 0.0171 * r295) / ( ( 1. + 0.0652 * r295) * (1. + 0.0652 * r295) ) ) ;
691
92862013 692 Grad[iParam] += epar * shape * deriv * (xpar - xDigit) ; // Derivative over x
9f616d61 693 iParam++ ;
92862013 694 Grad[iParam] += epar * shape * deriv * (zpar - zDigit) ; // Derivative over z
9f616d61 695 iParam++ ;
696 Grad[iParam] += shape ; // Derivative over energy
697 iParam++ ;
698 }
699 }
92862013 700 efit = 0;
9f616d61 701 iparam = 0 ;
98cbd830 702
92862013 703 while(iparam < nPar ){
9f616d61 704 Double_t xpar = x[iparam] ;
705 Double_t zpar = x[iparam+1] ;
92862013 706 Double_t epar = x[iparam+2] ;
9f616d61 707 iparam += 3 ;
92862013 708 Double_t distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) ;
709 distance = TMath::Sqrt(distance) ;
710 efit += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
9f616d61 711 }
98cbd830 712
92862013 713 fret += (efit-emcEnergies[iDigit])*(efit-emcEnergies[iDigit])/emcEnergies[iDigit] ;
9f616d61 714 // Here we assume, that sigma = sqrt(E)
d15a28e7 715 }
98cbd830 716
d15a28e7 717}