HPS-MC
stdhep_util.cpp
Go to the documentation of this file.
1 #include <stdhep_util.hh>
2 #include <stdhep_mcfio.h>
3 #include <math.h>
4 #include <stdhep.h>
5 #include <hepev4.h>
6 #include <stdio.h>
7 
8 //hepevt hepevt_;
9 bool xdr_init_done = false;
10 bool has_hepev4 = false;
11 
12 static vector<stdhep_entry*> vect_for_Memoery_Release;
13 
14 int read_stdhep(vector<stdhep_entry> *new_event)
15 {
16  int offset = new_event->size();
17  for (int i = 0; i < hepevt_.nhep; i++)
18  {
19  struct stdhep_entry *temp = new struct stdhep_entry;
20  temp->isthep = hepevt_.isthep[i];
21  temp->idhep = hepevt_.idhep[i];
22  for (int j = 0; j < 2; j++) {
23  temp->jmohep[j] = hepevt_.jmohep[i][j];
24  if (temp->jmohep[j] != 0) temp->jmohep[j] += offset;
25  }
26  for (int j = 0; j < 2; j++) {
27  temp->jdahep[j] = hepevt_.jdahep[i][j];
28  if (temp->jdahep[j] != 0) temp->jdahep[j] += offset;
29  }
30  for (int j = 0; j < 5; j++) temp->phep[j] = hepevt_.phep[i][j];
31  for (int j = 0; j < 4; j++) temp->vhep[j] = hepevt_.vhep[i][j];
32  new_event->push_back(*temp);
33  vect_for_Memoery_Release.push_back(temp);
34  }
35  return hepevt_.nevhep;
36 }
37 
38 void read_stdhep(stdhep_event *new_event)
39 {
40  new_event->nevhep = hepevt_.nevhep;
41  int offset = new_event->particles.size();
42  for (int i = 0; i < hepevt_.nhep; i++)
43  {
44  struct stdhep_entry *temp = new struct stdhep_entry;
45  temp->isthep = hepevt_.isthep[i];
46  temp->idhep = hepevt_.idhep[i];
47  for (int j = 0; j < 2; j++) {
48  temp->jmohep[j] = hepevt_.jmohep[i][j];
49  if (temp->jmohep[j] != 0) temp->jmohep[j] += offset;
50  }
51  for (int j = 0; j < 2; j++) {
52  temp->jdahep[j] = hepevt_.jdahep[i][j];
53  if (temp->jdahep[j] != 0) temp->jdahep[j] += offset;
54  }
55  for (int j = 0; j < 5; j++) temp->phep[j] = hepevt_.phep[i][j];
56  for (int j = 0; j < 4; j++) temp->vhep[j] = hepevt_.vhep[i][j];
57  new_event->particles.push_back(*temp);
58  vect_for_Memoery_Release.push_back(temp);
59  }
60  if (has_hepev4)
61  {
62  new_event->has_hepev4 = true;
63  new_event->idruplh = hepev4_.idruplh;
64  new_event->eventweightlh = hepev4_.eventweightlh;
65  }
66  else new_event->has_hepev4 = false;
67 }
68 
69 void write_stdhep(vector<stdhep_entry> *new_event, int nevhep)
70 {
71  hepevt_.nhep = new_event->size();
72  hepevt_.nevhep = nevhep;
73  //vector<stdhep_entry>::iterator it;
74  for (int i = 0; i < new_event->size(); i++)
75  {
76  struct stdhep_entry temp = new_event->at(i);
77  hepevt_.isthep[i] = temp.isthep;
78  hepevt_.idhep[i] = temp.idhep;
79  for (int j = 0; j < 2; j++) hepevt_.jmohep[i][j] = temp.jmohep[j];
80  for (int j = 0; j < 2; j++) hepevt_.jdahep[i][j] = temp.jdahep[j];
81  for (int j = 0; j < 5; j++) hepevt_.phep[i][j] = temp.phep[j];
82  for (int j = 0; j < 4; j++) hepevt_.vhep[i][j] = temp.vhep[j];
83  }
84  has_hepev4 = false;
85  for (int i = 0; i < vect_for_Memoery_Release.size(); i++) {
86  delete vect_for_Memoery_Release[i];
87  }
89  new_event->clear();
90 }
91 
92 void write_stdhep(stdhep_event *new_event)
93 {
94  hepevt_.nhep = new_event->particles.size();
95  hepevt_.nevhep = new_event->nevhep;
96  //vector<stdhep_entry>::iterator it;
97  for (int i = 0; i< new_event->particles.size(); i++)
98  {
99  struct stdhep_entry temp = new_event->particles.at(i);
100  hepevt_.isthep[i] = temp.isthep;
101  hepevt_.idhep[i] = temp.idhep;
102  for (int j = 0; j < 2; j++) hepevt_.jmohep[i][j] = temp.jmohep[j];
103  for (int j = 0; j < 2; j++) hepevt_.jdahep[i][j] = temp.jdahep[j];
104  for (int j = 0; j < 5; j++) hepevt_.phep[i][j] = temp.phep[j];
105  for (int j = 0; j < 4; j++) hepevt_.vhep[i][j] = temp.vhep[j];
106  }
107  if (new_event->has_hepev4)
108  {
109  hepev4_.idruplh = new_event->idruplh;
110  hepev4_.eventweightlh = new_event->eventweightlh;
111  hepev4_.alphaqedlh = 0;
112  hepev4_.alphaqcdlh = 0;
113  for (int i = 0; i < 10; i++) hepev4_.scalelh[i] = 0;
114  for (int i = 0; i < new_event->particles.size(); i++)
115  {
116  for (int j = 0; j < 3; j++) hepev4_.spinlh[i][j] = 0.0;
117  for (int j = 0; j < 3; j++) hepev4_.icolorflowlh[i][j] = 0;
118  }
119  has_hepev4 = true;
120  }
121  for (int i = 0; i< vect_for_Memoery_Release.size(); i++){
122  delete vect_for_Memoery_Release[i];
123  }
124  vect_for_Memoery_Release.clear();
125  new_event->particles.clear();
126 }
127 
128 void add_filler_particle(vector<stdhep_entry> *new_event)
129 {
130  struct stdhep_entry *temp = new struct stdhep_entry;
131  temp->isthep = 0;
132  temp->idhep = 22;
133  for (int j = 0; j < 2; j++) temp->jmohep[j] = 0;
134  for (int j = 0; j < 2; j++) temp->jdahep[j] = 0;
135  for (int j = 0; j < 5; j++) temp->phep[j] = 0.0;
136  temp->phep[0] += 0.1*sin(0.0305); //30.5 mrad in +x direction
137  temp->phep[2] += 0.1*cos(0.0305);
138  temp->phep[3] += 0.1;
139  for (int j = 0; j < 4; j++) temp->vhep[j] = 0.0;
140  temp->vhep[2]+=0.1; //0.1 mm after target
141  new_event->push_back(*temp);
142  vect_for_Memoery_Release.push_back(temp);
143 }
144 
145 int append_stdhep(vector<stdhep_entry> *event, const vector<stdhep_entry> *new_event)
146 {
147  int offset = event->size();
148  for (int i = 0; i < new_event->size(); i++)
149  {
150  struct stdhep_entry temp = (*new_event)[i];
151  for (int j = 0; j < 2; j++) {
152  if (temp.jmohep[j] != 0) temp.jmohep[j] += offset;
153  }
154  for (int j = 0; j < 2; j++) {
155  if (temp.jdahep[j] != 0) temp.jdahep[j] += offset;
156  }
157  event->push_back(temp);
158  }
159  return event->size();
160 }
161 
162 int open_read(char *filename, int istream, int n_events)
163 {
164  printf("Reading from %s; expecting %d events\n", filename, n_events);
165  if (xdr_init_done)
166  StdHepXdrReadOpenNTries(filename, &n_events, istream);
167  else
168  {
169  StdHepXdrReadInitNTries(filename, &n_events, istream);
170  xdr_init_done = true;
171  }
172  return n_events;
173 }
174 
175 void open_write(char *filename, int ostream, int n_events)
176 {
177  printf("Writing to %s; expecting %d events\n", filename, n_events);
178  if (xdr_init_done)
179  StdHepXdrWriteOpen(filename, filename, n_events, ostream);
180  else
181  {
182  StdHepXdrWriteInit(filename, filename, n_events, ostream);
183  xdr_init_done = true;
184  }
185  StdHepXdrWrite(100,ostream);
186 }
187 
188 bool read_next(int istream)
189 {
190  int ilbl;
191  bool read_ok;
192  do {
193  if (StdHepXdrRead(&ilbl, istream) != 0) {
194  printf("End of file\n");
195  return false;
196  }
197  if (ilbl == 1 || ilbl == 4) read_ok = true;
198  else
199  {
200  read_ok = false;
201  printf("ilbl = %d\n", ilbl);
202  }
203  has_hepev4 = (ilbl == 4);
204  } while (!read_ok);
205  return true;
206 }
207 
208 void close_write(int ostream)
209 {
210  StdHepXdrWrite(200, ostream);
211  StdHepXdrEnd(ostream);
212 }
213 
214 void write_file(int ostream)
215 {
216  if (has_hepev4)
217  StdHepXdrWrite(4, ostream);
218  else
219  StdHepXdrWrite(1, ostream);
220  has_hepev4 = false;
221 }
222 
223 void close_read(int istream)
224 {
225  StdHepXdrEnd(istream);
226 }
227 
229 {
230  printf("isthep = %d\tidhep = %d\t", entry.isthep, entry.idhep);
231  for (int i = 0; i < 2; i++) printf("jmohep[%d] = %d\t", i, entry.jmohep[i]);
232  // printf("\n");
233  for (int i = 0; i < 2; i++) printf("jdahep[%d] = %d\t", i, entry.jdahep[i]);
234  printf("\n");
235  for (int i = 0; i < 5; i++) printf("phep[%d] = %f\t", i, entry.phep[i]);
236  printf("\n");
237  for (int i=0;i<4;i++) printf("vhep[%d] = %f\t",i,entry.vhep[i]);
238  printf("\n");
239 }
int StdHepXdrWriteInit(char *filename, char *title, int ntries, int ist)
int StdHepXdrWrite(int ilbl, int ist)
int StdHepXdrRead(int *ilbl, int ist)
void StdHepXdrEnd(int ist)
int StdHepXdrReadInitNTries(char *filename, int *ntries, int ist)
int StdHepXdrWriteOpen(char *filename, char *title, int ntries, int ist)
int StdHepXdrReadOpenNTries(char *filename, int *ntries, int ist)
void add_filler_particle(vector< stdhep_entry > *new_event)
bool has_hepev4
Definition: stdhep_util.cpp:10
static vector< stdhep_entry * > vect_for_Memoery_Release
Definition: stdhep_util.cpp:12
void open_write(char *filename, int ostream, int n_events)
bool xdr_init_done
Definition: stdhep_util.cpp:9
int open_read(char *filename, int istream, int n_events)
int append_stdhep(vector< stdhep_entry > *event, const vector< stdhep_entry > *new_event)
void write_stdhep(vector< stdhep_entry > *new_event, int nevhep)
Definition: stdhep_util.cpp:69
int read_stdhep(vector< stdhep_entry > *new_event)
Definition: stdhep_util.cpp:14
bool read_next(int istream)
void close_read(int istream)
void print_entry(stdhep_entry entry)
void write_file(int ostream)
void close_write(int ostream)
Definition: stdhep_util.hh:7
int idhep
Definition: stdhep_util.hh:9
double vhep[4]
Definition: stdhep_util.hh:13
int jmohep[2]
Definition: stdhep_util.hh:10
double phep[5]
Definition: stdhep_util.hh:12
int isthep
Definition: stdhep_util.hh:8
int jdahep[2]
Definition: stdhep_util.hh:11
vector< stdhep_entry > particles
Definition: stdhep_util.hh:19
double eventweightlh
Definition: stdhep_util.hh:23