]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSimpleClusterServer.cxx
updated vertex selection
[u/mrichter/AliRoot.git] / MUON / AliMUONSimpleClusterServer.cxx
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
20 #include "AliCodeTimer.h"
21 #include "AliLog.h"
22 #include "AliMUONCluster.h"
23 #include "AliMUONConstants.h"
24 #include "AliMUONGeometryTransformer.h"
25 #include "AliMUONPad.h"
26 #include "AliMUONTriggerTrackToTrackerClusters.h"
27 #include "AliMUONVCluster.h"
28 #include "AliMUONVClusterFinder.h"
29 #include "AliMUONVClusterStore.h"
30 #include "AliMUONVDigitStore.h"
31 #include "AliMUONRecoParam.h"
32 #include "AliMpArea.h"
33 #include "AliMpDEIterator.h"
34 #include "AliMpDEManager.h"
35 #include "AliMpExMap.h"
36 #include "AliMpPad.h"
37 #include "AliMpSegmentation.h"
38 #include "AliMpVSegmentation.h"
39 #include <Riostream.h>
40 #include <TClonesArray.h>
41 #include <TString.h>
42 #include <float.h>
43
44 /// \class AliMUONSimpleClusterServer
45 ///
46 /// Implementation of AliMUONVClusterServer interface
47 /// 
48 /// 
49 /// \author Laurent Aphecetche, Subatech
50
51 /// \cond CLASSIMP  
52 ClassImp(AliMUONSimpleClusterServer)
53 /// \endcond
54
55 namespace
56 {
57   TString AsString(const AliMpArea& area)
58   {
59     return Form("(X,Y)=(%7.3f,%7.3f) (DX,DY)=(%7.3f,%7.3f)",
60                 area.GetPositionX(),
61                 area.GetPositionY(),
62                 area.GetDimensionX(),  /// TBCL was Y !!!
63                 area.GetDimensionY());
64   }
65 }
66
67 //_____________________________________________________________________________
68 AliMUONSimpleClusterServer::AliMUONSimpleClusterServer(AliMUONVClusterFinder* clusterFinder,
69                                                        const AliMUONGeometryTransformer& transformer)
70 : AliMUONVClusterServer(),
71   fDigitStore(0x0),
72   fClusterFinder(clusterFinder),
73   fkTransformer(transformer),
74   fPads(),
75   fTriggerTrackStore(0x0),
76   fBypass(0x0)
77 {
78     /// Ctor
79     /// Note that we take ownership of the clusterFinder
80     
81     fPads[0] = new AliMpExMap;
82     fPads[1] = new AliMpExMap;
83     
84 }
85
86 //_____________________________________________________________________________
87 AliMUONSimpleClusterServer::~AliMUONSimpleClusterServer()
88 {
89   /// Dtor
90   delete fClusterFinder;
91   delete fPads[0];
92   delete fPads[1];
93   delete fBypass;
94 }
95
96 //_____________________________________________________________________________
97 Int_t 
98 AliMUONSimpleClusterServer::Clusterize(Int_t chamberId,
99                                        AliMUONVClusterStore& clusterStore,
100                                        const AliMpArea& area,
101                                        const AliMUONRecoParam* recoParam)
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   
109   AliCodeTimerAuto(Form("Chamber %d",chamberId),0);
110   
111   if ( fTriggerTrackStore && chamberId >= 6 ) 
112   {
113     return fBypass->GenerateClusters(chamberId,clusterStore);
114   }
115   
116   if (!recoParam) {
117     AliError("Reconstruction parameters are missing: unable to clusterize");
118     return 0;
119   }
120   
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       {      
154         const AliMpVSegmentation* seg[2] = 
155         { AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath0),
156           AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath1)
157         };
158         if ( fClusterFinder->NeedSegmentation() )
159         {
160           fClusterFinder->Prepare(detElemId,pads,deArea,seg);
161         }
162         else
163         {
164           fClusterFinder->Prepare(detElemId,pads,deArea);
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         
174         while ( ( cluster = fClusterFinder->NextCluster() ) ) 
175         {      
176           // add new cluster to the store with information to build its ID
177           // increment the number of clusters into the store
178           AliMUONVCluster* rawCluster = clusterStore.Add(chamberId, detElemId, fNCluster++);
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);
189             
190             // skip virtual pads
191             if (!pad->IsReal()) continue;
192             
193             rawCluster->AddDigitId(pad->GetUniqueID());
194           }
195           
196           // fill charge and other cluster informations
197           rawCluster->SetCharge(cluster->Charge());
198           rawCluster->SetChi2(cluster->Chi2());
199           
200           Double_t xg, yg, zg;
201           fkTransformer.Local2Global(detElemId, 
202                                     cluster->Position().X(), cluster->Position().Y(), 
203                                     0, xg, yg, zg);
204           rawCluster->SetXYZ(xg, yg, zg);
205           rawCluster->SetErrXY(recoParam->GetDefaultNonBendingReso(chamberId),recoParam->GetDefaultBendingReso(chamberId));
206           
207           // Set MC label
208           if (fDigitStore && fDigitStore->HasMCInformation()) 
209           {
210             rawCluster->SetMCLabel(FindMCLabel(*cluster, detElemId, seg));
211           }
212           
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",
214                           detElemId,rawCluster->GetNDigits(),rawCluster->GetCharge(),
215                           cluster->Position().X(),cluster->Position().Y(),0.0,
216                           xg,yg,zg,rawCluster->GetMCLabel()));
217         }
218       }
219     }
220     it.Next();
221   }
222   
223   AliDebug(1,Form("chamberId = %2d NofClusters after = %d",chamberId,fNCluster));
224   
225   return nofAddedClusters;
226 }
227
228
229 //_____________________________________________________________________________
230 void
231 AliMUONSimpleClusterServer::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   
240   fkTransformer.Global2Local(detElemId,
241                              globalArea.GetPositionX(),globalArea.GetPositionY(),zg,
242                              xl,yl,zl);
243   
244   localArea = AliMpArea(xl,yl, globalArea.GetDimensionX(), globalArea.GetDimensionY());
245 }
246
247 //_____________________________________________________________________________
248 Bool_t
249 AliMUONSimpleClusterServer::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   
259   AliMpArea* globalDEArea = fkTransformer.GetDEArea(detElemId);
260   
261   if (!globalDEArea) return kFALSE;
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 //_____________________________________________________________________________
288 TClonesArray* 
289 AliMUONSimpleClusterServer::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 //_____________________________________________________________________________
297 Bool_t 
298 AliMUONSimpleClusterServer::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;
303   fBypass = new AliMUONTriggerTrackToTrackerClusters(fkTransformer,fTriggerTrackStore);
304   return kTRUE;
305 }
306
307 //_____________________________________________________________________________
308 void 
309 AliMUONSimpleClusterServer::UseDigits(TIter& next, AliMUONVDigitStore* digitStore)
310 {
311   /// Convert digitStore into two arrays of AliMUONPads
312   
313   fDigitStore = digitStore;
314   
315   fPads[0]->Clear();
316   fPads[1]->Clear();
317   
318   AliMUONVDigit* d;
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));
328     AliMpPad pad = seg->PadByIndices(ix,iy);
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,
338                     ix,iy,pad.GetPositionX(),pad.GetPositionY(),
339                     pad.GetDimensionX(),pad.GetDimensionY(),
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 //_____________________________________________________________________________
348 Int_t
349 AliMUONSimpleClusterServer::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++) {
358     AliMpPad pad 
359       = seg[AliMp::GetCathodType(iCath)]->PadByPosition(cluster.Position().X(), cluster.Position().Y(),kFALSE);
360     if (pad.IsValid()) {
361       digit[iCath] = fDigitStore->FindObject(detElemId, pad.GetManuId(), pad.GetManuChannel(), iCath);
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 //_____________________________________________________________________________
418 void 
419 AliMUONSimpleClusterServer::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