HPS-MC
add_mother_full_truth.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <stdhep_util.hh>
6 
7 #include <unistd.h>
8 #include <iostream>
9 using namespace std;
10 
16 int main(int argc,char** argv)
17 {
18  int nevhep;
19  vector<stdhep_entry> new_event;
20 
21  int id_beam = 623;
22  int id_pair = 622;
23 
24  double mass = 0.1;
25  double energy = 0.1;
26 
27  int c;
28 
29  while ((c = getopt(argc,argv,"hi1:i2:")) != -1)
30  switch (c)
31  {
32  case 'h':
33  printf("-h: print this help\n");
34  printf("-i: PDG ID of mother\n");
35  return(0);
36  break;
37  case 'i1':
38  id_beam = atoi(optarg);
39  break;
40  case 'i2':
41  id_pair = atoi(optarg);
42  break;
43  case '?':
44  printf("Invalid option or missing option argument; -h to list options\n");
45  return(1);
46  default:
47  abort();
48  }
49 
50  if (argc-optind < 3)
51  {
52  printf("<input stdhep filename> <input lhe filename> <output stdhep filename>\n");
53  return 1;
54  }
55 
56  int n_events;
57  int istream = 0;
58  int ostream = 1;
59 
60  n_events = open_read(argv[optind], istream);
61 
62  open_write(argv[optind+2], ostream, n_events);
63 
64  FILE * in_file = fopen(argv[optind+1], "r");
65 
66  while (true) {
68  if (!read_next(istream)) {
69  close_read(istream);
70  fclose(in_file);
71  close_write(ostream);
72  return(0);
73  }
74 
81  struct stdhep_entry *temp1 = new struct stdhep_entry;
82  temp1->isthep = 3;
83  temp1->idhep = id_beam;
84  for (int j = 0; j < 2; j++) temp1->jmohep[j] = 0;
85  for (int j = 0; j < 2; j++) temp1->jdahep[j] = 0;
86  for (int j = 0; j < 5; j++) temp1->phep[j] = 0.0;
87  temp1->phep[3] += energy;
88  temp1->phep[4] += mass;
89  for (int j = 0; j < 4; j++) temp1->vhep[j] = 0.0;
90  new_event.push_back(*temp1);
91 
92  struct stdhep_entry *temp2 = new struct stdhep_entry;
93  temp2->isthep = 2;
94  temp2->idhep = id_pair;
95  for (int j = 0; j < 2; j++) temp2->jmohep[j] = 0;
96  for (int j = 0; j < 2; j++) temp2->jdahep[j] = 0;
97  for (int j = 0; j < 5; j++) temp2->phep[j] = 0.0;
98  temp2->phep[3] += energy;
99  temp2->phep[4] += mass;
100  for (int j = 0; j < 4; j++) temp2->vhep[j] = 0.0;
101  new_event.push_back(*temp2);
102 
103  nevhep = read_stdhep(&new_event);
104  int offset = 2;
105 
107  char line[1000];
108  bool found_event = false;
109  while (fgets(line, 1000, in_file) != NULL) {
110  if (strstr(line, "<event") != NULL) {
111  found_event = true;
112  break;
113  }
114  }
115  if (!found_event) {
116  close_read(istream);
117  fclose(in_file);
118  close_write(ostream);
119  return(0);
120  }
121 
122  struct stdhep_entry *temp3 = new struct stdhep_entry;
123  struct stdhep_entry *temp4 = new struct stdhep_entry;
124  int nup;
125  fgets(line, 1000, in_file);
126  sscanf(line, "%d %*d %*f %*f %*f %*f", &nup);
127  for (int i = 0; i < nup; i++) {
128  struct stdhep_entry *temp = new struct stdhep_entry;
129  fgets(line, 1000, in_file);
130  int icolup0, icolup1;
131  double phep0 = 100.0;
132  char blah[1000];
133  sscanf(line, "%d %d %d %d %*d %*d %lf %lf %lf %lf %lf %*f %*f", &(temp->idhep), &(temp->isthep), &(temp->jmohep[0]), &(temp->jmohep[1]), &(temp->phep[0]), &(temp->phep[1]), &(temp->phep[2]), &(temp->phep[3]), &(temp->phep[4]));
134 
135  // nup == 6 && temp->idhep == 11 for electron/pair of rad
136  // nup == 7 && temp->idhep == 611 for electron/pair of ap
137  if((nup == 6 && temp->idhep == 11) || (nup == 7 && temp->idhep == 611)) temp3 = temp;
138  // nup == 6 && temp->idhep == -11 for positron/pair of rad
139  // nup == 7 && temp->idhep == -611 for positron/pair of ap
140  else if((nup == 6 && temp->idhep == -11) || (nup == 7 && temp->idhep == -611)) temp4 = temp;
141  else delete temp;
142  }
143 
145  for (int i = 2; i < new_event.size(); i++) {
146  if (new_event[i].jmohep[0] == 1 + offset) {
147  new_event[i].jmohep[0] = 2;
148  if (new_event[1].jdahep[0] == 0) {
149  new_event[1].jdahep[0] = i+1;
150  for (int j = 0; j < 4; j++) new_event[1].vhep[j] = new_event[i].vhep[j];
151  }
152  new_event[1].phep[0] = temp3->phep[0] + temp4->phep[0];
153  new_event[1].phep[1] = temp3->phep[1] + temp4->phep[1];
154  new_event[1].phep[2] = temp3->phep[2] + temp4->phep[2];
155  new_event[1].phep[3] = temp3->phep[3] + temp4->phep[3];
156  new_event[1].phep[4] = sqrt(pow(new_event[1].phep[3], 2) - pow(new_event[1].phep[2], 2) - pow(new_event[1].phep[1], 2) - pow(new_event[1].phep[0], 2));
157 
158  new_event[1].jdahep[1] = i+1;
159  }
160  else if (new_event[i].jmohep[0] == 0) {
161  new_event[i].jmohep[0] = 1;
162  if (new_event[0].jdahep[0] == 0) new_event[0].jdahep[0] = i+1;
163  new_event[0].jdahep[1] = i+1;
164  }
165  }
166 
167  write_stdhep(&new_event, nevhep);
168  write_file(ostream);
169  nevhep++;
170 
171  delete temp1;
172  delete temp2;
173  delete temp3;
174  delete temp4;
175  }
176 }
177 
int main(int argc, char **argv)
void open_write(char *filename, int ostream, int n_events)
int open_read(char *filename, int istream, int n_events)
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 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