ROOT logo
// $Id$
// $MpId: testSlatPads.C,v 1.1 2005/09/19 19:02:53 ivana Exp $

/// Macro to check segmentation of slats.
///
/// What is tested :
/// - first, that we can read both bending and non-bending for each slat
/// - next, for each plane (b,nb), we retrieve all the pads
///   in two different ways a) using a loop and HasPad method
///   b) and using pad iterator. We check that both lead to the same set of pads
///   and that each pad is retrieved only once ;-)
/// - finally we do a "circular test" for all pads, i.e. we get a pad p1
///   by indices and use p1's position to feed PadByPosition which gives us a
///   pad p2. p1 should be equal to p2, of course.
///
/// Usage : .L testSlatPads.C++
///         testSlatPads("slats.list"); where slats.list contains
///         the list of slats' name to be tested, e.g. 112233NR3, etc...

#if !defined(__CINT__) || defined(__MAKECINT__)

#include "AliMpStation12Type.h"
#include "AliMpPlaneType.h"
#include "AliMpDataProcessor.h"
#include "AliMpDataMap.h"
#include "AliMpDataStreams.h"
#include "AliMpSlatMotifMap.h"
#include "AliMpSlat.h"
#include "AliMpSlatSegmentation.h"
#include "AliMpPad.h"
#include "AliMpVPadIterator.h"
#include "AliMpArea.h"
#include "AliMpSt345Reader.h"
#include "AliMpIntPair.h"
#include "slats.h"

#include <TObjArray.h>
#include <TObjString.h>
#include <TList.h>
#include <TString.h>
#include <TStopwatch.h>
#include <Riostream.h>
#endif

//______________________________________________________________________________
Int_t CircularTest(const AliMpSlat& slat)
{
  Int_t n = -1;
  AliMpSlatSegmentation seg(&slat);
  
  for ( Int_t i = 0; i <= seg.MaxPadIndexX(); ++i )
  {
    for ( Int_t j = 0; j <= seg.MaxPadIndexY(); ++j )
    {
      AliMpPad pad = seg.PadByIndices(i,j,kFALSE);
      
      if ( pad.IsValid() )
      {
        ++n;
        AliMpPad xcheck 
          = seg.PadByPosition(pad.GetPositionX(),pad.GetPositionY(),kFALSE);
        if ( pad != xcheck ) 
        {
          cout << "(ix,iy)=" << i << "," << j << " ";
          pad.Print();
          cout << endl;
          xcheck.Print();
          return kFALSE;
        }             
      }
    }
  }
  return n;
}

//______________________________________________________________________________
Int_t Count(const AliMpSlat& slat)
{
  Int_t n = 0;
  AliMpSlatSegmentation seg(&slat);
  
  for ( Int_t i = 0; i <= seg.MaxPadIndexX(); ++i )
  {
    for ( Int_t j = 0; j <= seg.MaxPadIndexY(); ++j )
    {
      if ( seg.HasPadByIndices(i,j) ) 
      {
        ++n;
      }
    }
  }
  Int_t n2 = seg.NofPads();
  if ( n2 != n ) 
  {
    cout << Form("Count (%d) and NofPads (%d) give different results for slat %s",
                 n,n2,slat.GetName()) << endl;
  }
  return n;
}

//______________________________________________________________________________
Int_t Iterate(const AliMpSlat& slat)
{
  AliMpSlatSegmentation seg(&slat);
  
  AliMpVPadIterator* it = seg.CreateIterator();
  
  it->First();
  
  Int_t n = 0;
  
  while ( !it->IsDone() )
  {
    it->Next();
    ++n;
  }
  
  return n;
}

//______________________________________________________________________________
Int_t Contains(const TList& list, const AliMpPad& pad)
{
  TIter next(&list);
  AliMpPad* p;
  Int_t n(0);
  
  while ( ( p = (AliMpPad*)next() ) )
  {
    if ( pad == *p ) ++n;
  }
  return n;
}

//______________________________________________________________________________
void CheckMultiple(const TList& list, const char* title, const char* what)
{
  // check whether the list contains each pad only once
  
  TIter next(&list);
  AliMpPad* pad;
  while ( ( pad = (AliMpPad*)next() ) )
  {
    if ( Contains(list,*pad) != 1 )
    {
      cout << title << " " << what << " pad found more than once : " << endl;
      pad->Print();
    }
  }
      
}

