ROOT logo
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
*                                                                        *
* Author: The ALICE Off-line Project.                                    *
* Contributors are mentioned in the code where appropriate.              *
*                                                                        *
* Permission to use, copy, modify and distribute this software and its   *
* documentation strictly for non-commercial purposes is hereby granted   *
* without fee, provided that the above copyright notice appears in all   *
* copies and that both the copyright notice and this permission notice   *
* appear in the supporting documentation. The authors make no claims     *
* about the suitability of this software for any purpose. It is          *
* provided "as is" without express or implied warranty.                  *
**************************************************************************/

// $Id$

#include "AliMUONPreClusterFinderV2.h"

#include "AliLog.h"
#include "AliMUONCluster.h"
#include "AliMpVSegmentation.h"
#include "AliMpPad.h"
#include "TObjArray.h"
#include "TVector2.h"
#include "AliMUONPad.h"
#include "AliMUONVDigit.h"
#include "AliMUONVDigitStore.h"

//-----------------------------------------------------------------------------
/// \class AliMUONPreClusterFinderV2
///
/// Implementation of AliMUONVClusterFinder
///
/// This one ressembles the preclustering stage in the original ClusterFinderAZ
///
/// \author Laurent Aphecetche
//-----------------------------------------------------------------------------

ClassImp(AliMUONPreClusterFinderV2)

//_____________________________________________________________________________
AliMUONPreClusterFinderV2::AliMUONPreClusterFinderV2()
: AliMUONVClusterFinder(),
  fClusters(0x0),
  fkSegmentations(0x0),
  fPads(0x0),
  fDetElemId(0)
{
    /// ctor
}

//_____________________________________________________________________________
AliMUONPreClusterFinderV2::~AliMUONPreClusterFinderV2()
{
  /// dtor : note we're owner of the clusters, but not of the pads
  delete fClusters;
}

//_____________________________________________________________________________
Bool_t
AliMUONPreClusterFinderV2::UsePad(const AliMUONPad& pad)
{
  /// Add a pad to the list of pads to be considered
  if ( pad.DetElemId() != fDetElemId )
  {
    AliError(Form("Cannot add pad from DE %d to this cluster finder which is "
                  "currently dealing with DE %d",pad.DetElemId(),fDetElemId));
    return kFALSE;
  }
  
  fPads[pad.Cathode()]->Add(new AliMUONPad(pad)); 
  // FIXME: should set the ClusterId of that new pad to be -1
  return kTRUE;
}

//_____________________________________________________________________________
Bool_t
AliMUONPreClusterFinderV2::Prepare(Int_t detElemId,
                                   TObjArray* pads[2],
                                   const AliMpArea& area,
                                   const AliMpVSegmentation* seg[2])
{
  /// Prepare for clustering, by giving access to segmentations and digit lists
  
  if ( area.IsValid() ) 
  {
    AliError("Handling of area not yet implemented for this class. Please check");
  }
  
  delete fClusters;
  fClusters = new TClonesArray("AliMUONCluster");

  fPads = pads;
  fkSegmentations = seg;
  
  fDetElemId = detElemId;
  
  if ( fPads[0]->GetLast() < 0 && fPads[1]->GetLast() < 0 )
  {
    // no pad at all, nothing to do...
    return kFALSE;
  }

  return kTRUE;
}

//_____________________________________________________________________________
void
AliMUONPreClusterFinderV2::AddPad(AliMUONCluster& cluster, AliMUONPad* pad)
{
  /// Add a pad to a cluster
  
  AliMUONPad* addedPad = cluster.AddPad(*pad);
  
  Int_t cathode = pad->Cathode();
  TObjArray& padArray = *fPads[cathode];
  delete padArray.Remove(pad);
  
  // Check neighbours
  TObjArray neighbours;
  AliMpPad p = fkSegmentations[cathode]->PadByIndices(addedPad->Ix(),addedPad->Iy(),kTRUE);
  Int_t nn = fkSegmentations[cathode]->GetNeighbours(p,neighbours);
  for (Int_t in = 0; in < nn; ++in) 
  {
    AliMpPad* p1 = static_cast<AliMpPad*>(neighbours.At(in));
    
    TIter next2(&padArray);
    AliMUONPad* p2;
    
    while ( ( p2 = static_cast<AliMUONPad*>(next2()) ) )
    {
        if ( !p2->IsUsed() && p2->Ix()==p1->GetIx() 
             && p2->Iy() == p1->GetIy() &&
             p2->Cathode() == cathode )
        {
          AddPad(cluster,p2);
        }
    }
  } // for (Int_t in = 0;
}

