StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEStructMCReader.cxx
1 /**********************************************************************
2  *
3  * $Id: StEStructMCReader.cxx,v 1.10 2012/11/16 21:19:07 prindle Exp $
4  *
5  * Author: Chunhui Han
6  *
7  **********************************************************************
8  *
9  * Description:
10  * Read in STAR standard MC rootuples, and apply track and event cuts
11  * The output is StEStructEvent.
12  *
13  **********************************************************************
14  *
15  * $Log: StEStructMCReader.cxx,v $
16  * Revision 1.10 2012/11/16 21:19:07 prindle
17  * Moved EventCuts, TrackCuts to EventReader. Affects most readers.
18  * Added support to write and read EStructEvents.
19  * Cuts: 3D histo support, switch to control filling of histogram for reading EStructEvents
20  * EventCuts: A few new cuts
21  * MuDstReader: Add 2D to some histograms, treat ToFCut, PrimaryCuts, VertexRadius histograms like other cut histograms.
22  * QAHists: Add refMult
23  * TrackCuts: Add some hijing cuts.
24  *
25  * Revision 1.9 2009/01/26 14:40:43 fisyak
26  * Add missing (in ROOT 5.22) includes
27  *
28  * Revision 1.8 2006/04/06 00:53:59 prindle
29  * Tried to rationalize the way centrality is defined.
30  * Now the reader gives a float to StEStructEvent and this float is
31  * what is being used to define centrality. When we need a centrality
32  * bin index we pass this number into the centrality singleton object.
33  *
34  * Revision 1.7 2006/02/22 22:03:19 prindle
35  * Removed all references to multRef
36  *
37  * Revision 1.6 2004/03/19 21:42:48 chunhuih
38  * calculate pseudorapidity instead of rapidity for eta used in track cuts
39  *
40  * Revision 1.5 2004/03/18 18:35:48 chunhuih
41  *
42  * use const char * instead of char * for the constructor argument filelistfile.
43  *
44  * Revision 1.4 2004/03/05 23:48:11 chunhuih
45  *
46  * test the number of entries for the given chain: avoid integer overflow.
47  * use GetEntries() instead of GetEntriesFast() for TChain.
48  *
49  * Revision 1.3 2004/03/03 23:16:00 chunhuih
50  *
51  * removed the code in the constructor that gets the total number of events
52  * to reduce initialization time.
53  *
54  * Revision 1.2 2004/03/02 21:34:47 chunhuih
55  *
56  * added impact parameter information to the StEStructEvent
57  *
58  * Revision 1.1 2004/02/26 20:06:25 chunhuih
59  * initial import
60  *
61  *
62  **********************************************************************/
63 #include "StEStructMCReader.h"
64 #include "StEStructPool/AnalysisMaker/StEStructEventCuts.h"
65 #include "StEStructPool/AnalysisMaker/StEStructTrackCuts.h"
66 #include "StEStructPool/EventMaker/StEStructEvent.h"
67 #include "StEStructPool/EventMaker/StEStructTrack.h"
68 #include "StMessMgr.h"
69 #include "TMath.h"
70 #include "TH2.h"
71 #include "TStyle.h"
72 #include "TCanvas.h"
73 #include <fstream>
74 
75 ClassImp(StEStructMCReader)
76 
77 StEStructMCReader::StEStructMCReader(TTree *tree) : meventsToDo(0), meventCount(0), mloopIndex(0), mAmDone(false), mNentries(0), mIPMAX(1000000) {
78  Init(tree);
79 }
80 
81 StEStructMCReader::StEStructMCReader(int nevents, TTree *tree, StEStructEventCuts *ecuts, StEStructTrackCuts *tcuts) : meventsToDo(nevents), meventCount(0), mloopIndex(0), mAmDone(false), mNentries(0), mIPMAX(1000000) {
82  Init(tree);
83 }
84 
85 StEStructMCReader::StEStructMCReader(int nevents, const char *fileListFile, StEStructEventCuts *ecuts, StEStructTrackCuts *tcuts) : meventsToDo(nevents), meventCount(0), mloopIndex(0), mAmDone(false), mNentries(0), mIPMAX(1000000) {
86  ifstream fin(fileListFile);
87  char s[256];
88  TChain *chain = new TChain("h999");
89  while(fin >> s ) {
90  chain->Add(s);
91  }
92  Init((TTree *)chain);
93 }
94 
95 Int_t StEStructMCReader::LoadTree(Int_t entry)
96 {
97 // Set the environment to read one entry
98  if (!fChain) return -5;
99  Int_t centry = fChain->LoadTree(entry);
100  if (centry < 0) return centry;
101  if (fChain->IsA() != TChain::Class()) return centry;
102  TChain *chain = (TChain*)fChain;
103  if (chain->GetTreeNumber() != fCurrent) {
104  fCurrent = chain->GetTreeNumber();
105  Notify();
106  }
107  return centry;
108 }
109 
110 Int_t StEStructMCReader::GetEntry(Int_t entry)
111 {
112 // Read contents of entry.
113  if (!fChain) return 0;
114  return fChain->GetEntry(entry);
115 }
116 
117 void StEStructMCReader::Init(TTree *tree)
118 {
119 // Set branch addresses
120  if (tree == 0) return;
121 
122  fChain = tree;
123  fCurrent = -1;
124  fChain->SetMakeClass(1);
125 
126  fChain->SetBranchAddress("itrac",&itrac);
127  fChain->SetBranchAddress("istat",&istat);
128  fChain->SetBranchAddress("ipdg",&ipdg);
129  fChain->SetBranchAddress("moth1",&moth1);
130  fChain->SetBranchAddress("moth2",&moth2);
131  fChain->SetBranchAddress("idau1",&idau1);
132  fChain->SetBranchAddress("idau2",&idau2);
133  fChain->SetBranchAddress("Pxyz",Pxyz);
134  fChain->SetBranchAddress("ener",&ener);
135  fChain->SetBranchAddress("mass",&mass);
136  fChain->SetBranchAddress("Vxyz",Vxyz);
137  fChain->SetBranchAddress("Vtime",&Vtime);
138  Notify();
139 
140  // get number of entries in this chain
141  if(fChain != 0) {
142  double x = fChain->GetEntries();
143  if( x > (pow(2., 8. * sizeof(int) - 1.) - 1.) )
144  gMessMgr->Message("Number of Entries causes integer overflow.", "E");
145  else
146  mNentries = int(x);
147  }
148 }
149 
150 Bool_t StEStructMCReader::Notify()
151 {
152  // Called when loading a new file.
153  // Get branch pointers.
154  b_itrac = fChain->GetBranch("itrac");
155  b_istat = fChain->GetBranch("istat");
156  b_ipdg = fChain->GetBranch("ipdg");
157  b_moth1 = fChain->GetBranch("moth1");
158  b_moth2 = fChain->GetBranch("moth2");
159  b_idau1 = fChain->GetBranch("idau1");
160  b_idau2 = fChain->GetBranch("idau2");
161  b_Pxyz = fChain->GetBranch("Pxyz");
162  b_ener = fChain->GetBranch("ener");
163  b_mass = fChain->GetBranch("mass");
164  b_Vxyz = fChain->GetBranch("Vxyz");
165  b_Vtime = fChain->GetBranch("Vtime");
166  return kTRUE;
167 }
168 
169 void StEStructMCReader::Show(Int_t entry)
170 {
171 // Print contents of entry.
172 // If entry is not specified, print current entry
173  if (!fChain) return;
174  fChain->Show(entry);
175 }
176 Int_t StEStructMCReader::Cut(Int_t entry)
177 {
178 // This function may be called from Loop.
179 // returns 1 if entry is accepted.
180 // returns -1 otherwise.
181  return 1;
182 }
183 
184 void StEStructMCReader::Loop()
185 {
186  if (fChain == 0) return;
187 
188  Int_t nentries = Int_t(fChain->GetEntries());
189 
190  Int_t nbytes = 0, nb = 0;
191  for (Int_t jentry=0; jentry<nentries;jentry++) {
192  Int_t ientry = LoadTree(jentry);
193  if (ientry < 0) break;
194  nb = fChain->GetEntry(jentry); nbytes += nb;
195  }
196 }
197 
198 StEStructMCReader::~StEStructMCReader()
199 {
200  if (!fChain) return;
201  delete fChain->GetCurrentFile();
202 }
203 
204 int StEStructMCReader::getTotalEventCount() {
205  if (fChain == 0) return 0;
206 
207  double nentries = fChain->GetEntries();
208 
209  Int_t nbytes = 0, nb = 0;
210  int retVal = 0;
211  for (Int_t jentry=0; jentry<nentries;jentry++) {
212  Int_t ientry = LoadTree(jentry);
213  if (ientry < 0) break;
214  nb = fChain->GetEntry(jentry); nbytes += nb;
215  if(itrac == -1) retVal++;
216  }
217  return retVal;
218 }
219 
220 int StEStructMCReader::getCharge(int pid) {
221  if(pid == -11 || pid == -13) return 1;
222  else if(pid == 11 || pid == 13) return -1;
223  else if(pid > 0) return 1;
224  else if(pid < 0) return -1;
225  return 0;
226 }
227 
228 bool StEStructMCReader::measureable(int pid){
229  bool retVal=false;
230  if(pid<0)pid*=-1;
231 
232  switch(pid){
233 
234  case 211:
235  { // charged pion
236  retVal=true;
237  break;
238  }
239  case 321:
240  { // charged kaon
241  retVal=true;
242  break;
243  }
244  case 2212:
245  { // proton
246  retVal=true;
247  break;
248  }
249  case 11:
250  { // electron
251  retVal=true;
252  break;
253  }
254  default:
255  {
256  break;
257  }
258  }
259  return retVal;
260 }
261 
262 float* StEStructMCReader::globalDCA(float* p, float* v){
263 
264  // assumes primaryVertex at origin
265  float* r=new float[4];
266  r[0]=r[1]=r[2]=r[3]=0;
267 
268  // a is the component of v vector that is parallel to p.
269  float a = v[0] * p[0] + v[1] * p[1] + v[2] * p[2] ;
270  a = a / sqrt(p[0]*p[0] + p[1]*p[1] + p[2]*p[2]);
271  // r is the component of v vector perpendicular to p.
272  r[3] = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] - a*a);
273  // Since only the magnitude of globalDCA is used,
274  // leave r[0], r[1], r[2] to be 0 here.
275  return r;
276 }
277 
278 StEStructEvent* StEStructMCReader::next() {
279  if( fChain == NULL || (meventsToDo != 0 && meventCount == meventsToDo) ||
280  mloopIndex >= mNentries ) {
281  mAmDone = true;
282  return (StEStructEvent *)NULL;
283  }
284  StEStructEvent *retVal = new StEStructEvent();
285 
286  fillTracks(retVal);
287 
288  bool useEvent = mECuts->goodCentrality(retVal->Centrality());
289  if(!useEvent) {
290  delete retVal;
291  retVal = NULL;
292  }
293  else {
294  retVal->FillChargeCollections();
295  }
296  mECuts->fillHistogram(mECuts->centralityName(), retVal->Centrality(), useEvent);
297  return retVal;
298 }
299 
300 //--------------------------------------------------------------------------
301 void StEStructMCReader::fillTracks(StEStructEvent* estructEvent) {
302 
303  mnumTracks=0;
304  StEStructTrack* eTrack= new StEStructTrack();
305 
306  Int_t nbytes = 0, nb = 0;
307  itrac = 0;
308  for(; mloopIndex < mNentries && itrac != -1; mloopIndex++) {
309  Int_t ientry = LoadTree(mloopIndex);
310  if(ientry < 0) break;
311  nb = fChain->GetEntry(mloopIndex); nbytes += nb;
312  eTrack->SetInComplete();
313  if(itrac == -1 ) {
314  meventCount++;
315  }
316  // if(istat == 11 && ipdg == mIPMAX - 1):
317  // this event header contains:
318  // Pxyz[0]: run ID
319  // Pxyz[1]: evt ID
320  // Pxyz[2]: Date
321  if(istat == 11 && ipdg == mIPMAX - 2) {
322  // this header contains:
323  // Pxyz[0]: Impact Parameter
324  // Pxyz[1]:
325  // Pxyz[2]:
326  estructEvent->SetCentrality(Pxyz[0]);
327  }
328  if(istat == 11 && ipdg == mIPMAX - 3) {
329  // this header contains:
330  // Pxyz[0]: A1
331  // Pxyz[1]: Z1
332  // Pxyz[2]: A2
333  }
334  if(istat != 1 || !measureable(ipdg) ) continue;
335 
336  float pt = TMath::Sqrt(Pxyz[0] * Pxyz[0] + Pxyz[1] * Pxyz[1]);
337  if( pt < 0.15 ) continue;
338 
339  float *gdca = globalDCA(Pxyz, Vxyz);
340  bool useTrack = true;
341  useTrack = (mTCuts->goodGlobalDCA(gdca[3]) && useTrack);
342 
343  float theta = acos(Pxyz[2] / TMath::Sqrt(Pxyz[2] * Pxyz[2] + pt * pt));
344  float eta = -log(tan(theta/2.));
345  useTrack = (mTCuts->goodEta(eta) && useTrack);
346  float phi=atan2((double)Pxyz[1], (double)Pxyz[0]);
347  useTrack=(mTCuts->goodPhi(phi) && useTrack);
348  if(useTrack)mnumTracks++;
349 
350  useTrack=(mTCuts->goodPt(pt) && useTrack);
351 
352  float _r=pt/0.139;
353  float yt=log(sqrt(1+_r*_r)+_r);
354  useTrack = (mTCuts->goodYt(yt) && useTrack);
355 
356  mTCuts->fillHistograms(useTrack);
357 
358  eTrack->SetBx(gdca[0]);
359  eTrack->SetBy(gdca[1]);
360  eTrack->SetBz(gdca[2]);
361  eTrack->SetBxGlobal(gdca[0]);
362  eTrack->SetByGlobal(gdca[1]);
363  eTrack->SetBzGlobal(gdca[2]);
364 
365  delete [] gdca;
366  if(!useTrack) continue;
367 
368  eTrack->SetPx(Pxyz[0]);
369  eTrack->SetPy(Pxyz[1]);
370  eTrack->SetPz(Pxyz[2]);
371  eTrack->SetEta(eta);
372  eTrack->SetPhi(phi);
373  eTrack->SetCharge(getCharge(ipdg));
374  estructEvent->AddTrack(eTrack);
375  }
376  delete eTrack;
377  return;
378 }