//______________________________________________________________________________
void XCheck(const AliMpSlat& slat)
{
  // find out which pads are not found by iterator, as compared to HasPad method
  TList l1,l2;
  l1.SetOwner(kTRUE);
  l2.SetOwner(kTRUE);
  
  AliMpSlatSegmentation seg(&slat);
  
  for ( Int_t i = 0; i <= seg.MaxPadIndexX(); ++i )
  {
    for ( Int_t j = 0; j <= seg.MaxPadIndexY(); ++j )
    {
      AliMpPad pad = seg.PadByIndices(i,j,kFALSE);
      if ( pad.IsValid() )
      {
        l1.Add(new AliMpPad(pad));
      }
    }
  }
  
  AliMpVPadIterator* it = seg.CreateIterator();
  
  it->First();
    
  while ( !it->IsDone() )
  {
    l2.Add(new AliMpPad(it->CurrentItem()));
    it->Next();
  }

  TIter next(&l1);
  AliMpPad* pad;
  while ( ( pad = (AliMpPad*)next() ) )
  {
    if ( Contains(l2,*pad) != 1)
    {
      cout << "The following pad is not found by iterator : " << endl;
      pad->Print();
    }
  }
  
  CheckMultiple(l2,slat.GetName(),"iterator");

  CheckMultiple(l1,slat.GetName(),"padByIndices");
}

