Fixed memory leak
[u/mrichter/AliRoot.git] / MUON / AliMUONSimpleClusterServer.cxx
CommitLineData
d08b5461 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 "AliMUONSimpleClusterServer.h"
19
9bf6860b 20#include "AliCodeTimer.h"
21#include "AliLog.h"
d08b5461 22#include "AliMUONCluster.h"
9bf6860b 23#include "AliMUONConstants.h"
d08b5461 24#include "AliMUONGeometryTransformer.h"
25#include "AliMUONPad.h"
9bf6860b 26#include "AliMUONTriggerTrackToTrackerClusters.h"
d08b5461 27#include "AliMUONVCluster.h"
28#include "AliMUONVClusterFinder.h"
29#include "AliMUONVClusterStore.h"
30#include "AliMUONVDigitStore.h"
35be7ed7 31#include "AliMUONRecoParam.h"
d08b5461 32#include "AliMpArea.h"
33#include "AliMpDEIterator.h"
34#include "AliMpDEManager.h"
35#include "AliMpExMap.h"
e5b8601d 36#include "AliMpExMapIterator.h"
9bf6860b 37#include "AliMpPad.h"
d08b5461 38#include "AliMpSegmentation.h"
39#include "AliMpVSegmentation.h"
d08b5461 40#include <Riostream.h>
9e41a340 41#include <TObjArray.h>
d08b5461 42#include <TString.h>
9bf6860b 43#include <float.h>
7deb8eb0 44
d08b5461 45/// \class AliMUONSimpleClusterServer
46///
47/// Implementation of AliMUONVClusterServer interface
48///
49///
50/// \author Laurent Aphecetche, Subatech
51
cdd730d0 52using std::endl;
53using std::cout;
d08b5461 54/// \cond CLASSIMP
55ClassImp(AliMUONSimpleClusterServer)
56/// \endcond
57
58namespace
59{
60 TString AsString(const AliMpArea& area)
61 {
62 return Form("(X,Y)=(%7.3f,%7.3f) (DX,DY)=(%7.3f,%7.3f)",
6e97fbb8 63 area.GetPositionX(),
64 area.GetPositionY(),
65 area.GetDimensionX(), /// TBCL was Y !!!
66 area.GetDimensionY());
d08b5461 67 }
68}
69
70//_____________________________________________________________________________
9bf6860b 71AliMUONSimpleClusterServer::AliMUONSimpleClusterServer(AliMUONVClusterFinder* clusterFinder,
d08b5461 72 const AliMUONGeometryTransformer& transformer)
2e2d0c44 73: AliMUONVClusterServer(),
74 fDigitStore(0x0),
d08b5461 75 fClusterFinder(clusterFinder),
72dae9ff 76 fkTransformer(transformer),
d08b5461 77 fPads(),
9bf6860b 78 fTriggerTrackStore(0x0),
79 fBypass(0x0)
d08b5461 80{
81 /// Ctor
9bf6860b 82 /// Note that we take ownership of the clusterFinder
d08b5461 83
630711ed 84 fPads[0] = new AliMpExMap;
85 fPads[1] = new AliMpExMap;
7deb8eb0 86
e5b8601d 87 fPadsIterator[0] = fPads[0]->CreateIterator();
88 fPadsIterator[1] = fPads[1]->CreateIterator();
d08b5461 89}
90
91//_____________________________________________________________________________
92AliMUONSimpleClusterServer::~AliMUONSimpleClusterServer()
93{
94 /// Dtor
9bf6860b 95 delete fClusterFinder;
d08b5461 96 delete fPads[0];
97 delete fPads[1];
e5b8601d 98 delete fPadsIterator[0];
99 delete fPadsIterator[1];
9bf6860b 100 delete fBypass;
d08b5461 101}
102
103//_____________________________________________________________________________
104Int_t
105AliMUONSimpleClusterServer::Clusterize(Int_t chamberId,
106 AliMUONVClusterStore& clusterStore,
35be7ed7 107 const AliMpArea& area,
108 const AliMUONRecoParam* recoParam)
d08b5461 109{
110 /// Area is in absolute coordinate. If not valid, means clusterize all
111 /// the chamber.
112 ///
113 /// We first find out the list of DE that have a non-zero overlap with area,
114 /// and then use the clusterfinder to find clusters in those areas (and DE).
115
99c136e1 116 AliCodeTimerAuto(Form("Chamber %d",chamberId),0);
9bf6860b 117
118 if ( fTriggerTrackStore && chamberId >= 6 )
119 {
120 return fBypass->GenerateClusters(chamberId,clusterStore);
121 }
122
35be7ed7 123 if (!recoParam) {
124 AliError("Reconstruction parameters are missing: unable to clusterize");
125 return 0;
126 }
127
d08b5461 128 AliMpDEIterator it;
129
130 it.First(chamberId);
131
132 Int_t nofAddedClusters(0);
133 Int_t fNCluster = clusterStore.GetSize();
134
135 AliDebug(1,Form("chamberId = %2d NofClusters before = %d searchArea=%s",
136 chamberId,fNCluster,AsString(area).Data()));
137
138 while ( !it.IsDone() )
139 {
140 Int_t detElemId = it.CurrentDEId();
141
9e41a340 142 TObjArray* pads[2] =
d08b5461 143 {
9e41a340 144 static_cast<TObjArray*>(fPads[0]->GetValue(detElemId)),
145 static_cast<TObjArray*>(fPads[1]->GetValue(detElemId))
d08b5461 146 };
147
148 if ( ( pads[0] && pads[0]->GetLast()>=0 ) ||
149 ( pads[1] && pads[1]->GetLast()>=0 ) )
150 {
151 AliMpArea deArea; // area in DE-local-coordinates
152 Bool_t ok(kTRUE);
153
154 if ( area.IsValid() )
155 {
156 ok = Overlap(detElemId,area,deArea);
157 }
158
159 if ( ok )
160 {
2e2d0c44 161 const AliMpVSegmentation* seg[2] =
d08b5461 162 { AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath0),
163 AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath1)
164 };
110edb51 165
166 fClusterFinder->SetChargeHints(recoParam->LowestPadCharge(),
167 recoParam->LowestClusterCharge());
168
2e2d0c44 169 if ( fClusterFinder->NeedSegmentation() )
170 {
9bf6860b 171 fClusterFinder->Prepare(detElemId,pads,deArea,seg);
d08b5461 172 }
173 else
174 {
9bf6860b 175 fClusterFinder->Prepare(detElemId,pads,deArea);
d08b5461 176 }
177
178 AliDebug(1,Form("Clusterizing DE %04d with %3d pads (cath0) and %3d pads (cath1)",
179 detElemId,
180 (pads[0] ? pads[0]->GetLast()+1 : 0),
181 (pads[1] ? pads[1]->GetLast()+1 : 0)));
182
183 AliMUONCluster* cluster;
184
9bf6860b 185 while ( ( cluster = fClusterFinder->NextCluster() ) )
d08b5461 186 {
187 // add new cluster to the store with information to build its ID
188 // increment the number of clusters into the store
35be7ed7 189 AliMUONVCluster* rawCluster = clusterStore.Add(chamberId, detElemId, fNCluster++);
d08b5461 190
191 ++nofAddedClusters;
192
193 // fill array of Id of digits attached to this cluster
194 Int_t nPad = cluster->Multiplicity();
195 if (nPad < 1) AliWarning("no pad attached to the cluster");
196
197 for (Int_t iPad=0; iPad<nPad; iPad++)
198 {
199 AliMUONPad *pad = cluster->Pad(iPad);
89b4e052 200
201 // skip virtual pads
202 if (!pad->IsReal()) continue;
203
204 rawCluster->AddDigitId(pad->GetUniqueID());
d08b5461 205 }
206
207 // fill charge and other cluster informations
208 rawCluster->SetCharge(cluster->Charge());
89b4e052 209 rawCluster->SetChi2(cluster->Chi2());
d08b5461 210
211 Double_t xg, yg, zg;
72dae9ff 212 fkTransformer.Local2Global(detElemId,
d08b5461 213 cluster->Position().X(), cluster->Position().Y(),
214 0, xg, yg, zg);
215 rawCluster->SetXYZ(xg, yg, zg);
35be7ed7 216 rawCluster->SetErrXY(recoParam->GetDefaultNonBendingReso(chamberId),recoParam->GetDefaultBendingReso(chamberId));
d08b5461 217
91b67b6f 218 // Set MC label
219 if (fDigitStore && fDigitStore->HasMCInformation())
220 {
221 rawCluster->SetMCLabel(FindMCLabel(*cluster, detElemId, seg));
222 }
223
2e2d0c44 224 AliDebug(1,Form("Adding RawCluster detElemId %4d mult %2d charge %e (xl,yl,zl)=(%e,%e,%e) (xg,yg,zg)=(%e,%e,%e) label %d",
89b4e052 225 detElemId,rawCluster->GetNDigits(),rawCluster->GetCharge(),
d08b5461 226 cluster->Position().X(),cluster->Position().Y(),0.0,
2e2d0c44 227 xg,yg,zg,rawCluster->GetMCLabel()));
d08b5461 228 }
229 }
230 }
231 it.Next();
232 }
233
234 AliDebug(1,Form("chamberId = %2d NofClusters after = %d",chamberId,fNCluster));
91b67b6f 235
d08b5461 236 return nofAddedClusters;
237}
238
9bf6860b 239
d08b5461 240//_____________________________________________________________________________
241void
242AliMUONSimpleClusterServer::Global2Local(Int_t detElemId, const AliMpArea& globalArea,
243 AliMpArea& localArea) const
244{
245 /// Convert a global area in local area for a given DE
246
247 Double_t xl,yl,zl;
248
ad30b53f 249 Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
250 if ( chamberId < 0 ) {
251 AliErrorStream() << "Cannot get chamberId from detElemId=" << detElemId << endl;
252 return;
253 }
254 Double_t zg = AliMUONConstants::DefaultChamberZ(chamberId);
d08b5461 255
72dae9ff 256 fkTransformer.Global2Local(detElemId,
6e97fbb8 257 globalArea.GetPositionX(),globalArea.GetPositionY(),zg,
d08b5461 258 xl,yl,zl);
259
6e97fbb8 260 localArea = AliMpArea(xl,yl, globalArea.GetDimensionX(), globalArea.GetDimensionY());
d08b5461 261}
262
263//_____________________________________________________________________________
264Bool_t
265AliMUONSimpleClusterServer::Overlap(Int_t detElemId,
266 const AliMpArea& area,
267 AliMpArea& deArea) const
268{
269 /// Check whether (global) area overlaps with the given DE.
270 /// If it is, set deArea to the overlap region and convert it
271 /// in the local coordinate system of that DE.
272
273 Bool_t overlap(kFALSE);
274
72dae9ff 275 AliMpArea* globalDEArea = fkTransformer.GetDEArea(detElemId);
9bf6860b 276
277 if (!globalDEArea) return kFALSE;
d08b5461 278
279 AliMpArea overlapArea;
280
281 if ( area.Overlap(*globalDEArea) )
282 {
283 overlapArea = area.Intersect(*globalDEArea);
284 Global2Local(detElemId,overlapArea,deArea);
285 overlap = kTRUE;
286 }
287 else
288 {
289 deArea = AliMpArea();
290 }
291
292 AliDebug(1,Form("DE %04d area %s globalDEArea %s overlapArea %s deArea %s overlap=%d",
293 detElemId,
294 AsString(area).Data(),
295 AsString(*globalDEArea).Data(),
296 AsString(overlapArea).Data(),
297 AsString(deArea).Data(),
298 overlap));
299
300 return overlap;
301}
302
303//_____________________________________________________________________________
9e41a340 304TObjArray*
d08b5461 305AliMUONSimpleClusterServer::PadArray(Int_t detElemId, Int_t cathode) const
306{
307 /// Return array for given cathode of given DE
308
9e41a340 309 return static_cast<TObjArray*>(fPads[cathode]->GetValue(detElemId));
d08b5461 310}
311
312//_____________________________________________________________________________
9bf6860b 313Bool_t
314AliMUONSimpleClusterServer::UseTriggerTrackStore(AliMUONVTriggerTrackStore* trackStore)
315{
316 /// Tells us to use trigger track store, and thus to bypass St45 clusters
317 fTriggerTrackStore = trackStore; // not owner
318 delete fBypass;
72dae9ff 319 fBypass = new AliMUONTriggerTrackToTrackerClusters(fkTransformer,fTriggerTrackStore);
9bf6860b 320 return kTRUE;
321}
322
323//_____________________________________________________________________________
d08b5461 324void
2e2d0c44 325AliMUONSimpleClusterServer::UseDigits(TIter& next, AliMUONVDigitStore* digitStore)
d08b5461 326{
327 /// Convert digitStore into two arrays of AliMUONPads
2e2d0c44 328
329 fDigitStore = digitStore;
330
e5b8601d 331 // Clear pads arrays in the maps
332 for ( Int_t i=0; i<2; i++ ) {
333 fPadsIterator[i]->Reset();
334 Int_t key; TObject* obj;
335 while ( ( obj = fPadsIterator[i]->Next(key) ) ) {
336 //cout << "clearing array for detElemId " << key << " ";
337 obj->Clear();
338 }
339 }
340
d08b5461 341 AliMUONVDigit* d;
d08b5461 342 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
343 {
344 if ( ! d->Charge() > 0 ) continue; // skip void digits.
4750f29d 345 if ( ! d->IsTracker() ) continue; // skip trigger digits
d08b5461 346 Int_t ix = d->PadX();
347 Int_t iy = d->PadY();
348 Int_t cathode = d->Cathode();
349 Int_t detElemId = d->DetElemId();
350 const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->
351 GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
168e9c4d 352 AliMpPad pad = seg->PadByIndices(ix,iy);
d08b5461 353
9e41a340 354 TObjArray* padArray = PadArray(detElemId,cathode);
d08b5461 355 if (!padArray)
356 {
9e41a340 357 padArray = new TObjArray(100);
ed90feae 358 padArray->SetOwner(kTRUE);
d08b5461 359 fPads[cathode]->Add(detElemId,padArray);
360 }
361
9e41a340 362 AliMUONPad* mpad = new AliMUONPad(detElemId,cathode,
6e97fbb8 363 ix,iy,pad.GetPositionX(),pad.GetPositionY(),
364 pad.GetDimensionX(),pad.GetDimensionY(),
d08b5461 365 d->Charge());
9e41a340 366 if ( d->IsSaturated() ) mpad->SetSaturated(kTRUE);
367 mpad->SetUniqueID(d->GetUniqueID());
368 padArray->Add(mpad);
d08b5461 369 }
370}
371
372//_____________________________________________________________________________
2e2d0c44 373Int_t
374AliMUONSimpleClusterServer::FindMCLabel(const AliMUONCluster& cluster, Int_t detElemId, const AliMpVSegmentation* seg[2]) const
375{
376 /// Find the label of the most contributing MC track (-1 in case of failure)
377 /// The data member fDigitStore must be set
378
379 // --- get the digit (if any) located at the cluster position on both cathods ---
380 Int_t nTracks[2] = {0, 0};
381 AliMUONVDigit* digit[2] = {0x0, 0x0};
382 for (Int_t iCath = 0; iCath < 2; iCath++) {
6e97fbb8 383 AliMpPad pad
384 = seg[AliMp::GetCathodType(iCath)]->PadByPosition(cluster.Position().X(), cluster.Position().Y(),kFALSE);
2e2d0c44 385 if (pad.IsValid()) {
168e9c4d 386 digit[iCath] = fDigitStore->FindObject(detElemId, pad.GetManuId(), pad.GetManuChannel(), iCath);
2e2d0c44 387 if (digit[iCath]) nTracks[iCath] = digit[iCath]->Ntracks();
388 }
389 }
390
391 if (nTracks[0] + nTracks[1] == 0) return -1;
392
393 // --- build the list of contributing tracks and of the associated charge ---
394 Int_t* trackId = new Int_t[nTracks[0] + nTracks[1]];
395 Float_t* trackCharge = new Float_t[nTracks[0] + nTracks[1]];
396 Int_t nTracksTot = 0;
397
398 // fill with contributing tracks on first cathod
399 for (Int_t iTrack1 = 0; iTrack1 < nTracks[0]; iTrack1++) {
400 trackId[iTrack1] = digit[0]->Track(iTrack1);
401 trackCharge[iTrack1] = digit[0]->TrackCharge(iTrack1);
402 }
403 nTracksTot = nTracks[0];
404
405 // complement with contributing tracks on second cathod
406 for (Int_t iTrack2 = 0; iTrack2 < nTracks[1]; iTrack2++) {
407 Int_t trackId2 = digit[1]->Track(iTrack2);
408 // check if track exist
409 Bool_t trackExist = kFALSE;
410 for (Int_t iTrack1 = 0; iTrack1 < nTracks[0]; iTrack1++) {
411 if (trackId2 == trackId[iTrack1]) {
412 // complement existing track
413 trackCharge[iTrack1] += digit[1]->TrackCharge(iTrack2);
414 trackExist = kTRUE;
415 break;
416 }
417 }
418 // add the new track
419 if (!trackExist) {
420 trackId[nTracksTot] = trackId2;
421 trackCharge[nTracksTot] = digit[1]->TrackCharge(iTrack2);
422 nTracksTot++;
423 }
424 }
425
426 // --- Find the most contributing track ---
427 Int_t mainTrackId = -1;
428 Float_t maxCharge = 0.;
429 for (Int_t iTrack = 0; iTrack < nTracksTot; iTrack++) {
430 if (trackCharge[iTrack] > maxCharge) {
431 mainTrackId = trackId[iTrack];
432 maxCharge = trackCharge[iTrack];
433 }
434 }
435
436 delete[] trackId;
437 delete[] trackCharge;
438
439 return mainTrackId;
440}
441
442//_____________________________________________________________________________
d08b5461 443void
444AliMUONSimpleClusterServer::Print(Option_t*) const
445{
446 /// Printout for debug only
447
448 AliMpDEIterator it;
449
450 it.First();
451
452 while ( !it.IsDone() )
453 {
454 Int_t detElemId = it.CurrentDEId();
455
456 // printout the number of pads / de, and number of used pads / de
457
458 if ( ( PadArray(detElemId,0) && PadArray(detElemId,0)->GetLast() >= 0 ) ||
459 ( PadArray(detElemId,1) && PadArray(detElemId,1)->GetLast() >= 0 ) )
460 {
461 cout << Form("---- DE %04d",detElemId) << endl;
462
463 for ( Int_t cathode = 0; cathode < 2; ++cathode )
464 {
465 cout << Form(" -- Cathode %1d",cathode) << endl;
466
9e41a340 467 TObjArray* padArray = PadArray(detElemId,cathode);
d08b5461 468
469 if (!padArray)
470 {
471 cout << "no pad array" << endl;
472 }
473 else if ( padArray->GetLast() < 0 )
474 {
475 cout << "no pads" << endl;
476 }
477 else
478 {
479 TIter next(padArray);
480 AliMUONPad* pad;
481 while ( ( pad = static_cast<AliMUONPad*>(next()) ) )
482 {
483 pad->Print("full");
484 }
485 }
486 }
487 }
488 it.Next();
489 }
490}
491
492