EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EicSmearTask.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EicSmearTask.cxx
1 //
2 // AYK (ayk@bnl.gov), 2013/06/12
3 //
4 // Interface to eic-smear package;
5 //
6 
7 #include <iostream>
8 #include <cassert>
9 
10 #include "eicsmear/smear/Device.h"
11 
12 #include <PndPidCandidate.h>
13 #include <PndMCTrack.h>
14 
15 #include <EicSmearTask.h>
16 
17 // ---------------------------------------------------------------------------------------
18 
20 {
21  // Open eic-smear input file and get access to EICTree;
22  inFile = new TFile(inFileName, "READ");
23  if(not inFile->IsOpen()) {
24  std::cerr << "Unable to open " << inFileName << std::endl;
25  return kFATAL;
26  } // if
27  inFile->GetObject("EICTree", mcTree);
28  if(not mcTree) {
29  std::cerr << "Unable to find EICTree in " << inFileName << std::endl;
30  return kFATAL;
31  } // if
32 
33  // Unless provided via command line, create a dummy smearing detector;
34  if (!detector)
35  {
37  //Smear::Device momentum(Smear::kP, "0.01 * P");
41  detector->AddDevice(momentum);
42  detector->AddDevice(theta);
43  detector->AddDevice(phi);
45  } /*if*/
46 
47  // Create smeared event builder; follow the logic of SmearTree.cxx;
48  TClass branchClass(mcTree->GetBranch("event")->GetClassName());
49  if(branchClass.InheritsFrom("erhic::EventDis")) {
50  builder = new Smear::EventDisFactory(*detector, *(mcTree->GetBranch("event")));
51  } // if
52  // Leave it here for now (but OFF); will need to modify Exec() call if ever
53  // want to activate;
54 #ifdef WITH_PYTHIA6_OFF
55  else if(branchClass.InheritsFrom("erhic::hadronic::EventMC")) {
56  builder = new Smear::HadronicEventBuilder(detector, *(mcTree->GetBranch("event")));
57  } // else if
58 #endif
59  else {
60  std::cerr << branchClass.GetName() << " is not supported for smearing" <<
61  std::endl;
62  return kFATAL;
63  } // else
64 
65  // Open the output file;
66  TString outName = TString(inFileName).ReplaceAll(".root", ".smear.root");
67  outFile = new TFile(outName, "RECREATE");
68  if(not outFile->IsOpen()) {
69  std::cerr << "Unable to create " << outName << std::endl;
70  return kFATAL;
71  } // if
72  smearedTree = new TTree("Smeared", "A tree of smeared Monte Carlo events");
73 
74  eventbranch = builder->Branch(*smearedTree, "eventS");
75 
76  // Get access to arrays with PandaRoot MC tracks and charged track candidates;
78  fMCTracks = (TClonesArray*)fManager->GetObject("MCTrack");
79  if ( ! fMCTracks ) {
80  std::cout << "-E- EicSmearTask::Init: No MCTrack array found!" << std::endl;
81  return kERROR;
82  }
83  fPidChargedCand = (TClonesArray *)fManager->GetObject("PidChargedCand");
84  if ( ! fPidChargedCand) {
85  std::cout << "-E- EicSmearTask::Init: No PidChargedCand array found!" << std::endl;
86  return kERROR;
87  }
88 
89  return kSUCCESS;
90 } // EicSmearTask::Init()
91 
92 // ---------------------------------------------------------------------------------------
93 
95 {
96 } // EicSmearTask::SetParContainers()
97 
98 // ---------------------------------------------------------------------------------------
99 
100 void EicSmearTask::Exec(Option_t* opt)
101 {
102  static unsigned evcounter;
103 
104  if (!mcTree->GetEntry(evcounter++)) return;
105 
106  //std::cout << fMCTracks->GetEntriesFast() << " MCTrack entries" << std::endl;
107  //std::cout << fPidChargedCand->GetEntriesFast() << " PidChargedCand entries" << std::endl;
108 
109  {
111  unsigned id[mcEvent->GetNTracks()];
112 
113  // Cook compressed index array of original (eic-smear) particles; indeed
114  // only those were given to the GEANT4 in simulation.C, which had status = 1;
115  {
116  unsigned rcounter = 0;
117 
118  for(unsigned tr=0; tr<mcEvent->GetNTracks(); tr++)
119  {
120  erhic::VirtualParticle *vp = mcEvent->GetTrack(tr);
121 
122  // 'vp' pointer itself can not go NULL here; or should I check on that?;
123  if (vp->GetStatus() == 1) id[rcounter++] = tr;
124  } /*for tr*/
125  }
126 
127  // Loop through the charged track candidates forund by PandaRoot and modify
128  // 4-momentum fields in original eic-smear arrays before running smearing
129  // builder code;
130  for(unsigned ch=0; ch<fPidChargedCand->GetEntriesFast(); ch++)
131  {
132  PndPidCandidate* pidcand = (PndPidCandidate*)fPidChargedCand->At(ch);
133 
134  // Later may want to append eic-smear array with entries which are not
135  // associated with the original (MC) tracks (ghosts, etc);
136  Int_t mcid = pidcand->GetMcIndex();
137  if(mcid<0) continue; // no specified MC id... do nothing
138  PndMCTrack *mctrack = (PndMCTrack*)fMCTracks->At(mcid);
139  if( 0==mctrack) continue; // better do nothing on a null pointer
140  Int_t mcpdg = mctrack->GetPdgCode();
141 
142  // Find respective particle in the original MC array (eic-smear input);
143  erhic::VirtualParticle *vp = mcEvent->GetTrack(id[mcid]);
144 
145  // Modify 4-momentum; consider to use PDG code to obtain mass later?;
146  // for now mass is taken as PDG hypothesis provided to Kalman filter,
147  // which is pion per default I guess;
148  vp->Set4Vector(pidcand->GetLorentzVector());
149 
150  //printf(" %02d# -> %3d, PDG %5d (was %5d), rec.mass: %8.4f\n",
151  // ch, mcid, mcpdg, int(vp->Id()), pidcand->GetLorentzVector().Mag());
152  } /*for ch*/
153  }
154 
155  // Run eic-smear code, eventually;
157 } // EicSmearTask::Exec()
158 
159 // ---------------------------------------------------------------------------------------
160 
162 {
163  outFile->cd();
164  smearedTree->Write();
165 
166  // Is this stuff really needed?;
167  detector->Write("detector");
168  outFile->Purge();
169 
170 #if _OFF_
171  std::cout << smearedTree->GetEntries() << " entries in the output tree" << std::endl;
172  {
173  //erhic::VirtualEvent *event(0);
174  Smear::Event *event(0);
175  smearedTree->SetBranchAddress("eventS", &event);
176 
177  for(unsigned ev=0; ev<smearedTree->GetEntries(); ev++)
178  {
179  smearedTree->GetEntry(ev);
180 
181  std::cout << event->GetNTracks() << " tracks in the event" << std::endl;
182  // Loop through all the tracks and feed them to the FairRoot primary generator;
183  for(unsigned iq=0; iq<event->GetNTracks(); iq++)
184  {
185  //erhic::VirtualParticle *vp = event->GetTrack(iq);
186  Smear::ParticleMCS *vp = event->GetTrack(iq);
187 
188  // May want to do it better later; suffices for now;
189  if (!vp || vp->GetStatus() != 1) continue;
190 
191  std::cout << vp->id << " " << vp->GetPx() << " " << vp->GetPy() << " " << vp->GetPz() << std::endl;
192  } /*for iq*/
193  } /*for ev*/
194  }
195 
196  std::cout << "About to finish!" << std::endl;
197 #endif
198 
200 } // EicSmearTask::FinishTask()
201 
202 // ---------------------------------------------------------------------------------------
203