]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSpackageSSD.cxx
Introducing Riostream.h
[u/mrichter/AliRoot.git] / ITS / AliITSpackageSSD.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 /*
17 $Log$
18 Revision 1.5  2001/11/19 16:17:03  nilsen
19 Applyed fixes to bugs found by Rene Brun. With many thanks. Some additonal
20 bugs found by Rene require more work to fix. Will be fixed soon.
21
22 Revision 1.4  2000/07/12 05:32:20  fca
23 Correcting several syntax problem with static members
24
25 */
26
27 #include <Riostream.h>
28 #include <TClonesArray.h>
29 #include "AliITSpackageSSD.h"
30
31 //************************************************
32 //Piotr Krzysztof Skowronski
33 //Warsaw University of Technology
34 //skowron@if.pw.edu.pl
35 //
36
37 const Bool_t AliITSpackageSSD::fgkSIDEP=kTRUE;
38 const Bool_t AliITSpackageSSD::fgkSIDEN=kFALSE;
39
40 static const Int_t debug=0;
41
42 ClassImp(AliITSpackageSSD)
43
44 AliITSpackageSSD::AliITSpackageSSD()
45 {
46   fNclustersN=0;
47   fClusterNIndexes = 0; 
48                 
49   fNclustersP=0;
50   fClusterPIndexes = 0;
51   if (debug) cout<<"Default Ctor was used\n>>>>>>>>>>>>>><<<<<<<<<<<<<";
52 }
53
54
55 /*******************************************************/
56
57 AliITSpackageSSD::AliITSpackageSSD
58   (TClonesArray *clustersP, TClonesArray *clustersN)
59 {
60   fClustersP=clustersP;
61   fClustersN=clustersN;
62         
63   fNclustersN=0;
64   fClusterNIndexes = new TArrayI(300); 
65         
66   fNclustersP=0;
67   fClusterPIndexes = new TArrayI(300);          
68 }
69
70 /*******************************************************/
71
72
73 AliITSpackageSSD::AliITSpackageSSD
74   ( Int_t len, TClonesArray *clustersP, TClonesArray *clustersN)
75
76 {       
77   fClustersP=clustersP;
78   fClustersN=clustersN;
79
80   fNclustersN=0;
81   fClusterNIndexes = new TArrayI(len); 
82         
83   fNclustersP=0;
84   fClusterPIndexes = new TArrayI(len);          
85 }
86
87
88 /*******************************************************/
89
90 AliITSpackageSSD::~AliITSpackageSSD()
91 {
92   // destructor
93   delete fClusterNIndexes;
94   delete fClusterPIndexes;              
95 }
96 /*******************************************************/
97
98 AliITSpackageSSD::AliITSpackageSSD(const AliITSpackageSSD &package)
99 {
100   // copy constractor
101   Int_t i;  //iterator
102  
103   if (this == &package) return;
104   fClustersN = package.fClustersN;
105   fClustersP = package.fClustersP;
106   
107   fNclustersN= package.fNclustersN;
108   fNclustersP= package.fNclustersP;
109   
110   for ( i =0; i<fNclustersN;i++)
111     {
112       fClusterNIndexes[i]= package.fClusterNIndexes[i]; 
113     }
114   
115   for ( i =0; i<fNclustersP;i++)
116     {
117       fClusterPIndexes[i]= package.fClusterPIndexes[i]; 
118     }
119   
120   if (debug) cout << "Copying function was used\n<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>";
121   
122   return; 
123   
124 }
125 /*******************************************************/
126
127 AliITSpackageSSD&  
128 AliITSpackageSSD::operator=( const AliITSpackageSSD & package)
129 {
130
131 Int_t i;  //iterator
132  
133 if (this == &package) return *this;
134 fClustersN = package.fClustersN;
135 fClustersP = package.fClustersP;
136
137 fNclustersN= package.fNclustersN;
138 fNclustersP= package.fNclustersP;
139
140 for ( i =0; i<fNclustersN;i++)
141   {
142     fClusterNIndexes[i]= package.fClusterNIndexes[i]; 
143   }
144
145 for ( i =0; i<fNclustersP;i++)
146   {
147     fClusterPIndexes[i]= package.fClusterPIndexes[i]; 
148   }
149
150 if (debug) cout << "Copying function was used\n<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>";
151
152 return *this; 
153   
154 }
155
156
157 /*******************************************************/
158
159 Int_t 
160 AliITSpackageSSD::GetNSideClusterIdx(Int_t index)
161
162 {
163   if ((index>-1)&&(index<fNclustersN))
164     return (*fClusterNIndexes)[index];
165   else 
166    {
167     cout << "AliITSpackageSSD::GetNSideClusterIdx  : Out of Range\n";
168     return -1;
169    }
170 }
171 /*******************************************************/
172
173
174 Int_t AliITSpackageSSD::GetPSideClusterIdx(Int_t index)
175 {
176   if ((index>-1)&&(index<fNclustersP))
177     return (*fClusterPIndexes)[index];
178   else 
179   {
180   cout << "AliITSpackageSSD::GetPSideClusterIdx  : Out of Range\n";
181    return -1;
182   } 
183 }
184 /*******************************************************/
185 AliITSclusterSSD*  
186 AliITSpackageSSD::GetPSideCluster(Int_t index)
187 {
188
189 return (AliITSclusterSSD*)((*fClustersP)[GetPSideClusterIdx(index)]);
190 }
191
192 /*******************************************************/
193
194 AliITSclusterSSD*  
195 AliITSpackageSSD::GetNSideCluster(Int_t index)
196 {
197 return (AliITSclusterSSD*)((*fClustersN)[GetNSideClusterIdx(index)]);
198 }
199
200
201 /*******************************************************/
202
203
204
205 Bool_t 
206 AliITSpackageSSD::GetClusterWithOneCross
207   (Int_t & index, Bool_t& side)
208 {
209   
210  
211  if((fNclustersP==0)||(fNclustersN==0) )
212   {
213     printf("Empty package ((fNclustersP==0)||(fNclustersN==0))\n");
214     index = -2;
215     return kFALSE;
216   } 
217  Int_t ind;
218
219  ind =(*fClusterPIndexes)[fNclustersP-1]; 
220  if (  ( ((AliITSclusterSSD*)(*fClustersP)[ind]  )->GetCrossNo()  ) ==1 )
221   {
222     //index=ind;
223     index =fNclustersP-1; 
224     side=fgkSIDEP;
225     return kTRUE;
226   }
227  
228  ind =(*fClusterNIndexes)[fNclustersN-1]; 
229  if (  (  ((AliITSclusterSSD*)(*fClustersN)[ind]  )->GetCrossNo() ) ==1  )
230   {
231     //index=ind;
232     index = fNclustersN-1;
233     side=fgkSIDEN;
234     return kTRUE;
235   }
236   
237
238  ind =(*fClusterPIndexes)[0];
239  if (  ( ((AliITSclusterSSD*)(*fClustersP)[ind]  )->GetCrossNo() ) ==1 )
240   {
241     //index=ind;
242     index = 0;
243     side=fgkSIDEP;
244     return kTRUE;
245   }
246   
247
248  ind =(*fClusterNIndexes)[0];
249  if (  ( ((AliITSclusterSSD*)(*fClustersN)[ind]  )->GetCrossNo()  ) ==1  )
250   {
251 //    index=ind;
252     index = 0;  
253     side=fgkSIDEN;
254     return kTRUE;
255   }
256   
257   
258  //Add for to be shure 
259  index = -1;
260  return kFALSE;
261   
262 }
263 /*******************************************************/
264
265 void AliITSpackageSSD::DelCluster(Int_t index, Bool_t side)
266 {
267   if(side==fgkSIDEP) DelPCluster(index); else DelNCluster(index);
268 }
269 /*******************************************************/
270 void AliITSpackageSSD::DelPCluster(Int_t index)
271 {
272
273 //it not deletes delete given cluster physically, 
274 //but only complytely erase it from package
275 //all clusters are deleted automatically when TClonesArray is deleted
276
277   Int_t i;
278   Int_t idx;
279   Int_t clToDelIdx = GetPSideClusterIdx(index); //Index of cluster in TClonesArray 
280   AliITSclusterSSD *clToDel = GetPSideCluster(index); //cluster to be deleted
281   Int_t ncr = clToDel->GetCrossNo();
282   
283   for (i =0;i<ncr;i++)
284    {
285     idx = clToDel->GetCross(i);
286     ((AliITSclusterSSD *)((*fClustersN)[idx])   )->DelCross(clToDelIdx);
287    }
288  
289  
290  for (i=index;i<fNclustersP-1;i++)
291   {
292        (*fClusterPIndexes)[i]=(*fClusterPIndexes)[i+1];
293   }
294  fNclustersP--; 
295  if (debug) cout<<"Cluster P ("<<index<<") deleted\n";
296
297
298  for (i=0;i<fNclustersN;i++)
299   {
300     if ( (GetNSideCluster(i)->GetCrossNo())==0) DelNCluster(i);
301   }
302 }
303
304
305
306 /*******************************************************/
307 void AliITSpackageSSD::DelNCluster(Int_t index)
308 {
309
310 //it not deletes delete given cluster physically, 
311 //but only complytely erase it from package
312 //all clusters are deleted automatically when TClonesArray is deleted
313
314   Int_t i;
315   Int_t idx;
316   Int_t clToDelIdx = GetNSideClusterIdx(index); //Index of cluster in TClonesArray 
317   AliITSclusterSSD *clToDel = GetNSideCluster(index); //cluster to be deleted
318   Int_t ncr = clToDel->GetCrossNo();
319   
320   for (i =0;i<ncr;i++)
321    {
322     idx = clToDel->GetCross(i);
323     ((AliITSclusterSSD *)((*fClustersP)[idx])   )->DelCross(clToDelIdx);
324    }
325  
326  
327  for (i=index;i<fNclustersN-1;i++)
328   {
329        (*fClusterNIndexes)[i]=(*fClusterNIndexes)[i+1];
330   }
331  fNclustersN--; 
332  if (debug) cout<<"Cluster N ("<<index<<") deleted\n";
333
334  for (i=0;i<fNclustersP;i++)
335   {
336     if ( (GetPSideCluster(i)->GetCrossNo())==0) DelPCluster(i);
337   }
338
339 }
340
341
342 /*******************************************************/
343
344 void AliITSpackageSSD::DelPClusterOI(Int_t index)
345 {
346 //This function looks like this, 
347 //because probably cut cluster is 
348 //on the beginning or on the end of package 
349  Int_t i;
350  if( ((*fClusterPIndexes)[0]) == index) 
351   {
352     DelPCluster(0);
353     return;
354   }
355  else
356   { 
357    if( ((*fClusterPIndexes)[fNclustersP-1]) ==index)
358     {
359       DelPCluster(fNclustersP-1);
360       return;
361     }
362    else
363     {
364      for (i=1;i<fNclustersP-1;i++)
365        {
366          if( ((*fClusterPIndexes)[i])==index)
367           {
368             DelPCluster(i);
369             return;
370           }
371        }
372     }
373   }
374  
375  cout<<"AliITSpackageSSD - DelPClusterOI: index "<<index<<" not found\n";
376
377 }
378
379
380 /*******************************************************/
381
382 void AliITSpackageSSD::DelNClusterOI(Int_t index)
383 {
384 //This function looks like this, 
385 //because probably cluster to cut is 
386 //on the beginning or on the end of package 
387
388  Int_t i;
389  if( ((*fClusterNIndexes)[0])==index) 
390   {
391     DelNCluster(0);
392     return;
393   }
394  else
395   { 
396    if( ((*fClusterNIndexes)[fNclustersN-1])==index)
397     {
398       DelNCluster(fNclustersN-1);
399       return;
400     }
401    else
402     {
403      for (i=1;i<fNclustersN-1;i++)
404        {
405          if( ((*fClusterNIndexes)[i])==index)
406          {
407           DelNCluster(i);
408           return;
409          }
410        }
411     }
412   }
413  cout<<"AliITSpackageSSD - DelNClusterOI: index "<<index<<" not found\n";
414 }
415
416
417 /*******************************************************/
418
419
420 void AliITSpackageSSD::DelClusterOI(Int_t index, Bool_t side)
421 {
422  if (side == fgkSIDEP)
423   {    
424     DelPClusterOI(index);
425   } 
426   else
427   {
428     DelNClusterOI(index);
429   }
430
431 }
432
433
434 /**********************************************/
435
436
437 void  AliITSpackageSSD::GetAllCombinations(Int_t**array,Int_t &num,Int_t sizet)
438 {
439   
440   Int_t *takenNcl = new Int_t[fNclustersN];
441   
442   num=0;
443   
444   if (debug) PrintClusters();
445
446   for (Int_t i=0;i<fNclustersP;i++)
447    {
448      takenNcl[i]=-1;
449    }
450   //see comment on the beginning of MakeCombin
451   if (debug) cout<<"GetAllCombinations entered";
452   MakeCombin (array,num,0,takenNcl,sizet);
453
454   delete []takenNcl; 
455 }
456 /**********************************************/
457
458
459 void  AliITSpackageSSD::MakeCombin
460    (Int_t**arr,Int_t& nu, Int_t np, Int_t *occup, Int_t sizet)
461
462 {
463
464 //ATTENTION: anybody watching this function
465 //AliITSclusterSSD::GetCrossNo() returns index of cluster in main array belonging to AliITSmodulesSSD
466 //however, we have pointer to that array (TClonesArray)
467 //we can not use 
468 //Get?SideCluster because it takes index from local look_up_table
469  
470  Int_t i,j;
471  
472  //this cluster
473  AliITSclusterSSD *cl=GetPSideCluster(np);
474
475  Int_t NC = cl->GetCrossNo();  //number of crosses for this cluster
476  Int_t indcro;                 //index of given cluster on side N that 
477                                // this cluster crosses with
478    
479  if (np == fNclustersP-1) {
480    for (i=0;i<NC;i++) {
481      indcro=cl->GetCross(i);
482      if(IsFree(indcro,np,occup)) {
483         occup[np]=indcro;
484         for(j=0;j<fNclustersP;j++)  
485         {
486            if (nu<sizet) arr[nu][j]=occup[j];
487            else {
488                continue;}
489         }
490       
491         occup[np]=-1;
492         if (nu<sizet-1) nu++;
493      }
494    }
495   } else {
496     for (i=0;i<NC;i++) {
497        indcro=cl->GetCross(i);
498        if(IsFree(indcro,np,occup)) {
499           occup[np]=indcro;
500           if (nu<sizet) MakeCombin(arr,nu,(np+1),occup,sizet);
501           //else printf("MakeComb - exceeding array size!\n");
502        }
503     }
504     occup[np]=-1;
505   } 
506
507 }
508
509 /**********************************************/
510 Bool_t  AliITSpackageSSD::IsFree(Int_t idx, Int_t nn, Int_t *lis)
511 {
512
513   for (Int_t i=0;i<nn;i++)
514     {
515       if (lis[i]==idx) return kFALSE;
516     }
517   return kTRUE;
518 }
519
520 /**********************************************/
521 void AliITSpackageSSD::PrintClusters()
522 {
523
524 Int_t i,j;
525 cout<<"SIDE P\n";
526 for (i=0;i<fNclustersP;i++)
527  {
528    cout<<i<<".  IO="<<GetPSideClusterIdx(i)<<" NC="<<GetPSideCluster(i)->GetCrossNo()<<"     C. IDXs : ";
529    for (j=0;j<GetPSideCluster(i)->GetCrossNo();j++)
530     {
531       cout<<GetPSideCluster(i)->GetCross(j)<<" ";
532     }
533  //  if (GetPSideCluster(i)->GetSide()) cout<<" P\n";
534  //  else cout<<"BAD SIDE ==N\n";
535     cout<<"\n";
536    
537  }
538
539 cout <<"SIDE N\n";
540 for (i=0;i<fNclustersN;i++)
541  {
542    cout<<i<<".  IO="<<GetNSideClusterIdx(i)<<" NC="<<GetNSideCluster(i)->GetCrossNo()<<"     C. IDXs : ";
543    for (j=0;j<GetNSideCluster(i)->GetCrossNo();j++)
544     {
545       cout<<GetNSideCluster(i)->GetCross(j)<<" ";
546     }
547  //  if (GetNSideCluster(i)->GetSide()) cout<<" N\n";
548  //  else cout<<"BAD SIDE ==P\n";
549     cout<<"\n";   
550  }
551  
552  
553  
554 }
555 /**********************************************/
556 void AliITSpackageSSD::ConsumeClusters()
557 {
558 register Int_t i;
559
560 for(i=0;i<fNclustersP;i++)
561  {
562    GetPSideCluster(i)->Consume();
563  }
564
565 for(i=0;i<fNclustersN;i++)
566  {
567    GetNSideCluster(i)->Consume();
568  }
569
570 }
571
572 /**********************************************/
573
574 Int_t AliITSpackageSSD::GetNextPIdx(Int_t OI)
575 {
576  //Returns index of next P cluster OI in package; OI == Original Inedx (in TClonesArray)
577  //if not egsist return -1;
578  for (Int_t i =0; i<fNclustersP-1;i++)
579   {
580     if(GetPSideClusterIdx(i) == OI)
581        return GetPSideClusterIdx(i+1);
582   }
583  return -1;
584 }
585 /**********************************************/
586 Int_t AliITSpackageSSD::GetPrvPIdx(Int_t OI)
587 {
588  //Returns index of previous P cluster  OI in package; OI == Original Inedx (in TClonesArray)
589  //if not egsist return -1;
590  
591  for (Int_t i =1; i<fNclustersP;i++)
592   {
593     if(GetPSideClusterIdx(i) == OI)
594        return GetPSideClusterIdx(i-1);
595   }
596   return -1;
597 }
598 /**********************************************/
599 Int_t AliITSpackageSSD::GetNextNIdx(Int_t OI)
600 {
601 //Returns index of next N cluster OI in package; OI == Original Inedx (in TClonesArray)
602  //if not egsist return -1;
603  for (Int_t i =0; i<fNclustersN-1;i++)
604   {
605     if(GetNSideClusterIdx(i) == OI)
606        return GetNSideClusterIdx(i+1);
607   }
608  return -1;
609
610 }
611 /**********************************************/
612 Int_t  AliITSpackageSSD::GetPrvNIdx(Int_t OI)
613 {
614  //Returns index of previous N cluster OI in package; OI == Original Inedx (in TClonesArray)
615  //if not egsist return -1;
616  
617  for (Int_t i =1; i<fNclustersN;i++)
618   {
619     if(GetNSideClusterIdx(i) == OI)
620        return GetNSideClusterIdx(i-1);
621   }
622   return -1;
623  
624 }
625
626 void  AliITSpackageSSD::SplitPackage(Int_t pi, Int_t ni, AliITSpackageSSD* pkg)
627 {
628   // split package of clusters
629   Int_t p=-1, n=-1;
630   Int_t i;
631   for (i=0;i<fNclustersN;i++)
632    {
633      if((*fClusterNIndexes)[i]==ni) 
634       {
635         n = i;
636         break;
637       }
638     } 
639    
640   for (i=0;i<fNclustersP;i++)
641    {
642      if((*fClusterPIndexes)[i]==pi) 
643       {
644         p = i;
645         break;
646       }
647     }  
648   if (debug) cout<<" p = "<<p<<"  n = "<<n;
649   if ((p==-1)||(n==-1)) return;
650   
651   for (i=p;i<fNclustersP;i++)
652    {
653      pkg->AddPSideCluster(GetPSideClusterIdx(i));       
654    }
655   fNclustersP = p;
656   
657   for (i=n;i<fNclustersN;i++)
658    {
659      pkg->AddNSideCluster(GetNSideClusterIdx(i));       
660    }
661   fNclustersN = n;
662  
663   cout<<"  After split: fNclustersP = "<< fNclustersP<< "fNclustersN = "<< fNclustersN<<"\n";
664 }