//______________________________________________________________________________
void testSt345Pads()
{
  AliMpDataProcessor mp;
  AliMpDataMap* dataMap = mp.CreateDataMap("data");
  AliMpDataStreams dataStreams(dataMap);

  AliMpSlatMotifMap* motifMap = new AliMpSlatMotifMap();
  AliMpSt345Reader reader(dataStreams, motifMap);
  
  Int_t ok(0);
  
  for ( Int_t i = 0; i < NSLATS; ++i ) 
  {
    Bool_t slatOK(kTRUE);
    
    TString slatName( slatTypeNames[i] );
    
    AliMpSlat* bending = reader.ReadSlat(slatName.Data(),AliMp::kBendingPlane);
    AliMpSlat* nonbending = reader.ReadSlat(slatName.Data(),AliMp::kNonBendingPlane);
  
    Int_t NumberOfBendingPads(0);
    Int_t NumberOfNonBendingPads(0);
    Int_t xcheck_b(0);
    Int_t xcheck_nb(0);
    Int_t nc_b(0);
    Int_t nc_nb(0);
    
    if ( bending )
    {
      NumberOfBendingPads = Count(*bending);
      xcheck_b = Iterate(*bending);   
      nc_b = CircularTest(*bending);   
    }
    else
    {
      cout << "Could not read bending plane of slat " << slatName.Data() << endl;
    }
    
    if ( nonbending ) 
    {
      NumberOfNonBendingPads = Count(*nonbending);
      xcheck_nb = Iterate(*nonbending);
      nc_nb = CircularTest(*nonbending);
    }
    else
    {
      cout << "Could not read bending plane of slat " << slatName.Data() << endl;
    }
    
    cout << setw(10) << slatName
      << " BENDING : " << setw(5) << NumberOfBendingPads
      << " NONBENDING : " << setw(5) << NumberOfNonBendingPads
      << " CT for " << (nc_b+nc_nb) << " pads ";
    
    if ( nc_b>0 && nc_nb>0 ) 
    {
      cout << "OK.";
    }
    else
    {
      slatOK = kFALSE;
      cout << "FAILED.";
    }
    cout << endl;

    if ( nonbending ) XCheck(*nonbending);
    if ( bending ) XCheck(*bending);

    if ( xcheck_b != NumberOfBendingPads )
    {
      cout << setw(20) << " Bending : HasPad and Iterator give different results !" 
      << " " << NumberOfBendingPads << " vs " << xcheck_b << endl;
      slatOK = kFALSE;
    }
    if ( xcheck_nb != NumberOfNonBendingPads )
    {
      cout << setw(20) << " NonBending : HasPad and Iterator give different results !"
      << " " << NumberOfNonBendingPads << " vs " << xcheck_nb << endl;
      slatOK = kFALSE;
    }

    if (slatOK) ++ok;
   }
  
  if ( ok == NSLATS ) 
  {
    cout << "Successfully tested " << ok << " slats" << endl;
  }
  else
  {
    cout << "Failed to read " << (NSLATS-ok) << " out of " << NSLATS << " slats" << endl;
  }
}
 testSt345Pads.C:1
 testSt345Pads.C:2
 testSt345Pads.C:3
 testSt345Pads.C:4
 testSt345Pads.C:5
 testSt345Pads.C:6
 testSt345Pads.C:7
 testSt345Pads.C:8
 testSt345Pads.C:9
 testSt345Pads.C:10
 testSt345Pads.C:11
 testSt345Pads.C:12
 testSt345Pads.C:13
 testSt345Pads.C:14
 testSt345Pads.C:15
 testSt345Pads.C:16
 testSt345Pads.C:17
 testSt345Pads.C:18
 testSt345Pads.C:19
 testSt345Pads.C:20
 testSt345Pads.C:21
 testSt345Pads.C:22
 testSt345Pads.C:23
 testSt345Pads.C:24
 testSt345Pads.C:25
 testSt345Pads.C:26
 testSt345Pads.C:27
 testSt345Pads.C:28
 testSt345Pads.C:29
 testSt345Pads.C:30
 testSt345Pads.C:31
 testSt345Pads.C:32
 testSt345Pads.C:33
 testSt345Pads.C:34
 testSt345Pads.C:35
 testSt345Pads.C:36
 testSt345Pads.C:37
 testSt345Pads.C:38
 testSt345Pads.C:39
 testSt345Pads.C:40
 testSt345Pads.C:41
 testSt345Pads.C:42
 testSt345Pads.C:43
 testSt345Pads.C:44
 testSt345Pads.C:45
 testSt345Pads.C:46
 testSt345Pads.C:47
 testSt345Pads.C:48
 testSt345Pads.C:49
 testSt345Pads.C:50
 testSt345Pads.C:51
 testSt345Pads.C:52
 testSt345Pads.C:53
 testSt345Pads.C:54
 testSt345Pads.C:55
 testSt345Pads.C:56
 testSt345Pads.C:57
 testSt345Pads.C:58
 testSt345Pads.C:59
 testSt345Pads.C:60
 testSt345Pads.C:61
 testSt345Pads.C:62
 testSt345Pads.C:63
 testSt345Pads.C:64
 testSt345Pads.C:65
 testSt345Pads.C:66
 testSt345Pads.C:67
 testSt345Pads.C:68
 testSt345Pads.C:69
 testSt345Pads.C:70
 testSt345Pads.C:71
 testSt345Pads.C:72
 testSt345Pads.C:73
 testSt345Pads.C:74
 testSt345Pads.C:75
 testSt345Pads.C:76
 testSt345Pads.C:77
 testSt345Pads.C:78
 testSt345Pads.C:79
 testSt345Pads.C:80
 testSt345Pads.C:81
 testSt345Pads.C:82
 testSt345Pads.C:83
 testSt345Pads.C:84
 testSt345Pads.C:85
 testSt345Pads.C:86
 testSt345Pads.C:87
 testSt345Pads.C:88
 testSt345Pads.C:89
 testSt345Pads.C:90
 testSt345Pads.C:91
 testSt345Pads.C:92
 testSt345Pads.C:93
 testSt345Pads.C:94
 testSt345Pads.C:95
 testSt345Pads.C:96
 testSt345Pads.C:97
 testSt345Pads.C:98
 testSt345Pads.C:99
 testSt345Pads.C:100
 testSt345Pads.C:101
 testSt345Pads.C:102
 testSt345Pads.C:103
 testSt345Pads.C:104
 testSt345Pads.C:105
 testSt345Pads.C:106
 testSt345Pads.C:107
 testSt345Pads.C:108
 testSt345Pads.C:109
 testSt345Pads.C:110
 testSt345Pads.C:111
 testSt345Pads.C:112
 testSt345Pads.C:113
 testSt345Pads.C:114
 testSt345Pads.C:115
 testSt345Pads.C:116
 testSt345Pads.C:117
 testSt345Pads.C:118
 testSt345Pads.C:119
 testSt345Pads.C:120
 testSt345Pads.C:121
 testSt345Pads.C:122
 testSt345Pads.C:123
 testSt345Pads.C:124
 testSt345Pads.C:125
 testSt345Pads.C:126
 testSt345Pads.C:127
 testSt345Pads.C:128
 testSt345Pads.C:129
 testSt345Pads.C:130
 testSt345Pads.C:131
 testSt345Pads.C:132
 testSt345Pads.C:133
 testSt345Pads.C:134
 testSt345Pads.C:135
 testSt345Pads.C:136
 testSt345Pads.C:137
 testSt345Pads.C:138
 testSt345Pads.C:139
 testSt345Pads.C:140
 testSt345Pads.C:141
 testSt345Pads.C:142
 testSt345Pads.C:143
 testSt345Pads.C:144
 testSt345Pads.C:145
 testSt345Pads.C:146
 testSt345Pads.C:147
 testSt345Pads.C:148
 testSt345Pads.C:149
 testSt345Pads.C:150
 testSt345Pads.C:151
 testSt345Pads.C:152
 testSt345Pads.C:153
 testSt345Pads.C:154
 testSt345Pads.C:155
 testSt345Pads.C:156
 testSt345Pads.C:157
 testSt345Pads.C:158
 testSt345Pads.C:159
 testSt345Pads.C:160
 testSt345Pads.C:161
 testSt345Pads.C:162
 testSt345Pads.C:163
 testSt345Pads.C:164
 testSt345Pads.C:165
 testSt345Pads.C:166
 testSt345Pads.C:167
 testSt345Pads.C:168
 testSt345Pads.C:169
 testSt345Pads.C:170
 testSt345Pads.C:171
 testSt345Pads.C:172
 testSt345Pads.C:173
 testSt345Pads.C:174
 testSt345Pads.C:175
 testSt345Pads.C:176
 testSt345Pads.C:177
 testSt345Pads.C:178
 testSt345Pads.C:179
 testSt345Pads.C:180
 testSt345Pads.C:181
 testSt345Pads.C:182
 testSt345Pads.C:183
 testSt345Pads.C:184
 testSt345Pads.C:185
 testSt345Pads.C:186
 testSt345Pads.C:187
 testSt345Pads.C:188
 testSt345Pads.C:189
 testSt345Pads.C:190
 testSt345Pads.C:191
 testSt345Pads.C:192
 testSt345Pads.C:193
 testSt345Pads.C:194
 testSt345Pads.C:195
 testSt345Pads.C:196
 testSt345Pads.C:197
 testSt345Pads.C:198
 testSt345Pads.C:199
 testSt345Pads.C:200
 testSt345Pads.C:201
 testSt345Pads.C:202
 testSt345Pads.C:203
 testSt345Pads.C:204
 testSt345Pads.C:205
 testSt345Pads.C:206
 testSt345Pads.C:207
 testSt345Pads.C:208
 testSt345Pads.C:209
 testSt345Pads.C:210
 testSt345Pads.C:211
 testSt345Pads.C:212
 testSt345Pads.C:213
 testSt345Pads.C:214
 testSt345Pads.C:215
 testSt345Pads.C:216
 testSt345Pads.C:217
 testSt345Pads.C:218
 testSt345Pads.C:219
 testSt345Pads.C:220
 testSt345Pads.C:221
 testSt345Pads.C:222
 testSt345Pads.C:223
 testSt345Pads.C:224
 testSt345Pads.C:225
 testSt345Pads.C:226
 testSt345Pads.C:227
 testSt345Pads.C:228
 testSt345Pads.C:229
 testSt345Pads.C:230
 testSt345Pads.C:231
 testSt345Pads.C:232
 testSt345Pads.C:233
 testSt345Pads.C:234
 testSt345Pads.C:235
 testSt345Pads.C:236
 testSt345Pads.C:237
 testSt345Pads.C:238
 testSt345Pads.C:239
 testSt345Pads.C:240
 testSt345Pads.C:241
 testSt345Pads.C:242
 testSt345Pads.C:243
 testSt345Pads.C:244
 testSt345Pads.C:245
 testSt345Pads.C:246
 testSt345Pads.C:247
 testSt345Pads.C:248
 testSt345Pads.C:249
 testSt345Pads.C:250
 testSt345Pads.C:251
 testSt345Pads.C:252
 testSt345Pads.C:253
 testSt345Pads.C:254
 testSt345Pads.C:255
 testSt345Pads.C:256
 testSt345Pads.C:257
 testSt345Pads.C:258
 testSt345Pads.C:259
 testSt345Pads.C:260
 testSt345Pads.C:261
 testSt345Pads.C:262
 testSt345Pads.C:263
 testSt345Pads.C:264
 testSt345Pads.C:265
 testSt345Pads.C:266
 testSt345Pads.C:267
 testSt345Pads.C:268
 testSt345Pads.C:269
 testSt345Pads.C:270
 testSt345Pads.C:271
 testSt345Pads.C:272
 testSt345Pads.C:273
 testSt345Pads.C:274
 testSt345Pads.C:275
 testSt345Pads.C:276
 testSt345Pads.C:277
 testSt345Pads.C:278
 testSt345Pads.C:279
 testSt345Pads.C:280
 testSt345Pads.C:281
 testSt345Pads.C:282
 testSt345Pads.C:283
 testSt345Pads.C:284
 testSt345Pads.C:285
 testSt345Pads.C:286
 testSt345Pads.C:287
 testSt345Pads.C:288
 testSt345Pads.C:289
 testSt345Pads.C:290
 testSt345Pads.C:291
 testSt345Pads.C:292
 testSt345Pads.C:293
 testSt345Pads.C:294
 testSt345Pads.C:295