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