StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMuFcsUtil.cxx
1 #include "StEvent/StFcsCluster.h"
2 #include "StMuDSTMaker/COMMON/StMuFcsCluster.h"
3 #include "StMuDSTMaker/COMMON/StMuFcsHit.h"
4 #include "StMuDSTMaker/COMMON/StMuFcsPoint.h"
5 #include "StMuDSTMaker/COMMON/StMuFcsUtil.h"
6 #include "StMuDSTMaker/COMMON/StMuFcsInfo.h"
7 #include "StMuDSTMaker/COMMON/StMuFcsCollection.h"
8 #include "StMuDSTMaker/COMMON/StMuDst.h"
9 #include "StMuDSTMaker/COMMON/StMuEvent.h"
10 #include "StEvent/StEvent.h"
11 #include "St_base/StMessMgr.h"
12 #include "StEvent/StEventTypes.h"
13 #include "StEvent/StTriggerData.h"
14 
15 #include <algorithm> // For std::find
16 #include <iterator> // For std::distance
17 
18 #include "TCollection.h" // For TIter
19 #include "TRefArray.h"
20 #include "TVector3.h"
21 
22 ClassImp(StMuFcsUtil)
23 
25 {
26 }
27 StMuFcsUtil::~StMuFcsUtil()
28 {
29 }
30 
31 StMuFcsCollection* StMuFcsUtil::getMuFcs(StFcsCollection *fcscol)
32 {
33  LOG_DEBUG << "StMuFcsUtil::getMuFcs" << endm;
34  if(!fcscol) return NULL;
36  fillMuFcs(muFcs,fcscol);
37  return muFcs;
38 } // getMuFcs
39 
40 StFcsCollection* StMuFcsUtil::getFcs(StMuFcsCollection* muFcs)
41 {
42  if(!muFcs) return NULL;
43 
45  fillFcs(fcs,muFcs);
46  return fcs;
47 } //getFcs
48 
49 void StMuFcsUtil::fillMuFcs(StMuFcsCollection *muFcs,StFcsCollection *fcscol)
50 {
51  if(!fcscol) return;
52  if(!muFcs) return;
53 
54  fillMuFcsHits(muFcs, fcscol);
55  fillMuFcsPoints(muFcs, fcscol);
56  fillMuFcsClusters(muFcs, fcscol);
57  fillMuFcsInfo(muFcs, fcscol);
58 
59  // rebuild the pointers that give cluster <-> hit, cluster <-> cluster,
60  // and cluster <-> point relationships
61  rebuildRelationships( fcscol, muFcs );
62 } // fillMuFcs
63 
64 void StMuFcsUtil::fillFcs(StFcsCollection* fcscol,StMuFcsCollection* muFcs)
65 {
66  if(!muFcs) return;
67  if(!fcscol) return;
68  fillFcsHits(fcscol, muFcs);
69 } // fillFcs
70 
71 void StMuFcsUtil::fillMuFcsHits(StMuFcsCollection* muFcs,
72  StFcsCollection* fcscol) {
73 
74  for ( unsigned int idet = 0; idet < kFcsNDet+1; idet++ ){
75  const StSPtrVecFcsHit& vecHit = fcscol->hits(idet);
76  for(unsigned int i=0; i<fcscol->numberOfHits(idet); i++){
77 
78  StMuFcsHit* muFcsHit = muFcs->addHit();
79 
80 
81  unsigned int l = vecHit[i]->nTimeBin();
82  if ( vecHit[i]->zs() ) l *= 2; // size of data, x2 if zero suppressed
83  unsigned short data[ l ];
84  for (unsigned int j = 0; j < l; j++ ){
85  data[ j ] = vecHit[i]->data( j );
86  }
87 
88  muFcsHit->setFcsHit(
89  vecHit[i]->zs(), vecHit[i]->detectorId(), vecHit[i]->id(),
90  vecHit[i]->ns(), vecHit[i]->ehp(), vecHit[i]->dep(), vecHit[i]->channel(),
91  l, data
92  );
93 
94  // set the corrected energy too
95  muFcsHit->setAdcSum ( vecHit[i]->adcSum() );
96  muFcsHit->setFitPeak ( vecHit[i]->fitPeak() );
97  muFcsHit->setFitSigma( vecHit[i]->fitSigma());
98  muFcsHit->setFitChi2 ( vecHit[i]->fitChi2() );
99  muFcsHit->setNPeak ( vecHit[i]->nPeak() );
100  muFcsHit->setEnergy ( vecHit[i]->energy() );
101 
102  // store in memory map between StEvent and StMuDst version
103  mMapHits[ fcscol->hits(idet)[i] ] = muFcsHit;
104  } // for i
105  } // for idet
106 } // fillMuFcsHits
107 
108 void StMuFcsUtil::rebuildRelationships(StFcsCollection* fcscol,
109  StMuFcsCollection* muFcs
110  ) {
111 
112  // First take care of the hits
113  for (unsigned int idet = 0; idet < kFcsNDet+1; idet++){
114  for(unsigned int i=0; i<fcscol->numberOfHits(idet); i++){
115 
116  if ( mMapHits.count( fcscol->hits(idet)[i] ) > 0 ) {
117  StMuFcsHit * muHit = static_cast<StMuFcsHit*>(mMapHits[ fcscol->hits(idet)[i] ]);
118  if ( nullptr == fcscol->hits(idet)[i]->cluster() ) continue;
119  if ( mMapClusters.count( fcscol->hits(idet)[i]->cluster() ) == 0
120  || mMapClusters[ fcscol->hits(idet)[i]->cluster() ] == nullptr )
121  continue;
122 
123  StMuFcsCluster* muCluster = mMapClusters[ fcscol->hits(idet)[i]->cluster() ];
124  muHit->setCluster( muCluster );
125  } else {
126  // By construction the StFcs objectts should always exist in the map, so this is an error
127  LOG_ERROR << "Cannot find StMuFcsHit for StFcsHit=" << fcscol->hits(idet)[i] << endm;
128  } // if / else
129  } // for i
130  } // for idet
131 
132  // Take care of the clusters
133  for (unsigned int idet = 0; idet < kFcsNDet; idet++){
134  const StSPtrVecFcsCluster &vecClu = fcscol->clusters(idet);
135  for(unsigned int i=0; i<vecClu.size(); i++){
136 
137  assert( mMapClusters.count( vecClu[i] ) > 0 );
138 
139  StMuFcsCluster * clu = static_cast<StMuFcsCluster*>( mMapClusters[ vecClu[i]] );
140  StPtrVecFcsHit& vecHits = vecClu[i]->hits();
141  StPtrVecFcsCluster& vecNeighbors = vecClu[i]->neighbor();
142  StPtrVecFcsPoint& vecPoints = vecClu[i]->points();
143 
144  for (unsigned int j = 0; j < vecHits.size(); j++ ){
145  if ( vecHits[j] && mMapHits.count( vecHits[j] ) > 0 && mMapHits[vecHits[j]] != nullptr ) {
146  StMuFcsHit * muHit = static_cast<StMuFcsHit*>( mMapHits[ vecHits[j]] );
147  clu->hits()->Add( muHit );
148  } else {
149  LOG_ERROR << "Cannot find StMuFcsHit for StFcsHit=" << vecHits[j] << endm;
150  } // if / else
151  } // for j
152 
153  for (unsigned int j = 0; j < vecNeighbors.size(); j++ ){
154  if ( vecNeighbors[j] && mMapClusters.count( vecNeighbors[j] ) > 0 && mMapClusters[vecNeighbors[j]] != nullptr) {
155  StMuFcsCluster * muCluster = static_cast<StMuFcsCluster*>( mMapClusters[vecNeighbors[j]]);
156  clu->addNeighbor( muCluster );
157  } else {
158  LOG_ERROR << "Cannot find StMuFcsCluster for StFcsCluster=" << vecNeighbors[j] << endm;
159  } // if / else
160  } // for j
161 
162  for (unsigned int j = 0; j < vecPoints.size(); j++ ){
163  if ( vecPoints[j] && mMapPoints.count( vecPoints[j] ) > 0 && mMapPoints[vecPoints[j]] != nullptr ) {
164  StMuFcsPoint * muPoint = static_cast<StMuFcsPoint*>( mMapPoints[vecPoints[j]]);
165  clu->addPoint( muPoint );
166  } else {
167  LOG_ERROR << "Cannot find StMuFcsPoint for StFcsPoint=" << vecPoints[j] << endm;
168  }
169  } // for j
170  } // for i
171  } // for idet
172 
173  // // Take care of the Points
174  // for (unsigned int idet = 0; idet < kFcsNDet; idet++){
175  // // StSPtrVecFcsHit vecHit = fcscol->hits(idet);
176  // for(unsigned int i=0; i<fcscol->numberOfPoints(idet); i++){
177  // if ( mMapPoints.count( fcscol->points(idet)[i] ) > 0 ) {
178  // StMuFcsPoint * muPoint = static_cast<StMuFcsPoint*>(mMapPoints[ fcscol->points(idet)[i] ]);
179  // muPoint->setCluster( static_cast<StMuFcsCluster*>(mMapClusters[ fcscol->points(idet)[i]->cluster() ]) );
180  // } else {
181  // LOG_ERROR << "Cannot find StMuFcsPoint for StFcsPoint=" << fcscol->points(idet)[i] << endm;
182  // } // if / else
183  // } // for i
184  // } // for idet
185 } // rebuildRelationships(...)
186 
187 
188 void StMuFcsUtil::fillMuFcsClusters(StMuFcsCollection* muFcs,
189  StFcsCollection* fcscol) {
190  // Fill clusters
191  for ( unsigned int idet = 0; idet < kFcsNDet; idet++ ){
192  for (unsigned i(0); i < fcscol->numberOfClusters(idet); ++i) {
193  const StFcsCluster* cluster = fcscol->clusters(idet)[i];
194  StMuFcsCluster* muCluster = muFcs->addCluster(); // Expand StMuFcsCollection cluster array by 1
195 
196  muCluster->setId( cluster->id() );
197  muCluster->setDetectorId( cluster->detectorId() );
198  muCluster->setCategory( cluster->category() );
199  muCluster->setNTowers( cluster->nTowers() );
200  muCluster->setEnergy( cluster->energy() );
201  muCluster->setX( cluster->x() );
202  muCluster->setY( cluster->y() );
203  muCluster->setSigmaMin( cluster->sigmaMin() );
204  muCluster->setSigmaMax( cluster->sigmaMax() );
205  muCluster->setTheta( cluster->theta() );
206  muCluster->setChi2Ndf1Photon( cluster->chi2Ndf1Photon() );
207  muCluster->setChi2Ndf2Photon( cluster->chi2Ndf2Photon() );
208  TLorentzVector lv;
209  lv.SetPxPyPzE( cluster->fourMomentum().px(), cluster->fourMomentum().py(), cluster->fourMomentum().pz(), cluster->fourMomentum().e() );
210  muCluster->setFourMomentum( lv );
211 
212  mMapClusters[ cluster ] = muCluster;
213  } // for i
214  } // for idet
215 } //fillMuFcsClusters
216 
217 void StMuFcsUtil::fillMuFcsPoints(StMuFcsCollection* muFcs,
218  StFcsCollection* fcscol) {
219 
220  for ( unsigned int idet = 0; idet < kFcsNDet; idet++ ){
221  for (unsigned i(0); i < fcscol->numberOfPoints(idet); ++i) {
222  const StFcsPoint* point = fcscol->points(idet)[i];
223  StMuFcsPoint* muPoint = muFcs->addPoint();
224  if (point && muPoint) {
225  muPoint->setDetectorId( point->detectorId() );
226  muPoint->setEnergy( point->energy() );
227  muPoint->setX( point->x() );
228  muPoint->setY( point->y() );
229  muPoint->setNParentClusterPhotons( point->nParentClusterPhotons() );
230  muPoint->setXYZ( TVector3(point->xyz().x(), point->xyz().y(), point->xyz().z()) );
231  TLorentzVector lv;
232  lv.SetPxPyPzE( point->fourMomentum().px(), point->fourMomentum().py(), point->fourMomentum().pz(), point->fourMomentum().e() );
233  muPoint->setFourMomentum( lv );
234 
235  mMapPoints[ point ] = muPoint;
236  } // if
237  } // for i
238  } // for idet
239 } // fillMuFcsPoints
240 
241 void StMuFcsUtil::fillMuFcsInfo(StMuFcsCollection* muFcs,
242  StFcsCollection* fcscol) {
243  muFcs->addInfo();
244  muFcs->setFcsReconstructionFlag(fcscol->fcsReconstructionFlag());
245 
246  // Now store the index for the first and last hit/cluster/point
247  // for each detector
248  size_t hitCount = 0;
249  size_t clusterCount = 0;
250  size_t pointCount = 0;
251  for ( unsigned int idet = 0; idet < kFcsNDet + 1; idet++ ){
252 
253 
254  muFcs->getInfo()->setHitIndex( idet, hitCount );
255  hitCount += fcscol->numberOfHits(idet);
256 
257  if ( idet == kFcsNDet )
258  break;
259  muFcs->getInfo()->setClusterIndex( idet, clusterCount );
260  clusterCount += fcscol->numberOfClusters(idet);
261 
262  muFcs->getInfo()->setPointIndex( idet, pointCount );
263  pointCount += fcscol->numberOfPoints(idet);
264  }
265 
266  // fill the final index with the total number
267  // so that it is easy to compute the # for the final detector
268  muFcs->getInfo()->setHitIndex( kFcsNDet + 1, hitCount );
269  muFcs->getInfo()->setClusterIndex( kFcsNDet, clusterCount );
270  muFcs->getInfo()->setPointIndex( kFcsNDet, pointCount );
271 
272 }
273 
274 void StMuFcsUtil::fillFcsHits(StFcsCollection* fcscol,
275  StMuFcsCollection* muFcs) {
276  // Using TIter to iterate is safe in the case of hits being NULL
277  TIter next(muFcs->getHitArray());
278  StMuFcsHit* muHit(NULL);
279  while ((muHit = static_cast<StMuFcsHit*>(next()))) {
280  unsigned int idet = static_cast<unsigned int>(muHit->detectorId());
281  fcscol->addHit(idet, new StFcsHit);
282  StFcsHit* hit = fcscol->hits(idet).back();
283 
284  unsigned int l = muHit->nTimeBin();
285  if ( muHit->zs() ) l *= 2;
286  hit->setFcsHit(
287  muHit->zs(), muHit->detectorId(), muHit->id(),
288  muHit->ns(), muHit->ehp(), muHit->dep(), muHit->channel(),
289  l, muHit->data()
290  );
291 
292  hit->setAdcSum ( muHit->adcSum() );
293  hit->setFitPeak ( muHit->fitPeak() );
294  hit->setFitSigma( muHit->fitSigma());
295  hit->setFitChi2 ( muHit->fitChi2() );
296  hit->setNPeak ( muHit->nPeak() );
297  hit->setEnergy ( muHit->energy() );
298  } // while
299 } // fillFcsHits