Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KFPInputData.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KFPInputData.h
1 /*
2  * This file is part of KFParticle package
3  * Copyright (C) 2007-2019 FIAS Frankfurt Institute for Advanced Studies
4  * 2007-2019 Goethe University of Frankfurt
5  * 2007-2019 Ivan Kisel <I.Kisel@compeng.uni-frankfurt.de>
6  * 2007-2019 Maksym Zyzak
7  *
8  * KFParticle is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * KFParticle is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef KFPINPUTDATA_H
23 #define KFPINPUTDATA_H
24 
25 #include "KFPTrackVector.h"
26 #include "KFParticle.h"
27 
28 #include <vector>
29 #include <string>
30 #include <fstream>
31 
46 {
47  int fIndex;
48  int fPdg;
49 
50  static bool Compare(const KFPTrackIndex& a, const KFPTrackIndex& b)
51  {
59  int pdg1 = a.fPdg == -1 ? 250 : a.fPdg;
60  int pdg2 = b.fPdg == -1 ? 250 : b.fPdg;
61 
62  return (abs(pdg1) < abs(pdg2));
63  }
64 };
65 
66 
78 {
79  public:
80 
81  void *operator new(size_t size) { return _mm_malloc(size, sizeof(float_v)); }
82  void *operator new[](size_t size) { return _mm_malloc(size, sizeof(float_v)); }
83  void *operator new(size_t size, void *ptr) { return ::operator new(size, ptr);}
84  void *operator new[](size_t size, void *ptr) { return ::operator new(size, ptr);}
85  void operator delete(void *ptr, size_t) { _mm_free(ptr); }
86  void operator delete[](void *ptr, size_t) { _mm_free(ptr); }
87 
88  KFPInputData():fPV(0),fBz(0.f) {};
90 
92  {
96  std::ifstream ifile(prefix.data());
97  if ( !ifile.is_open() ) return 0;
98  int nSets;
99  ifile >> fBz;
100  ifile >> nSets;
101  for(int iSet=0; iSet<nSets; iSet++)
102  {
103  int nTracks = 0;
104  ifile >> nTracks;
105  fTracks[iSet].Resize(nTracks);
106 
107  for(int iP=0; iP<6; iP++)
108  {
109  float value;
110  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
111  {
112  ifile >> value;
113  fTracks[iSet].SetParameter(value, iP, iTr);
114  }
115  }
116 
117  for(int iC=0; iC<21; iC++)
118  {
119  float value;
120  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
121  {
122  ifile >> value;
123  fTracks[iSet].SetCovariance(value, iC, iTr);
124  }
125  }
126 
127  int tmpInt;
128  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
129  {
130  ifile >> tmpInt;
131  fTracks[iSet].SetId(tmpInt, iTr);
132  }
133 
134  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
135  {
136  ifile >> tmpInt;
137  fTracks[iSet].SetPDG(tmpInt, iTr);
138  }
139 
140  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
141  {
142  ifile >> tmpInt;
143  fTracks[iSet].SetQ(tmpInt, iTr);
144  }
145 
146  for(int iTr=0; iTr<fTracks[iSet].Size(); iTr++)
147  {
148  ifile >> tmpInt;
149  fTracks[iSet].SetPVIndex(tmpInt, iTr);
150  }
151 
152  ifile >> tmpInt;
153  fTracks[iSet].SetLastElectron(tmpInt);
154  ifile >> tmpInt;
155  fTracks[iSet].SetLastMuon (tmpInt);
156  ifile >> tmpInt;
157  fTracks[iSet].SetLastPion (tmpInt);
158  ifile >> tmpInt;
159  fTracks[iSet].SetLastKaon (tmpInt);
160  ifile >> tmpInt;
161  fTracks[iSet].SetLastProton (tmpInt);
162  }
163 
164  int nPV;
165  ifile>>nPV;
166  fPV.resize(nPV);
167  for(unsigned int iPV=0; iPV < fPV.size(); iPV++)
168  {
169  for(int iP=0; iP<3; iP++)
170  ifile >> fPV[iPV].Parameter(iP);
171 
172  for(int iC=0; iC<6; iC++)
173  ifile >> fPV[iPV].Covariance(iC);
174  }
175 
176  ifile.close();
177  return 1;
178  }
179 
180  void SetDataToVector(int* data, int& dataSize)
181  {
186  dataSize = NInputSets + 1 + 1; //sizes of the track vectors and pv vector, and field
187  for(int iSet=0; iSet<NInputSets; iSet++)
188  dataSize += fTracks[iSet].DataSize();
189  dataSize += fPV.size() * 9;
190 
191  for(int iSet=0; iSet<NInputSets; iSet++)
192  data[iSet] = fTracks[iSet].Size();
193  data[NInputSets] = fPV.size();
194 
195  float& field = reinterpret_cast<float&>(data[NInputSets+1]);
196  field = fBz;
197 
198  int offset = NInputSets+2;
199 
200  for(int iSet=0; iSet<NInputSets; iSet++)
201  fTracks[iSet].SetDataToVector(data, offset);
202 
203  for(int iP=0; iP<3; iP++)
204  {
205  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
206  {
207  float& tmpFloat = reinterpret_cast<float&>(data[offset + iPV]);
208  tmpFloat = fPV[iPV].Parameter(iP);
209  }
210  offset += fPV.size();
211  }
212 
213  for(int iC=0; iC<6; iC++)
214  {
215  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
216  {
217  float& tmpFloat = reinterpret_cast<float&>(data[offset + iPV]);
218  tmpFloat = fPV[iPV].Covariance(iC);
219  }
220  offset += fPV.size();
221  }
222  }
223 
225  {
229  int offset = NInputSets+2;
230  for(int iSet=0; iSet<NInputSets; iSet++)
231  {
232  fTracks[iSet].Resize(data[iSet]);
233  fTracks[iSet].ReadDataFromVector(data, offset);
234  }
235 
236  float& field = reinterpret_cast<float&>(data[NInputSets+1]);
237  fBz = field;
238 
239  fPV.resize(data[NInputSets]);
240 
241  for(int iP=0; iP<3; iP++)
242  {
243  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
244  {
245  float& tmpFloat = reinterpret_cast<float&>(data[offset + iPV]);
246  fPV[iPV].Parameter(iP) = tmpFloat;
247  }
248  offset += fPV.size();
249  }
250 
251  for(int iC=0; iC<6; iC++)
252  {
253  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
254  {
255  float& tmpFloat = reinterpret_cast<float&>(data[offset + iPV]);
256  fPV[iPV].Covariance(iC) = tmpFloat;
257  }
258  offset += fPV.size();
259  }
260  }
261 
262  void Print()
263  {
265  for(int iSet=0; iSet<NInputSets; iSet++)
266  fTracks[iSet].Print();
267  std::cout << "N PV: " << fPV.size() << std::endl;
268 
269  std::cout << "X: ";
270  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
271  std::cout << fPV[iPV].X() <<" ";
272  std::cout << std::endl;
273  std::cout << "Y: ";
274  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
275  std::cout << fPV[iPV].Y() <<" ";
276  std::cout << std::endl;
277  std::cout << "Z: ";
278  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
279  std::cout << fPV[iPV].Z() <<" ";
280  std::cout << std::endl;
281 
282  std::cout << "Cov matrix: " << std::endl;
283  for(int iC=0; iC<6; iC++)
284  {
285  std::cout << " iC " << iC << ": ";
286  for(unsigned int iPV=0; iPV<fPV.size(); iPV++)
287  std::cout << fPV[iPV].Covariance(iC) <<" ";
288  std::cout << std::endl;
289  }
290 
291  std::cout << "Field: " << fBz << std::endl;
292  }
293 
295  float GetBz() const { return fBz; }
296  const std::vector<KFParticle>& GetPV() const { return fPV; }
297 
299  {
301  for(int i=0; i<NInputSets; i++)
302  fTracks[i] = data.fTracks[i];
303  fPV = data.fPV;
304  fBz = data.fBz;
305 
306  return *this;
307  }
309  {
311  for(int i=0; i<NInputSets; i++)
312  fTracks[i] = data.fTracks[i];
313  fPV = data.fPV;
314  fBz = data.fBz;
315  }
316 
317  protected:
329  KFPTrackVector fTracks[NInputSets]__attribute__((aligned(sizeof(float_v))));
330  std::vector<KFParticle> fPV;
331  float fBz;
332 } __attribute__((aligned(sizeof(float_v))));
333 
346  ~KFPInputDataArray() { if(fInput) delete [] fInput; }
347 
349 
350  private:
353 };
354 
355 
368 {
369  void *operator new(size_t size) { return _mm_malloc(size, sizeof(float_v)); }
370  void *operator new[](size_t size) { return _mm_malloc(size, sizeof(float_v)); }
371  void *operator new(size_t size, void *ptr) { return ::operator new(size, ptr);}
372  void *operator new[](size_t size, void *ptr) { return ::operator new(size, ptr);}
373  void operator delete(void *ptr, size_t) { _mm_free(ptr); }
374  void operator delete[](void *ptr, size_t) { _mm_free(ptr); }
375 
376  KFPInputData data __attribute__((aligned(sizeof(float_v))));
378 } __attribute__((aligned(sizeof(float_v))));
379 
380 #endif