00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "assert.h"
00025 #include "StMCTruth.h"
00026 #include "TMath.h"
00027 #include "TExMap.h"
00028 #include <cassert>
00029
00030 StMCTruth &StMCTruth::operator=(int word)
00031 {
00032 trackId = word&((1<<16)-1);
00033 trackWt = word>>16;
00034 return *this;
00035 }
00036
00037 StMCTruth::operator int() const
00038 {
00039 return trackId | (trackWt<<16);
00040 }
00041
00042 StMCPivotTruth::StMCPivotTruth(int normInput)
00043 {
00044 fNorm = normInput;
00045 Reset();
00046 }
00047
00048
00049 void StMCPivotTruth::Add(int trackId, double wt)
00050 {
00051
00052
00053 wt = fabs(wt);
00054
00055 for (int i=0;i<fN;i++) {
00056 if (mTrackIds[i]!=trackId) continue;
00057 mTrackWts[i]+=wt;
00058 mTrackNum[i]+=1;
00059 return;
00060 }
00061 if (fN>=HOWMANY) return;
00062 mTrackIds[fN] = trackId;
00063 mTrackWts[fN] = (float)wt;
00064 mTrackNum[fN] = 1;
00065 fN++;
00066
00067 }
00068
00069 void StMCPivotTruth::Add(StMCTruth truth)
00070 {
00071 fNorm=1;
00072 Add(truth.trackId,truth.trackWt);
00073 }
00074
00075 void StMCPivotTruth::Add(StMCTruth truth, double wt)
00076 {
00077 fNorm=0;
00078 Add(truth.trackId,truth.trackWt*wt);
00079 }
00080
00081 StMCTruth StMCPivotTruth::Get(int byCount) const
00082 {
00083 int trackId=0,zeroId=byCount>>1;
00084 double wt=0,sum=0,sun=0,sum0=0,sun0=0;
00085 const float *wts = (byCount&1)? mTrackNum:mTrackWts;
00086 for (int i=0;i<fN;i++) {
00087 if (mTrackIds[i]==0) { sun0=mTrackNum[i];sum0=wts[i];continue;}
00088 sun += mTrackNum[i];
00089 sum += wts[i];
00090 if (wts[i]<wt) continue;
00091 trackId = mTrackIds[i];
00092 wt = wts[i];
00093 }
00094 if (zeroId) { sum+=sum0; wt+=sum0; sun+=sun0;}
00095 sum = ((byCount&1) || fNorm==0)? sum/100 : sun;
00096 wt /=(sum+1e-20);
00097 return StMCTruth(trackId,int(wt+0.5));
00098 }
00099
00100
00101 StMCPivotTruthMap::StMCPivotTruthMap(int normInput)
00102 {
00103 fNorm = normInput;
00104 fMap = new TExMap;
00105 fIter=0;
00106 }
00107
00108 StMCPivotTruthMap::~StMCPivotTruthMap()
00109 {
00110 LongKey_t key,val;
00111 TExMapIter it(fMap);
00112 while (it.Next(key,val)) { delete (StMCPivotTruth*)val; }
00113 delete fMap; fMap=0;
00114 delete fIter; fIter=0;
00115 }
00116
00117 void StMCPivotTruthMap::Add(LongKey_t token, int trackId, double wt)
00118 {
00119 LongKey_t& word = (*fMap)(TMath::Hash(&token,sizeof(token)),token);
00120 StMCPivotTruth *&pivo = (StMCPivotTruth *&)word;
00121 if (!pivo) pivo = new StMCPivotTruth(fNorm);
00122 pivo->Add(trackId,wt);
00123 }
00124
00125 void StMCPivotTruthMap::Add(LongKey_t token, StMCTruth truth)
00126 {
00127 Add(token,truth.trackId,truth.trackWt);
00128 }
00129
00130 StMCTruth StMCPivotTruthMap::Get(LongKey_t token,int byCount) const
00131 {
00132 LongKey_t word = fMap->GetValue(TMath::Hash(&token,sizeof(token)),token);
00133 assert(word);
00134 if (!word) return StMCTruth(0,0);
00135
00136 StMCPivotTruth *pivo = (StMCPivotTruth*)word;
00137 return pivo->Get(byCount);
00138 }
00139
00140
00141 StMCTruth StMCPivotTruthMap::Iter(LongKey_t &token) const
00142 {
00143 LongKey_t val;
00144 if (token == -1L) {
00145 if (!fIter) fIter = new TExMapIter(fMap);
00146 fIter->Reset();
00147 }
00148 if (!fIter->Next(token,val)) {token = -1; return 0;}
00149 StMCPivotTruth *pivo = (StMCPivotTruth*)val;
00150 return pivo->Get();
00151 }
00152