star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StMcAnalysisMaker.cxx
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TH3F.h"
3 #include "TNtuple.h"
4 #include "TSystem.h"
5 
6 #include "StarClassLibrary/StParticleDefinition.hh"
7 #include "StEvent/StTrack.h"
8 #include "StEvent/StPrimaryTrack.h"
9 #include "StEvent/StTrackGeometry.h"
10 #include "StMcEvent/StMcEventTypes.hh"
11 #include "StMcEvent/StMcContainers.hh"
12 #include "StarClassLibrary/SystemOfUnits.h"
13 #include "StEvent/StEvent.h"
14 #include "StEvent/StTrackNode.h"
15 #include "StEvent/StGlobalTrack.h"
16 #include "StEvent/StTpcHit.h"
17 #include "StEvent/StTrackTopologyMap.h"
18 
19 #include "StBFChain/StBFChain.h"
20 #include "StMcEvent/StMcEvent.hh"
21 #include "StAssociationMaker/StAssociationMaker.h"
22 #include "StAssociationMaker/StTrackPairInfo.hh"
23 #include "StMcEvent/StMcTpcHit.hh"
24 #include "StMcAnalysisMaker.h"
25 #include "StMcEvent/StMcTpcHitCollection.hh"
26 
27 #include "StEvent/StEventTypes.h"
28 #include "StEventUtilities/StuRefMult.hh"
29 #include "StEvent/StEventSummary.h"
30 #include "StEvent/StBTofCollection.h"
31 #include "StEvent/StBTofHeader.h"
32 #include "StEvent/StEnumerations.h"
33 #include "StEvent/StTpcDedxPidAlgorithm.h"
34 #include "StMcEvent/StMcVertex.hh"
35 #include "StarClassLibrary/StParticleTypes.hh"
36 
37 #include "StDetectorDbMaker/StDetectorDbTriggerID.h"
38 
40 
41 StMcAnalysisMaker::StMcAnalysisMaker(const char *name, const char *title): StMaker(name), mFile(NULL),
42  mIsNtuple(true) , mNtuple(NULL), mTpcNtuple(NULL)
43 {
44  cout << "StMcAnalysisMaker::StMcAnalysisMaker - DONE" << endl;
45 }
46 //__________________________________
48 {
49  StBFChain *bfChain = (StBFChain *) StMaker::GetChain();
50 
51  /*if (!bfChain) return kStFatal;
52  TString fileName( gSystem->BaseName(bfChain->GetFileOut().Data()) );
53  fileName = fileName.ReplaceAll(".event.root","");
54  */
55 
56  if(!mOutFileName.Length()) mOutFileName = "mcAnalysis";
57  mOutFileName = mOutFileName.ReplaceAll(".root","");
58 
59  mFile = new TFile(Form("%s.tpcRes.root",mOutFileName.Data()), "recreate");
60  assert(mFile && !mFile->IsZombie());
61 
62  hTpcHitsDiffXVsPadrowVsSector = new TH3F("hTpcHitsDiffXVsPadrowVsSector", "hTpcHitsDiffXVsPadrowVsSector", 50, 0, 50, 26, 0, 26, 200, -2, 2);
63  hTpcHitsDiffYVsPadrowVsSector = new TH3F("hTpcHitsDiffYVsPadrowVsSector", "hTpcHitsDiffYVsPadrowVsSector", 50, 0, 50, 26, 0, 26, 200, -2, 2);
64  hTpcHitsDiffZVsPadrowVsSector = new TH3F("hTpcHitsDiffZVsPadrowVsSector", "hTpcHitsDiffZVsPadrowVsSector", 50, 0, 50, 26, 0, 26, 200, -2, 2);
65 
66  mAssoc = (StAssociationMaker*)GetMaker("StAssociationMaker");
67 
68  if (!mAssoc)
69  {
70  cout << " empty StAssociateMaker, stop!!" << endl;
71  exit(1);
72  }
73 
74  if (mIsNtuple)
75  {
76  mNtuple = new TNtuple("nt", "nt", "pt:phi:eta:geantId:svx:svy:svz:nCommon:nFit:nMax:gPt:gPhi:gEta:dca:dcaXY:dcaZ:hftTopo:isTrueHft");
77  mTpcNtuple = new TNtuple("tpc","tpc","pt:nCommon:nFit:nMax:hOx:hOy:hOz:eta:phi:mcX:mcY:mcZ:mcTb:rcX:rcY:rcZ:rcTb:sector:padrow:pad:dE:adc:mcHitToRcTrackX:mcHitToRcTrackY:mcHitToRcTrackZ");
78  }
79 
80  cout << "StMcAnalysisMaker::Init - DONE" << endl;
81  return StMaker::Init();
82 }
83 
84 //__________________________________
86 {
87  cout << "StMcAnalysisMaker::Make() - call" << endl;
88  StMcEvent* mcEvent = (StMcEvent*)GetDataSet("StMcEvent");
89 
90  if (!mcEvent)
91  {
92  LOG_WARN << "No StMcEvent" << endm;
93  return kStWarn;
94  }
95 
96  StEvent* event = (StEvent*)GetDataSet("StEvent");
97  if (!event)
98  {
99  LOG_WARN << "No StEvent" << endm;
100  return kStWarn;
101  }
102 
103  cout << "StMcAnalysisMaker::Make() : event: " << event->id() << endl;
104 
105  return fillTracks(mcEvent,event);
106 }
107 //____________________________________
108 int StMcAnalysisMaker::fillTracks(StMcEvent* mcEvent,StEvent* event)
109 {
110  StSPtrVecMcTrack& trks = mcEvent->tracks();
111  cout << "Filling " << trks.size() << " mcTracks..." << endl;
112 
113  if (!mAssoc->rcTpcHitMap())
114  {
115  cout << "!!!There is no rcTpcHitMap in the association maker!!!!" << endl;
116  return 1;
117  }
118 
119  for (unsigned int i = 0; i < trks.size(); i++)
120  {
121  StMcTrack* mcTrack = trks[i];
122 
123  float pTrkSvx, pTrkSvy;
124  if(mcTrack->startVertex())
125  {
126  pTrkSvx = mcTrack->startVertex()->position().x();
127  pTrkSvy = mcTrack->startVertex()->position().y();
128  }
129  else continue;
130 
131  if(sqrt(pTrkSvx*pTrkSvx + pTrkSvy*pTrkSvy)>3.) continue;
132 
133  int ncommonhits = 0;
134  const StTrack* rcTrack = findPartner(mcTrack, ncommonhits);
135 
136  bool istTruth = true;
137  bool pxlTruth1 = true;
138  bool pxlTruth2 = true;
139 
140  if (rcTrack)
141  {
142  StPtrVecHit rcIstHits = rcTrack->detectorInfo()->hits(kIstId);
143  StPtrVecHit rcPxlHits = rcTrack->detectorInfo()->hits(kPxlId);
144 
145  int nRcIstHits = (int)rcIstHits.size();
146  int nRcPxlHits = (int)rcPxlHits.size();
147 
148 
149  for (int iHit = 0; iHit < nRcIstHits; ++iHit)
150  {
151  if (rcIstHits[iHit]->idTruth() != mcTrack->key())
152  {
153  istTruth = false;
154  break;
155  }
156  }
157 
158  for (int iHit = 0; iHit < nRcPxlHits; ++iHit)
159  {
160  if (rcPxlHits[iHit]->idTruth() != mcTrack->key())
161  {
162  StThreeVectorF pos = rcPxlHits[iHit]->position();
163 
164  float const R = pow(pos.x(), 2.0) + pow(pos.y(), 2.0);
165  if (R > 3.5 * 3.5) pxlTruth2 = false;
166  else pxlTruth1 = false;
167  }
168  }
169  }
170 
171  float dca = -999.;
172  float dcaXY = -999.;
173  float dcaZ = -999.;
174 
175  if(rcTrack)
176  {
177  StPhysicalHelixD helix = rcTrack->geometry()->helix();
178  dca = helix.distance(event->primaryVertex()->position());
179  dcaXY = helix.geometricSignedDistance(event->primaryVertex()->position().x(),event->primaryVertex()->position().y());
180 
181  StThreeVectorF dcaPoint = helix.at(helix.pathLength(event->primaryVertex()->position()));
182  dcaZ = dcaPoint.z() - event->primaryVertex()->position().z();
183  }
184 
185  float arr[50];
186  int iArr = 0;
187  arr[iArr++] = mcTrack->pt();
188  arr[iArr++] = mcTrack->momentum().phi();
189  arr[iArr++] = mcTrack->pseudoRapidity();
190  arr[iArr++] = mcTrack->geantId();
191  arr[iArr++] = mcTrack->startVertex()->position().x();
192  arr[iArr++] = mcTrack->startVertex()->position().y();
193  arr[iArr++] = mcTrack->startVertex()->position().z();
194  arr[iArr++] = rcTrack ? ncommonhits : -999;
195  arr[iArr++] = rcTrack ? rcTrack->fitTraits().numberOfFitPoints(kTpcId) : -999;
196  arr[iArr++] = rcTrack ? rcTrack->numberOfPossiblePoints() : -999;
197  arr[iArr++] = rcTrack ? rcTrack->geometry()->momentum().perp() : -999;
198  arr[iArr++] = rcTrack ? rcTrack->geometry()->momentum().phi() : -999;
199  arr[iArr++] = rcTrack ? rcTrack->geometry()->momentum().pseudoRapidity() : -999;
200  arr[iArr++] = dca;
201  arr[iArr++] = dcaXY;
202  arr[iArr++] = dcaZ;
203  arr[iArr++] = rcTrack ? (rcTrack->topologyMap().data(0)) >> 1 & 0x7F: 0;
204  arr[iArr++] = static_cast<float>(istTruth && pxlTruth1 && pxlTruth2);
205  mNtuple->Fill(arr);
206 
207  if (!rcTrack) continue;
208 
209  StPtrVecHit rcTpcHits = rcTrack->detectorInfo()->hits(kTpcId);
210  // const StPtrVecMcHit* mcTpcHits = mcTrack->Hits(kTpcId);
211 
212  StPhysicalHelixD helix = rcTrack->geometry()->helix();
213 
214  for (size_t ih = 0; ih < rcTpcHits.size(); ih++)
215  {
216  StTpcHit* rcHit = dynamic_cast<StTpcHit*>(rcTpcHits[ih]);
217  if (!rcHit) continue;
218 
219  pair<rcTpcHitMapIter, rcTpcHitMapIter> bounds = mAssoc->rcTpcHitMap()->equal_range(rcHit);
220 
221  // loop over all mcHits associated with this rcHit
222  bool found = false;
223  for (rcTpcHitMapIter iter = bounds.first; iter != bounds.second; iter++)
224  {
225  const StMcTpcHit* mcHit = (*iter).second;
226 
227  // fill histograms if this mcHit belongs to this mcTrack
228  if (mcHit->parentTrack()->key() == mcTrack->key())
229  {
230  StThreeVectorF mcHitTotRcTrack = helix.at(helix.pathLength(mcHit->position().x(),mcHit->position().y())) - mcHit->position();
231 
232  float arr[55];
233  int iArr = 0;
234  arr[iArr++] = mcTrack->pt();
235  arr[iArr++] = rcTrack ? ncommonhits : -999;
236  arr[iArr++] = rcTrack ? rcTrack->fitTraits().numberOfFitPoints(kTpcId) : -999;
237  arr[iArr++] = rcTrack ? rcTrack->numberOfPossiblePoints() : -999;
238  arr[iArr++] = helix.origin().x();
239  arr[iArr++] = helix.origin().y();
240  arr[iArr++] = helix.origin().z();
241  arr[iArr++] = mcTrack->pseudoRapidity();
242  arr[iArr++] = mcTrack->momentum().phi();
243  arr[iArr++] = mcHit->position().x();
244  arr[iArr++] = mcHit->position().y();
245  arr[iArr++] = mcHit->position().z();
246  arr[iArr++] = mcHit->timeBucket();
247  arr[iArr++] = rcHit->position().x();
248  arr[iArr++] = rcHit->position().y();
249  arr[iArr++] = rcHit->position().z();
250  arr[iArr++] = rcHit->timeBucket();
251  arr[iArr++] = mcHit->sector();
252  arr[iArr++] = mcHit->padrow();
253  arr[iArr++] = mcHit->pad();
254  arr[iArr++] = mcHit->dE();
255  arr[iArr++] = rcHit->adc();
256  arr[iArr++] = mcHitTotRcTrack.x();
257  arr[iArr++] = mcHitTotRcTrack.y();
258  arr[iArr++] = mcHitTotRcTrack.z();
259 
260  mTpcNtuple->Fill(arr);
261 
262  hTpcHitsDiffXVsPadrowVsSector->Fill(mcHit->padrow(), mcHit->sector(), mcHit->position().x() - rcHit->position().x());
263  hTpcHitsDiffYVsPadrowVsSector->Fill(mcHit->padrow(), mcHit->sector(), mcHit->position().y() - rcHit->position().y());
264  hTpcHitsDiffZVsPadrowVsSector->Fill(mcHit->padrow(), mcHit->sector(), mcHit->position().z() - rcHit->position().z());
265  found = true;
266  }
267  else
268  {
269  cout << "Not a good candidate" << endl;
270  }
271  }
272 
273  if (!found)
274  {
275  cout << "No mc hit was found for this rc Hit!!!!" << endl;
276  }
277  }
278 
279 
280  /*
281  cout<<"ncommonhits "<<ncommonhits<<endl;
282  cout<<"Rc track hits"<<endl;
283  for(size_t ih = 0; ih <rcTpcHits.size(); ih++)
284  {
285  StTpcHit* rcHit = dynamic_cast<StTpcHit*>(rcTpcHits[ih]);
286  if(!rcHit) continue;
287  cout<<rcHit->qaTruth()<<endl;
288  }
289 
290  cout<<"MC track hits"<<endl;
291  for(size_t ih = 0; ih <mcTpcHits->size(); ih++)
292  {
293  StMcTpcHit* mcHit = dynamic_cast<StMcTpcHit*>((*mcTpcHits)[ih]);
294  if(!mcHit) continue;
295  cout<<mcHit->key()<<endl;
296  }
297  */
298  }
299 
300  return kStOk;
301 }
302 
303 
304 //________________________________________________
305 const StTrack* StMcAnalysisMaker::findPartner(StMcTrack* mcTrack, int& maxCommonTpcHits)
306 {
307  //..StMcTrack find partner from the StTracks
308  pair<mcTrackMapIter, mcTrackMapIter> p = mAssoc->mcTrackMap()->equal_range(mcTrack);
309 
310  const StTrack* maxTrack = 0;
311  maxCommonTpcHits = 0;
312  for (mcTrackMapIter k = p.first; k != p.second; ++k)
313  {
314  int commonTpcHits = k->second->commonTpcHits();
315  const StTrack* track = k->second->partnerTrack()->node()->track(global);//should be global
316  if (track && commonTpcHits > maxCommonTpcHits)
317  {
318  maxTrack = track;
319  maxCommonTpcHits = commonTpcHits;
320  }
321  }
322  return maxTrack;
323 }
324 
325 
326 //________________________________________________
327 const StMcTrack* StMcAnalysisMaker::findPartner(StGlobalTrack* rcTrack, int& maxCommonTpcHits)
328 {
329  //.. StGlobalTracks find partner from StMcTracks.
330  //.. See example from StRoot/StMcAnalysisMaker
331  pair<rcTrackMapIter, rcTrackMapIter> p = mAssoc->rcTrackMap()->equal_range(rcTrack);
332 
333  const StMcTrack* maxTrack = 0;
334  maxCommonTpcHits = 0;
335  for (rcTrackMapIter k = p.first; k != p.second; ++k)
336  {
337  int commonTpcHits = k->second->commonTpcHits();
338 
339  const StMcTrack* track = k->second->partnerMcTrack();
340 
341  if (track && commonTpcHits > maxCommonTpcHits)
342  {
343  maxTrack = track;
344  maxCommonTpcHits = commonTpcHits;
345  }
346  }
347  return maxTrack;
348 }
349 //______________________________________________________
351 {
352  mFile->cd();
353  if (mIsNtuple)
354  {
355  mNtuple->Write();
356  mTpcNtuple->Write();
357  }
361  mFile->Close();
362  return kStOk;
363 }
const StTrack * findPartner(StMcTrack *, int &)
ClassImp(StMcAnalysisMaker)
StAssociationMaker * mAssoc
int fillTracks(StMcEvent *, StEvent *)
TH3F * hTpcHitsDiffZVsPadrowVsSector
TH3F * hTpcHitsDiffXVsPadrowVsSector
TH3F * hTpcHitsDiffYVsPadrowVsSector
StMcAnalysisMaker(const char *name="StMcAnalysisMaker", const char *title="event/StMcAnalysisMaker")