HPS-MC
lhe_tridents_displaceuni.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 <gsl/gsl_rng.h>
8 #include <gsl/gsl_randist.h>
9 
10 #include <unistd.h>
11 
12 void remove_nulls(vector<stdhep_entry> *event)
13 {
14  int i=0;
15  while (i<event->size()) {
16  if (event->at(i).isthep == 0) {
17  event->erase(event->begin() + i);
18  for (int j = 0; j < event->size(); j++) {
19  if (event->at(j).jmohep[0] > i)
20  event->at(j).jmohep[0]--;
21  if (event->at(j).jmohep[1] > i)
22  event->at(j).jmohep[1]--;
23  if (event->at(j).jdahep[0] > i)
24  event->at(j).jdahep[0]--;
25  if (event->at(j).jdahep[1] > i)
26  event->at(j).jdahep[1]--;
27  }
28  }
29  i++;
30  }
31 }
32 
33 void set_jdahep(vector<stdhep_entry> *event)
34 {
35  for (int i=0;i<event->size();i++) {
36  event->at(i).jdahep[0] = 0;
37  event->at(i).jdahep[1] = 0;
38  for (int j = 0; j < event->size(); j++) {
39  if (event->at(j).jmohep[0] == i+1 || event->at(j).jmohep[1] == i+1) {
40  if (event->at(i).jdahep[0] == 0)
41  event->at(i).jdahep[0] = j+1;
42  event->at(i).jdahep[1] = j+1;
43  }
44  }
45  }
46 }
47 
48 void displace_vertex(vector<stdhep_entry> *event, gsl_rng *r, double decay_length)
49 {
50  int ap_id = -1;
51  double vx[4];
52  for (int i = 0; i < event->size(); i++) {
53  if (event->at(i).idhep == 625 || event->at(i).idhep == 1000023) {
54  if (ap_id != -1) {
55  printf("multiple A' found\n");
56  break;
57  }
58  ap_id = i;
59  double gamma = event->at(i).phep[3]/event->at(i).phep[4];
60  double beta = sqrt(1-pow(gamma,-2.0));
61  double p = 0.0;
62  for (int j = 0; j < 3; j++) p = sqrt(p*p + event->at(i).phep[j]*event->at(i).phep[j]);
63  double distance = decay_length*gsl_rng_uniform(r);;
64  for (int j = 0; j < 3; j++) vx[j] = distance * event->at(i).phep[j]/p;
65  vx[3] = distance/beta;
66  //printf("gamma=%f p=%f distance=%f vx=%f,%f,%f\n",gamma,p,distance,vx[0],vx[1],vx[2]);
67  }
68  }
69  if (ap_id < 0) printf("no A' found\n");
70  else for (int i = 0; i < event->size(); i++) {
71  if (event->at(i).jmohep[0] == ap_id+1 || event->at(i).jmohep[1] == ap_id+1 || event->at(i).idhep == 625 || event->at(i).idhep == 622) {
72  for (int j = 0; j < 4; j++) event->at(i).vhep[j] = vx[j];
73  }
74  }
75 }
76 
82 int main(int argc, char** argv)
83 {
84  int nevhep;
85  vector<stdhep_entry> new_event;
86 
87  int rseed = 0;
88  double decay_length = 150.0;
89  int c;
90 
91  while ((c = getopt(argc,argv,"hs:l:")) != -1)
92  switch (c)
93  {
94  case 'h':
95  printf("-h: print this help\n");
96  printf("-s: RNG seed\n");
97  printf("-l: A' decay length in mm\n");
98  return(0);
99  break;
100  case 's':
101  rseed = atoi(optarg);
102  break;
103  case 'l':
104  decay_length = atof(optarg);
105  break;
106  case '?':
107  printf("Invalid option or missing option argument; -h to list options\n");
108  return(1);
109  default:
110  abort();
111  }
112 
113  if (argc-optind < 2)
114  {
115  printf("<input stdhep filename> <output stdhep filename>\n");
116  return 1;
117  }
118 
119  FILE * in_file;
120  in_file = fopen(argv[optind],"r");
121 
122  //initialize the RNG
123  const gsl_rng_type * T;
124  gsl_rng * r;
125  gsl_rng_env_setup();
126 
127  T = gsl_rng_mt19937;
128  r = gsl_rng_alloc (T);
129  gsl_rng_set(r, rseed);
130 
131  int ostream = 0;
132 
133  open_write(argv[optind+1], ostream, 1000);
134  nevhep = 1;
135 
136  printf("Applying decay length of %f mm\n", decay_length > 0 ? decay_length : 0.0);
137 
138  while (true) {
139  char line[1000];
140  bool found_event = false;
141  while (fgets(line, 1000, in_file) != NULL) {
142  if (strstr(line, "<event") != NULL) {
143  found_event = true;
144  break;
145  }
146  }
147  if (!found_event) {
148  fclose(in_file);
149  close_write(ostream);
150  return(0);
151  }
152 
153  int nup;
154 
155  fgets(line, 1000, in_file);
156  sscanf(line, "%d %*d %*f %*f %*f %*f", &nup);
157  for (int i = 0; i < nup; i++) {
158  struct stdhep_entry *temp = new struct stdhep_entry;
159  fgets(line, 1000, in_file);
160  sscanf(line, "%d %d %d %d %*d %*d %lf %lf %lf %lf %lf %*f %*f",
161  &(temp->idhep), &(temp->isthep),
162  &(temp->jmohep[0]), &(temp->jmohep[1]),
163  &(temp->phep[0]), &(temp->phep[1]), &(temp->phep[2]), &(temp->phep[3]), &(temp->phep[4]));
164  switch (temp->isthep) {
165  case 1:
166  case 2:
167  break;
168  case -1:
169  temp->isthep = 3;
170  break;
171  default:
172  temp->isthep = 0;
173  }
174  switch (temp->idhep) {
175  case 611:
176  temp->idhep = 11;
177  break;
178  case -611:
179  temp->idhep = -11;
180  break;
181  }
182  for (int j = 0; j < 4; j++) temp->vhep[j] = 0.0;
183  for (int j = 0; j < 2; j++) temp->jdahep[j] = 0;
184  new_event.push_back(*temp);
185  }
186  remove_nulls(&new_event);
187  set_jdahep(&new_event);
188  displace_vertex(&new_event, r, decay_length);
189  write_stdhep(&new_event, nevhep);
190  write_file(ostream);
191  nevhep++;
192  }
193 }
194 
int main(int argc, char **argv)
void set_jdahep(vector< stdhep_entry > *event)
void remove_nulls(vector< stdhep_entry > *event)
void displace_vertex(vector< stdhep_entry > *event, gsl_rng *r, double decay_length)
void open_write(char *filename, int ostream, int n_events)
void write_stdhep(vector< stdhep_entry > *new_event, int nevhep)
Definition: stdhep_util.cpp:69
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