namespace
{
//_____________________________________________________________________________
Bool_t
AreOverlapping(const AliMUONPad& pad, const AliMUONCluster& cluster)
{
  /// Whether the pad overlaps with the cluster
  
  static Double_t precision = 1E-4; // cm
  static TVector2 precisionAdjustment(precision,precision);//-precision,-precision);
  for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
  {
    AliMUONPad* testPad = cluster.Pad(i);
    // Note: we use negative precision numbers, meaning
    // the area of the pads will be *increased* by these small numbers
    // prior to check the overlap by the AreOverlapping method,
    // so pads touching only by the corners will be considered as
    // overlapping.    
    if ( AliMUONPad::AreOverlapping(*testPad,pad,precisionAdjustment) )
    {
      return kTRUE;
    }
  }
  return kFALSE;
}
}

//_____________________________________________________________________________
AliMUONCluster* 
AliMUONPreClusterFinderV2::NextCluster()
{
  /// Builds the next cluster, and returns it.
  
  // Start a new cluster
  Int_t id = fClusters->GetLast()+1;
  AliMUONCluster* cluster = new ((*fClusters)[id]) AliMUONCluster;
  cluster->SetUniqueID(id);
  
  AliMUONPad* pad;
  TIter next(fPads[0]);
  while (  ( pad = static_cast<AliMUONPad*>(next())) && pad->IsUsed() ) {}

  if (!pad) // protection against no pad in first cathode, which might happen
  {
    // try other cathode
    TIter next2(fPads[1]);
    while (  ( pad = static_cast<AliMUONPad*>(next2())) && pad->IsUsed() ) {}
    if (!pad) 
    {
      // we are done.
      return 0x0;
    }
    // Builds (recursively) a cluster on second cathode only
    AddPad(*cluster,pad);
  }
  else
  {
    // Builds (recursively) a cluster on first cathode only
      
    AddPad(*cluster,pad);

    // On the 2nd cathode, only add pads overlapping with the current cluster
    TIter next1(fPads[1]);
    AliMUONPad* testPad;
  
    while ( ( testPad = static_cast<AliMUONPad*>(next1())))
    {
      if ( !testPad->IsUsed() && AreOverlapping(*testPad,*cluster) )
      {
        AddPad(*cluster,testPad);
      }
    }
  }
  
  if ( cluster->Multiplicity() <= 1 )
  {
    if ( cluster->Multiplicity() == 0 ) 
    {
      // no pad is suspicious
      AliWarning("Got an empty cluster...");
    }
    // else only 1 pad (not suspicious, but kind of useless, probably noise)
    // so we remove it from our list
    fClusters->Remove(cluster);
    fClusters->Compress();
    // then proceed further
    return NextCluster();
  }
  
  return cluster;
}
 AliMUONPreClusterFinderV2.cxx:1
 AliMUONPreClusterFinderV2.cxx:2
 AliMUONPreClusterFinderV2.cxx:3
 AliMUONPreClusterFinderV2.cxx:4
 AliMUONPreClusterFinderV2.cxx:5
 AliMUONPreClusterFinderV2.cxx:6
 AliMUONPreClusterFinderV2.cxx:7
 AliMUONPreClusterFinderV2.cxx:8
 AliMUONPreClusterFinderV2.cxx:9
 AliMUONPreClusterFinderV2.cxx:10
 AliMUONPreClusterFinderV2.cxx:11
 AliMUONPreClusterFinderV2.cxx:12
 AliMUONPreClusterFinderV2.cxx:13
 AliMUONPreClusterFinderV2.cxx:14
 AliMUONPreClusterFinderV2.cxx:15
 AliMUONPreClusterFinderV2.cxx:16
 AliMUONPreClusterFinderV2.cxx:17
 AliMUONPreClusterFinderV2.cxx:18
 AliMUONPreClusterFinderV2.cxx:19
 AliMUONPreClusterFinderV2.cxx:20
 AliMUONPreClusterFinderV2.cxx:21
 AliMUONPreClusterFinderV2.cxx:22
 AliMUONPreClusterFinderV2.cxx:23
 AliMUONPreClusterFinderV2.cxx:24
 AliMUONPreClusterFinderV2.cxx:25
 AliMUONPreClusterFinderV2.cxx:26
 AliMUONPreClusterFinderV2.cxx:27
 AliMUONPreClusterFinderV2.cxx:28
 AliMUONPreClusterFinderV2.cxx:29
 AliMUONPreClusterFinderV2.cxx:30
 AliMUONPreClusterFinderV2.cxx:31
 AliMUONPreClusterFinderV2.cxx:32
 AliMUONPreClusterFinderV2.cxx:33
 AliMUONPreClusterFinderV2.cxx:34
 AliMUONPreClusterFinderV2.cxx:35
 AliMUONPreClusterFinderV2.cxx:36
 AliMUONPreClusterFinderV2.cxx:37
 AliMUONPreClusterFinderV2.cxx:38
 AliMUONPreClusterFinderV2.cxx:39
 AliMUONPreClusterFinderV2.cxx:40
 AliMUONPreClusterFinderV2.cxx:41
 AliMUONPreClusterFinderV2.cxx:42
 AliMUONPreClusterFinderV2.cxx:43
 AliMUONPreClusterFinderV2.cxx:44
 AliMUONPreClusterFinderV2.cxx:45
 AliMUONPreClusterFinderV2.cxx:46
 AliMUONPreClusterFinderV2.cxx:47
 AliMUONPreClusterFinderV2.cxx:48
 AliMUONPreClusterFinderV2.cxx:49
 AliMUONPreClusterFinderV2.cxx:50
 AliMUONPreClusterFinderV2.cxx:51
 AliMUONPreClusterFinderV2.cxx:52
 AliMUONPreClusterFinderV2.cxx:53
 AliMUONPreClusterFinderV2.cxx:54
 AliMUONPreClusterFinderV2.cxx:55
 AliMUONPreClusterFinderV2.cxx:56
 AliMUONPreClusterFinderV2.cxx:57
 AliMUONPreClusterFinderV2.cxx:58
 AliMUONPreClusterFinderV2.cxx:59
 AliMUONPreClusterFinderV2.cxx:60
 AliMUONPreClusterFinderV2.cxx:61
 AliMUONPreClusterFinderV2.cxx:62
 AliMUONPreClusterFinderV2.cxx:63
 AliMUONPreClusterFinderV2.cxx:64
 AliMUONPreClusterFinderV2.cxx:65
 AliMUONPreClusterFinderV2.cxx:66
 AliMUONPreClusterFinderV2.cxx:67
 AliMUONPreClusterFinderV2.cxx:68
 AliMUONPreClusterFinderV2.cxx:69
 AliMUONPreClusterFinderV2.cxx:70
 AliMUONPreClusterFinderV2.cxx:71
 AliMUONPreClusterFinderV2.cxx:72
 AliMUONPreClusterFinderV2.cxx:73
 AliMUONPreClusterFinderV2.cxx:74
 AliMUONPreClusterFinderV2.cxx:75
 AliMUONPreClusterFinderV2.cxx:76
 AliMUONPreClusterFinderV2.cxx:77
 AliMUONPreClusterFinderV2.cxx:78
 AliMUONPreClusterFinderV2.cxx:79
 AliMUONPreClusterFinderV2.cxx:80
 AliMUONPreClusterFinderV2.cxx:81
 AliMUONPreClusterFinderV2.cxx:82
 AliMUONPreClusterFinderV2.cxx:83
 AliMUONPreClusterFinderV2.cxx:84
 AliMUONPreClusterFinderV2.cxx:85
 AliMUONPreClusterFinderV2.cxx:86
 AliMUONPreClusterFinderV2.cxx:87
 AliMUONPreClusterFinderV2.cxx:88
 AliMUONPreClusterFinderV2.cxx:89
 AliMUONPreClusterFinderV2.cxx:90
 AliMUONPreClusterFinderV2.cxx:91
 AliMUONPreClusterFinderV2.cxx:92
 AliMUONPreClusterFinderV2.cxx:93
 AliMUONPreClusterFinderV2.cxx:94
 AliMUONPreClusterFinderV2.cxx:95
 AliMUONPreClusterFinderV2.cxx:96
 AliMUONPreClusterFinderV2.cxx:97
 AliMUONPreClusterFinderV2.cxx:98
 AliMUONPreClusterFinderV2.cxx:99
 AliMUONPreClusterFinderV2.cxx:100
 AliMUONPreClusterFinderV2.cxx:101
 AliMUONPreClusterFinderV2.cxx:102
 AliMUONPreClusterFinderV2.cxx:103
 AliMUONPreClusterFinderV2.cxx:104
 AliMUONPreClusterFinderV2.cxx:105
 AliMUONPreClusterFinderV2.cxx:106
 AliMUONPreClusterFinderV2.cxx:107
 AliMUONPreClusterFinderV2.cxx:108
 AliMUONPreClusterFinderV2.cxx:109
 AliMUONPreClusterFinderV2.cxx:110
 AliMUONPreClusterFinderV2.cxx:111
 AliMUONPreClusterFinderV2.cxx:112
 AliMUONPreClusterFinderV2.cxx:113
 AliMUONPreClusterFinderV2.cxx:114
 AliMUONPreClusterFinderV2.cxx:115
 AliMUONPreClusterFinderV2.cxx:116
 AliMUONPreClusterFinderV2.cxx:117
 AliMUONPreClusterFinderV2.cxx:118
 AliMUONPreClusterFinderV2.cxx:119
 AliMUONPreClusterFinderV2.cxx:120
 AliMUONPreClusterFinderV2.cxx:121
 AliMUONPreClusterFinderV2.cxx:122
 AliMUONPreClusterFinderV2.cxx:123
 AliMUONPreClusterFinderV2.cxx:124
 AliMUONPreClusterFinderV2.cxx:125
 AliMUONPreClusterFinderV2.cxx:126
 AliMUONPreClusterFinderV2.cxx:127
 AliMUONPreClusterFinderV2.cxx:128
 AliMUONPreClusterFinderV2.cxx:129
 AliMUONPreClusterFinderV2.cxx:130
 AliMUONPreClusterFinderV2.cxx:131
 AliMUONPreClusterFinderV2.cxx:132
 AliMUONPreClusterFinderV2.cxx:133
 AliMUONPreClusterFinderV2.cxx:134
 AliMUONPreClusterFinderV2.cxx:135
 AliMUONPreClusterFinderV2.cxx:136
 AliMUONPreClusterFinderV2.cxx:137
 AliMUONPreClusterFinderV2.cxx:138
 AliMUONPreClusterFinderV2.cxx:139
 AliMUONPreClusterFinderV2.cxx:140
 AliMUONPreClusterFinderV2.cxx:141
 AliMUONPreClusterFinderV2.cxx:142
 AliMUONPreClusterFinderV2.cxx:143
 AliMUONPreClusterFinderV2.cxx:144
 AliMUONPreClusterFinderV2.cxx:145
 AliMUONPreClusterFinderV2.cxx:146
 AliMUONPreClusterFinderV2.cxx:147
 AliMUONPreClusterFinderV2.cxx:148
 AliMUONPreClusterFinderV2.cxx:149
 AliMUONPreClusterFinderV2.cxx:150
 AliMUONPreClusterFinderV2.cxx:151
 AliMUONPreClusterFinderV2.cxx:152
 AliMUONPreClusterFinderV2.cxx:153
 AliMUONPreClusterFinderV2.cxx:154
 AliMUONPreClusterFinderV2.cxx:155
 AliMUONPreClusterFinderV2.cxx:156
 AliMUONPreClusterFinderV2.cxx:157
 AliMUONPreClusterFinderV2.cxx:158
 AliMUONPreClusterFinderV2.cxx:159
 AliMUONPreClusterFinderV2.cxx:160
 AliMUONPreClusterFinderV2.cxx:161
 AliMUONPreClusterFinderV2.cxx:162
 AliMUONPreClusterFinderV2.cxx:163
 AliMUONPreClusterFinderV2.cxx:164
 AliMUONPreClusterFinderV2.cxx:165
 AliMUONPreClusterFinderV2.cxx:166
 AliMUONPreClusterFinderV2.cxx:167
 AliMUONPreClusterFinderV2.cxx:168
 AliMUONPreClusterFinderV2.cxx:169
 AliMUONPreClusterFinderV2.cxx:170
 AliMUONPreClusterFinderV2.cxx:171
 AliMUONPreClusterFinderV2.cxx:172
 AliMUONPreClusterFinderV2.cxx:173
 AliMUONPreClusterFinderV2.cxx:174
 AliMUONPreClusterFinderV2.cxx:175
 AliMUONPreClusterFinderV2.cxx:176
 AliMUONPreClusterFinderV2.cxx:177
 AliMUONPreClusterFinderV2.cxx:178
 AliMUONPreClusterFinderV2.cxx:179
 AliMUONPreClusterFinderV2.cxx:180
 AliMUONPreClusterFinderV2.cxx:181
 AliMUONPreClusterFinderV2.cxx:182
 AliMUONPreClusterFinderV2.cxx:183
 AliMUONPreClusterFinderV2.cxx:184
 AliMUONPreClusterFinderV2.cxx:185
 AliMUONPreClusterFinderV2.cxx:186
 AliMUONPreClusterFinderV2.cxx:187
 AliMUONPreClusterFinderV2.cxx:188
 AliMUONPreClusterFinderV2.cxx:189
 AliMUONPreClusterFinderV2.cxx:190
 AliMUONPreClusterFinderV2.cxx:191
 AliMUONPreClusterFinderV2.cxx:192
 AliMUONPreClusterFinderV2.cxx:193
 AliMUONPreClusterFinderV2.cxx:194
 AliMUONPreClusterFinderV2.cxx:195
 AliMUONPreClusterFinderV2.cxx:196
 AliMUONPreClusterFinderV2.cxx:197
 AliMUONPreClusterFinderV2.cxx:198
 AliMUONPreClusterFinderV2.cxx:199
 AliMUONPreClusterFinderV2.cxx:200
 AliMUONPreClusterFinderV2.cxx:201
 AliMUONPreClusterFinderV2.cxx:202
 AliMUONPreClusterFinderV2.cxx:203
 AliMUONPreClusterFinderV2.cxx:204
 AliMUONPreClusterFinderV2.cxx:205
 AliMUONPreClusterFinderV2.cxx:206
 AliMUONPreClusterFinderV2.cxx:207
 AliMUONPreClusterFinderV2.cxx:208
 AliMUONPreClusterFinderV2.cxx:209
 AliMUONPreClusterFinderV2.cxx:210
 AliMUONPreClusterFinderV2.cxx:211
 AliMUONPreClusterFinderV2.cxx:212
 AliMUONPreClusterFinderV2.cxx:213
 AliMUONPreClusterFinderV2.cxx:214
 AliMUONPreClusterFinderV2.cxx:215
 AliMUONPreClusterFinderV2.cxx:216
 AliMUONPreClusterFinderV2.cxx:217
 AliMUONPreClusterFinderV2.cxx:218
 AliMUONPreClusterFinderV2.cxx:219
 AliMUONPreClusterFinderV2.cxx:220
 AliMUONPreClusterFinderV2.cxx:221
 AliMUONPreClusterFinderV2.cxx:222
 AliMUONPreClusterFinderV2.cxx:223
 AliMUONPreClusterFinderV2.cxx:224
 AliMUONPreClusterFinderV2.cxx:225
 AliMUONPreClusterFinderV2.cxx:226
 AliMUONPreClusterFinderV2.cxx:227
 AliMUONPreClusterFinderV2.cxx:228
 AliMUONPreClusterFinderV2.cxx:229
 AliMUONPreClusterFinderV2.cxx:230
 AliMUONPreClusterFinderV2.cxx:231
 AliMUONPreClusterFinderV2.cxx:232
 AliMUONPreClusterFinderV2.cxx:233