StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMCTruth.cxx
1 /***************************************************************************
2  *
3  * $Id: StMCTruth.cxx,v 1.5 2016/07/25 17:33:49 jwebb Exp $
4  *
5  * Author: Victor Perev, Jun 2005
6  ***************************************************************************
7  *
8  * Description:
9  *
10  ***************************************************************************
11  *
12  * $Log: StMCTruth.cxx,v $
13  * Revision 1.5 2016/07/25 17:33:49 jwebb
14  * Init members in ctor / coverity
15  *
16  * Revision 1.4 2016/07/25 17:18:57 jwebb
17  * Comment out deadcode / coverity
18  *
19  * Revision 1.3 2009/12/17 08:37:26 fisyak
20  * account signature change snce root 5.24
21  *
22  * Revision 1.2 2009/08/28 16:38:26 fine
23  * fix the compilation issues under SL5_64_bits gcc 4.3.2
24  *
25  * Revision 1.1 2005/07/19 22:39:58 perev
26  * IdTruth classes
27  *
28  *
29  **************************************************************************/
30 #include "assert.h"
31 #include "StMCTruth.h"
32 #include "TMath.h"
33 #include "TExMap.h"
34 #include <cassert>
35 //__________________________________________________________________________________________________
36 StMCTruth &StMCTruth::operator=(int word)
37 {
38  trackId = word&((1<<16)-1);
39  trackWt = word>>16;
40  return *this;
41 }
42 //__________________________________________________________________________________________________
43 StMCTruth::operator int() const
44 {
45  return trackId | (trackWt<<16);
46 }
47 //__________________________________________________________________________________________________
48 StMCPivotTruth::StMCPivotTruth(int normInput) :
49  fN(0), fNorm(normInput),
50  mTrackIds{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
51  mTrackWts{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
52  mTrackNum{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
53  qwe(0)
54 {
55  fNorm = normInput;
56  Reset();
57 }
58 
59 //__________________________________________________________________________________________________
60 void StMCPivotTruth::Add(int trackId, double wt)
61 {
62 
63 // assert(trackId);
64  wt = fabs(wt);
65 // assert(wt);
66  for (int i=0;i<fN;i++) {
67  if (mTrackIds[i]!=trackId) continue;
68  mTrackWts[i]+=wt;
69  mTrackNum[i]+=1;
70  return;
71  }
72  if (fN>=HOWMANY) return;
73  mTrackIds[fN] = trackId;
74  mTrackWts[fN] = (float)wt;
75  mTrackNum[fN] = 1;
76  fN++;
77 
78 }
79 //__________________________________________________________________________________________________
80 void StMCPivotTruth::Add(StMCTruth truth)
81 {
82  fNorm=1;
83  Add(truth.trackId,truth.trackWt);
84 }
85 //__________________________________________________________________________________________________
86 void StMCPivotTruth::Add(StMCTruth truth, double wt)
87 {
88  fNorm=0;
89  Add(truth.trackId,truth.trackWt*wt);
90 }
91 //__________________________________________________________________________________________________
92 StMCTruth StMCPivotTruth::Get(int byCount) const
93 {
94  int trackId=0,zeroId=byCount>>1;
95  double wt=0,sum=0,sun=0,sum0=0,sun0=0;
96  const float *wts = (byCount&1)? mTrackNum:mTrackWts;
97  for (int i=0;i<fN;i++) {
98  if (mTrackIds[i]==0) { sun0=mTrackNum[i];sum0=wts[i];continue;}
99  sun += mTrackNum[i];
100  sum += wts[i];
101  if (wts[i]<wt) continue;
102  trackId = mTrackIds[i];
103  wt = wts[i];
104  }
105  if (zeroId) { sum+=sum0; wt+=sum0; sun+=sun0;}
106  sum = ((byCount&1) || fNorm==0)? sum/100 : sun;
107  wt /=(sum+1e-20);
108  return StMCTruth(trackId,int(wt+0.5));
109 }
110 
111 //__________________________________________________________________________________________________
112 StMCPivotTruthMap::StMCPivotTruthMap(int normInput)
113 {
114  fNorm = normInput;
115  fMap = new TExMap;
116  fIter=0;
117 }
118 //__________________________________________________________________________________________________
119 StMCPivotTruthMap::~StMCPivotTruthMap()
120 {
121  LongKey_t key,val;
122  TExMapIter it(fMap);
123  while (it.Next(key,val)) { delete (StMCPivotTruth*)val; }
124  delete fMap; fMap=0;
125  delete fIter; fIter=0;
126 }
127 //__________________________________________________________________________________________________
128 void StMCPivotTruthMap::Add(LongKey_t token, int trackId, double wt)
129 {
130  LongKey_t& word = (*fMap)(TMath::Hash(&token,sizeof(token)),token);
131  StMCPivotTruth *&pivo = (StMCPivotTruth *&)word;
132  if (!pivo) pivo = new StMCPivotTruth(fNorm);
133  pivo->Add(trackId,wt);
134 }
135 //__________________________________________________________________________________________________
136 void StMCPivotTruthMap::Add(LongKey_t token, StMCTruth truth)
137 {
138  Add(token,truth.trackId,truth.trackWt);
139 }
140 //__________________________________________________________________________________________________
141 StMCTruth StMCPivotTruthMap::Get(LongKey_t token,int byCount) const
142 {
143  LongKey_t word = fMap->GetValue(TMath::Hash(&token,sizeof(token)),token);
144  assert(word);
145  //if (!word) return StMCTruth(0,0); // deadcode
146 
147  StMCPivotTruth *pivo = (StMCPivotTruth*)word;
148  return pivo->Get(byCount);
149 }
150 
151 //__________________________________________________________________________________________________
152 StMCTruth StMCPivotTruthMap::Iter(LongKey_t &token) const
153 {
154  LongKey_t val;
155  if (token == -1L) {
156  if (!fIter) fIter = new TExMapIter(fMap);
157  fIter->Reset();
158  }
159  if (!fIter->Next(token,val)) {token = -1; return 0;}
160  StMCPivotTruth *pivo = (StMCPivotTruth*)val;
161  return pivo->Get();
162 }
163