star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vtxhist.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cfloat>
3 #include <iostream>
4 #include <string>
5 
6 #include "TTree.h"
7 #include "TChain.h"
8 
9 #include "StEvent/StBTofHeader.h"
10 #include "StEvent/StTrackTopologyMap.h"
11 #include "StMuDSTMaker/COMMON/StMuDst.h"
12 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
13 #include "StMuDSTMaker/COMMON/StMuEvent.h"
14 #include "StMuDSTMaker/COMMON/StMuMcTrack.h"
15 #include "StMuDSTMaker/COMMON/StMuMcVertex.h"
16 #include "StMuDSTMaker/COMMON/StMuPrimaryVertex.h"
17 #include "StMuDSTMaker/COMMON/StMuTrack.h"
18 #include "StSecondaryVertexMaker/DecayVertexFinder.h"
19 #include "StVertexRootIO/TPrimaryVertex.h"
20 #include "StVertexRootIO/TDecayVertexContainers.h"
21 
22 #include "travex/ProgramOptions.h"
23 #include "src-tools/utils.h"
24 #include "VtxEval/VertexRootFile.h"
25 
26 
27 namespace po = boost::program_options;
28 
29 
30 void process_muDst(VertexRootFile& outFile);
31 
32 // Verify whether this vertex has an HFT track with a PXL hit
33 bool checkVertexHasPxlHit(int vertexIndex, const StMuDst& stMuDst);
34 
35 // Currently used to work around a bug in reco chains giving different numerical
36 // values in the second event
37 bool SkipCurrentEvent(const StMuDstMaker& maker);
38 
39 
40 
41 int main(int argc, char **argv)
42 {
43  tvx::ProgramOptions prgOpts(argc, argv);
44 
45  prgOpts.ProcessOptions();
46  prgOpts.Print();
47 
48  VertexRootFile vertexOutFile(prgOpts, "recreate");
49 
50  process_muDst(vertexOutFile);
51 
52  vertexOutFile.Write();
53  vertexOutFile.Close();
54 
55  return EXIT_SUCCESS;
56 }
57 
58 
59 
61 {
62  // Create a TTree with primary vertex info
63  TPrimaryVertex *primVtx = new TPrimaryVertex();
64 
65  TTree *primVertexTree = new TTree("primVertexTree", "The Primary Vertices");
66  primVertexTree->Branch("v.", "TPrimaryVertex", &primVtx, 64000, 99);
67 
68  // Create a TTree with secondary decay vertex info
69  TDecayVertexVec *decayVertices = new TDecayVertexVec();
70 
71  TTree *decayVertexTree = new TTree("decayVertexTree", "Secondary Decay Vertices");
72  decayVertexTree->Branch("v.", "TDecayVertexVec", &decayVertices, 64000, 99);
73 
74  DecayVertexFinder decayVertexFinder;
75 
76  StMuDstMaker *maker = new StMuDstMaker(0, 0, "", outFile.GetPrgOptions().PathToInputFile().c_str(), "st:MuDst.root", 1e9); // set up maker in read mode
77  // 0, 0 this mean read mode
78  // dir read all files in this directory
79  // file bla.lis real all file in this list, if (file!="") dir is ignored
80  // filter apply filter to filenames, multiple filters are separated by ':'
81  // 10 maximum number of file to read
82 
83  // Disable all branches
84  maker->SetStatus("*", 0);
85 
86  std::vector<std::string> activeBranchNames = {
87  "MuEvent",
88  "PrimaryVertices",
89  "PrimaryTracks",
90  "GlobalTracks",
91  "BTofHit",
92  "BTofHeader",
93  "StStMuMcVertex",
94  "StStMuMcTrack"
95  };
96 
97  // Enable selected branches
98  for (const auto& branchName : activeBranchNames)
99  maker->SetStatus(branchName.c_str(), 1);
100 
101  TChain *tree = maker->chain();
102  unsigned int nentries = tree->GetEntries();
103  unsigned int nEventsUser = outFile.GetPrgOptions().GetMaxEventsUser();
104  unsigned int nevent = nEventsUser > 0 ? std::min(nEventsUser, nentries) : nentries;
105  std::cout << nentries << " events in chain, " << nevent << " will be read." << std::endl;
106  tree->SetCacheSize(-1); //by setting the read cache to -1 we set it to the AutoFlush value when writing
107  tree->SetCacheLearnEntries(1); //one entry is sufficient to learn
108  tree->SetCacheEntryRange(0, nevent);
109 
110 
111  // Keep track of the number of events with 0 reconstructed verticies
112  int nEventsNoRecoVertex = 0;
113 
114  // Main loop over events
115  for (int iEvent = 0; iEvent < nevent; iEvent++)
116  {
117  if (maker->Make()) break;
118 
119  if ( SkipCurrentEvent(*maker) ) continue;
120 
121  StMuDst *muDst = maker->muDst(); // get a pointer to the StMuDst class, the class that points to all the data
122  StMuEvent *muEvent = muDst->event(); // get a pointer to the class holding event-wise information
123 
124  // Identify secondary decay vertices and put them in the container
125  decayVertexFinder.Find(*muDst, decayVertices->mVertices);
126 
127  TClonesArray *primaryVertices = muDst->primaryVertices();
128  int nPrimaryVertices = primaryVertices->GetEntriesFast();
129 
130  if (nPrimaryVertices == 0) nEventsNoRecoVertex++;
131 
132  TClonesArray *MuMcVertices = muDst->mcArray(0);
133  int nMcVertices = MuMcVertices->GetEntriesFast();
134 
135  int maxMultiplicity = 0;
136  double maxRank = -DBL_MAX;
137  // Pointer to the max rank vertex
138  StMuPrimaryVertex *maxRankVertex = nullptr;
139 
140  // Loop over primary verticies in the event
141  for (int iVertex = 0; iVertex < nPrimaryVertices; iVertex++)
142  {
143  StMuPrimaryVertex *recoVertex = (StMuPrimaryVertex *) primaryVertices->UncheckedAt(iVertex);
144 
145  if (!recoVertex) continue;
146 
147  // Find vertex with the highest track multiplicity
148  if (recoVertex->noTracks() > maxMultiplicity) {
149  maxMultiplicity = recoVertex->noTracks();
150  }
151 
152  // Find vertex with the highest rank
153  if (recoVertex->ranking() > maxRank) {
154  maxRank = recoVertex->ranking();
155  maxRankVertex = recoVertex;
156  }
157 
159  int idTruth = recoVertex->idTruth();
160 
161  // Proceed only if idTruth == 1, i.e. we reconstructed a candidate for the primary vertex
162  if (idTruth != 1) continue;
163 
164  StMuMcVertex *mcVertex = (idTruth > 0 && idTruth <= nMcVertices) ? (StMuMcVertex *) MuMcVertices->UncheckedAt(idTruth - 1) : nullptr;
165 
166  float zVpd = (muDst->btofHeader() ? muDst->btofHeader()->vpdVz(): 999.);
167  bool isMaxMult = (recoVertex->noTracks() == maxMultiplicity);
168 
169  primVtx->Set(*recoVertex, mcVertex, isMaxMult, zVpd);
170  primVertexTree->Fill();
171 
172  bool hasPxlTrack = checkVertexHasPxlHit(iVertex, *muDst);
173 
174  if (hasPxlTrack)
175  outFile.FillHistsHftTracks(*recoVertex, mcVertex);
176 
177  outFile.FillHists(*recoVertex, mcVertex);
178  outFile.FillHists(*recoVertex, decayVertices->mVertices);
179  }
180 
181  // Consider vertex with maximum rank and its simulated counterpart if available
182  if (maxRankVertex)
183  {
184  int idTruth = maxRankVertex->idTruth();
185  StMuMcVertex* mcVertex = (idTruth > 0 && idTruth <= nMcVertices) ?
186  (StMuMcVertex *) MuMcVertices->UncheckedAt(idTruth - 1) : nullptr;
187 
188  // Fill vertex hist container for max rank vertex
189  outFile.FillHistsMaxRank(*maxRankVertex, mcVertex);
190  }
191 
192  outFile.FillHists(*muDst);
193 
194  // Also add primary struct to the saved struct
195  decayVertices->mPrimaryVertex = *primVtx;
196  // Save the tree with secondary decay vertices
197  decayVertexTree->Fill();
198  }
199 
200  outFile.Finalize();
201 
202  std::cout << "Number of events: " << nevent
203  << ", with 0 reconstructed verticies: " << nEventsNoRecoVertex << std::endl;
204 }
205 
206 
211 bool checkVertexHasPxlHit(int vertexIndex, const StMuDst& stMuDst)
212 {
213  stMuDst.setVertexIndex(vertexIndex);
214 
215  TObjArray *primaryTracks = stMuDst.primaryTracks();
216 
217  if (!primaryTracks)
218  return false;
219 
220  int nPrimaryTracks = primaryTracks->GetEntriesFast();
221 
222  for (int i=0; i<nPrimaryTracks; i++)
223  {
224  StMuTrack *stTrack = static_cast<StMuTrack*>(primaryTracks->UncheckedAt(i));
225  StTrackTopologyMap trackHitMap = stTrack->topologyMap();
226 
227  if ( trackHitMap.hasHitInPxlLayer(1) || trackHitMap.hasHitInPxlLayer(2) || trackHitMap.hasHitInPxlLayer(3) )
228  return true;
229  }
230 
231  return false;
232 }
233 
234 
235 
236 bool SkipCurrentEvent(const StMuDstMaker& maker)
237 {
238  static std::string prev_file_name("");
239  static std::string curr_file_name("");
240  static int curr_event_index = 0;
241 
242  curr_file_name = maker.GetFileName();
243 
244  if (curr_file_name != prev_file_name) {
245  std::cout << "Processing new file: " << curr_file_name << std::endl;
246  prev_file_name = curr_file_name;
247  curr_event_index = 0;
248  }
249 
250  // Do not process the first two events from each file
251  if (curr_event_index < 2) {
252  std::cout << "Skipping this event index: " << curr_event_index << std::endl;
253  curr_event_index++;
254  return true;
255  }
256 
257  return false;
258 }
259 
260 
261 vtxeval::VectorMcTracks vtxeval::getMcTracksMatchingMcVertex(const StMuDst& stMuDst, const StMuMcVertex* mcVertex)
262 {
263  std::vector<const StMuMcTrack*> mcTracks;
264 
265  if (!mcVertex) return mcTracks;
266 
267  TClonesArray *muMcTracks = stMuDst.mcArray(1);
268  int nMuMcTracks = muMcTracks->GetEntriesFast();
269 
270  for (int i = 0; i < nMuMcTracks; i++)
271  {
272  StMuMcTrack *mcTrack = static_cast<StMuMcTrack*>( muMcTracks->UncheckedAt(i) );
273 
274  if ( !mcTrack || mcTrack->IdVx() != mcVertex->Id() ) continue;
275 
276  mcTracks.push_back(mcTrack);
277  }
278 
279  return mcTracks;
280 }
vtxeval::VectorMcTracks getMcTracksMatchingMcVertex(const StMuDst &stMuDst, const StMuMcVertex *mcVertex)
Definition: vtxhist.cc:261
bool SkipCurrentEvent(const StMuDstMaker &maker)
Definition: vtxhist.cc:236
int main(int argc, char **argv)
Definition: vtxhist.cc:41
void FillHistsMaxRank(const StMuPrimaryVertex &vertex, const StMuMcVertex *mcVertex=nullptr)
bool checkVertexHasPxlHit(int vertexIndex, const StMuDst &stMuDst)
Returns true if at least one PXL hit is on a track pointing to vertex with vertexIndex.
Definition: vtxhist.cc:211
void FillHistsHftTracks(const StMuPrimaryVertex &vertex, const StMuMcVertex *mcVertex=nullptr)
std::vector< const StMuMcTrack * > VectorMcTracks
Definition: utils.h:14
void process_muDst(VertexRootFile &outFile)
Definition: vtxhist.cc:60
void FillHists(const StMuDst &event)