]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSDATreeCluster.cxx
Coverity 18637
[u/mrichter/AliRoot.git] / PHOS / AliPHOSDATreeCluster.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 /* $Id$ */
16
17 // --
18 // --
19 // Implementation for TTree output in PHOS DA
20 // for calibrating energy by pi0 and MIP.
21 // --
22 // -- Author: Hisayuki Torii (Hiroshima Univ.)
23 // --
24
25 #include <math.h>
26 #include <Rtypes.h>
27 #include <iostream>
28 #include "AliPHOSDATreeDigit.h"
29 #include "AliPHOSDATreeCluster.h"
30 ClassImp(AliPHOSDATreeCluster)
31 //------------------------------------------------------------------------
32 AliPHOSDATreeCluster::AliPHOSDATreeCluster(const AliPHOSDATreeCluster& cluster):
33 //fEnergy(cluster.fEnergy),fX(cluster.fX),fY(cluster.fY),fZ(cluster.fZ),fNDigits(cluster.fNDigits){
34 fEnergy(cluster.fEnergy),fRow(cluster.fRow),fCol(cluster.fCol),fNDigits(cluster.fNDigits),fDigits(0){
35   // Copy Constructor
36
37   if( fNDigits > 0 ){
38     fDigits = new AliPHOSDATreeDigit[fNDigits];
39     int ndigits = fNDigits;
40     while( ndigits-- ){
41       fDigits[ndigits] = cluster.fDigits[ndigits];
42     }
43   } else {
44     fDigits = 0;
45   }
46 }
47 //------------------------------------------------------------------------
48 AliPHOSDATreeCluster& AliPHOSDATreeCluster::operator=(const AliPHOSDATreeCluster& cluster){
49   // Copy Operator
50
51   if (this != &cluster) {
52     if( fNDigits> 0 ) delete[] fDigits;
53     fEnergy = cluster.fEnergy;
54     fNDigits = cluster.fNDigits;
55     fRow = cluster.fRow;
56     fCol = cluster.fCol;
57     //fX = cluster.fX;
58     //fY = cluster.fY;
59     //fZ = cluster.fZ;
60     if( fNDigits > 0 ){
61       fDigits = new AliPHOSDATreeDigit[fNDigits];
62       int ndigits = fNDigits;
63       while( ndigits-- ){
64         fDigits[ndigits] = cluster.fDigits[ndigits];
65       }
66     } else {
67       fDigits = 0;
68     }
69   }
70   return *this;
71 }
72 //------------------------------------------------------------------------
73 void AliPHOSDATreeCluster::Print(Option_t *opt) const
74 {
75   // Print out
76   std::cout<<" AliPHOSDATreeCluster:: Energy="<<fEnergy<<" NDigits="<<fNDigits
77            <<" (row,col)=("<<fRow<<","<<fCol<<")"<<std::endl;
78     //<<" (x,y,z)=("<<fX<<","<<fY<<","<<fZ<<")"<<std::endl;
79   int ndigits = fNDigits;
80   while( ndigits-- ){
81     std::cout<<"   -->["<<ndigits<<"] : ";
82     fDigits[ndigits].Print(opt);
83   }
84 }
85 //------------------------------------------------------------------------
86 std::ostream& operator<<(std::ostream& out, const AliPHOSDATreeCluster& cluster){
87   // Print out
88   out<<" AliPHOSDATreeCluster:: Energy="<<cluster.fEnergy<<" NDigits="<<cluster.fNDigits
89      <<" (row,col)=("<<cluster.fRow<<","<<cluster.fCol<<")"<<std::endl;
90   int ndigits = cluster.fNDigits;
91   while( ndigits-- ){
92     out<<"   -->["<<ndigits<<"] : "<<cluster.fDigits[ndigits];
93     if( ndigits!=0 ) out<<std::endl;
94   }
95   return out;
96 }
97 //------------------------------------------------------------------------
98 void AliPHOSDATreeCluster::Reset(){
99   // Reset information
100
101   fEnergy = 0;
102   fRow = -100;
103   fCol = -100;
104   //fX = 0;
105   //fY = 0;
106   //fZ = 0;
107   if( fNDigits> 0 ) delete[] fDigits;
108   fNDigits = 0;
109 }
110 //------------------------------------------------------------------------
111 bool AliPHOSDATreeCluster::Append(AliPHOSDATreeDigit& digit){
112   // Add digit information and sum all energy
113   //
114   if(! digit.IsValid() ){
115     std::cout<<" AliPHOSDATreeCluster::Append():: Error!! Digit is not valid.."<<std::endl;
116     return false;
117   }
118   AliPHOSDATreeDigit* newfDigits = new AliPHOSDATreeDigit[fNDigits+1];
119   bool bsearching = true;
120   int ndigit = fNDigits;
121   while( ndigit-- ){
122     if( fDigits[ndigit].GetAbsId() == digit.GetAbsId() ){
123       std::cout<<" AliPHOSDATreeCluster::Append():: Error!! The channel already exist."<<std::endl;
124       std::cout<<" Add "<<digit<<std::endl;
125       std::cout<<" into *this"<<*this<<std::endl;
126       delete[] newfDigits;
127       return false;
128     }
129     if( fDigits[ndigit].GetEnergy() < digit.GetEnergy() ){
130       newfDigits[ndigit+1] = fDigits[ndigit];
131     } else {
132       if( bsearching ) {
133         bsearching = false;
134         newfDigits[ndigit+1] = digit;
135       }
136       newfDigits[ndigit] = fDigits[ndigit];
137     }
138   }
139   if( bsearching ) newfDigits[0] = digit;
140   if( fNDigits>0 ) delete[] fDigits;
141   fNDigits++;
142   fDigits = newfDigits;
143   fEnergy += digit.GetEnergy();
144   return true;
145 }
146 //------------------------------------------------------------------------
147 bool AliPHOSDATreeCluster::Append(AliPHOSDATreeCluster& cluster){
148   // Add another cluster information and sum all energy
149   //
150   AliPHOSDATreeDigit* newfDigits = new AliPHOSDATreeDigit[fNDigits+cluster.fNDigits];
151   int ndigits1 = fNDigits;
152   int ndigits2 = cluster.fNDigits;
153   int ndigitsall = ndigits1 + ndigits2;
154   while( ndigitsall-- ){
155     //std::cout<<" ------ ndigits1:"<<ndigits1<<" ndigits2:"<<ndigits2<<std::endl;
156     if( ndigits1 && ndigits2 ){
157       if( fDigits[ndigits1-1].GetEnergy() < cluster.fDigits[ndigits2-1].GetEnergy() ){
158         newfDigits[ndigitsall] = fDigits[--ndigits1];
159       } else {
160         newfDigits[ndigitsall]= cluster.fDigits[--ndigits2];
161       }
162     } else if ( ndigits1 && ndigits2==0 ){
163       newfDigits[ndigitsall] = fDigits[--ndigits1];
164     } else if ( ndigits2 && ndigits1==0 ){
165       newfDigits[ndigitsall]= cluster.fDigits[--ndigits2];
166     } else {
167       std::cout<<" AliPHOSDATreeCluster::Append() Something wrong.. "<<std::endl;
168       return false;
169     }
170   }
171   if(fNDigits>0) delete[] fDigits;
172   fDigits = newfDigits;
173   fNDigits += cluster.fNDigits;
174   fEnergy += cluster.GetEnergy();
175   return true;
176     
177 }
178 //------------------------------------------------------------------------
179 bool AliPHOSDATreeCluster::IsNeighbor(const AliPHOSDATreeDigit& digit) const{
180   // Check wether the given digit is neighboring to this cluster.
181   // Return true if yes.
182
183   bool status = false;
184   int ndigits = fNDigits;
185   while( ndigits-- && !status ){
186     status = digit.IsNeighbor(fDigits[ndigits]);
187   }
188   return status;
189 }
190 //------------------------------------------------------------------------
191 bool AliPHOSDATreeCluster::IsNeighbor(const AliPHOSDATreeCluster& cluster) const{
192   // Check wether the given cluster is neighboring to this cluster.
193   // Return true if yes.
194
195   bool status = false;
196   int ndigits = fNDigits;
197   while( ndigits-- && !status ){
198     status = cluster.IsNeighbor(fDigits[ndigits]);
199   }
200   return status;
201 }
202 //------------------------------------------------------------------------
203 bool AliPHOSDATreeCluster::CalculateProperty(){
204   // Calculate the hit position
205   // (calculation of dispersion is not valid)
206   
207   fCol = 0;
208   fRow = 0;
209   float totweight = 0;
210   float weight;
211   int ndigits = fNDigits;
212   while( ndigits-- ){
213     weight = log(fDigits[ndigits].GetEnergy()/fEnergy) + 4.5; //4.5 is for PHOS
214     //std::cout<<" AliPHOSDATreeCluster::CalculateProperty() DEBUG: ndigits="<<ndigits<<" weight="<<weight<<std::endl;
215     if( weight > 0 ){
216       totweight += weight;
217       fRow += fDigits[ndigits].GetRow() * weight;
218       fCol += fDigits[ndigits].GetCol() * weight;
219     }
220   }
221   //std::cout<<" AliPHOSDATreeCluster::CalculateProperty() DEBUG: totweight="<<totweight<<std::endl;
222   if( totweight > 0 ){
223     fRow /= totweight;
224     fCol /= totweight;
225   } else {
226     fRow = 0;
227     fCol = 0;
228   }
229   /*
230   float disp = 0;
231   if( totweight > ){
232     ndigits = fNDigits;
233     while( ndigits-- ){
234       weight = log(fDigits[ndigits].GetEnergy()/fEnergy) + 4.5; //4.5 is for PHOS
235       disp += weight * ( (fDigits[ndigits].GetRow()-fRow)*(fDigits[ndigits].GetRow()-fRow) +
236                          (fDigits[ndigits].GetCol()-fCol)*(fDigits[ndigits].GetCol()-fCol) );
237     }
238     disp /= totweight;
239   }
240   */
241   return true;
242 }
243 //------------------------------------------------------------------------