HPS-MC
lhe_tridents_displacetime.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 == 622) {
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 += event->at(i).phep[j]*event->at(i).phep[j];
63  p = sqrt(p);
64  double proper_time = gsl_ran_exponential(r, decay_length);
65  double distance = gamma*beta*proper_time;
66  for (int j = 0; j < 3; j++) vx[j] = distance * event->at(i).phep[j]/p;
67  vx[3] = gamma*proper_time;
68  //printf("gamma=%f p=%f distance=%f vx=%f,%f,%f\n",gamma,p,distance,vx[0],vx[1],vx[2]);
69  }
70  }
71  if (ap_id < 0) printf("no A' found\n");
72  else for (int i = 0; i < event->size(); i++) {
73  if (event->at(i).jmohep[0] == ap_id+1 || event->at(i).jmohep[1] == ap_id+1) {
74  for (int j = 0; j < 4; j++) event->at(i).vhep[j] = vx[j];
75  }
76  }
77 }
78 
84 int main(int argc,char** argv)
85 {
86  int nevhep;
87  vector<stdhep_entry> new_event;
88 
89  int rseed = 0;
90  double decay_length = -1.0;
91  int c;
92 
93  while ((c = getopt(argc,argv,"hs:l:")) != -1)
94  switch (c)
95  {
96  case 'h':
97  printf("-h: print this help\n");
98  printf("-s: RNG seed\n");
99  printf("-l: A' decay length in mm\n");
100  return(0);
101  break;
102  case 's':
103  rseed = atoi(optarg);
104  break;
105  case 'l':
106  decay_length = atof(optarg);
107  break;
108  case '?':
109  printf("Invalid option or missing option argument; -h to list options\n");
110  return(1);
111  default:
112  abort();
113  }
114 
115  if (argc-optind < 2)
116  {
117  printf("<input stdhep filename> <output stdhep filename>\n");
118  return 1;
119  }
120 
121  FILE * in_file;
122  in_file = fopen(argv[optind],"r");
123 
124  // initialize the RNG
125  const gsl_rng_type * T;
126  gsl_rng * r;
127  gsl_rng_env_setup();
128 
129  T = gsl_rng_mt19937;
130  r = gsl_rng_alloc (T);
131  gsl_rng_set(r, rseed);
132 
133  int ostream = 0;
134 
135  open_write(argv[optind+1], ostream, 1000);
136  nevhep = 1;
137 
138  printf("Applying decay length of %f mm\n", decay_length > 0 ? decay_length : 0.0);
139 
140  while (true) {
141  char line[1000];
142  bool found_event = false;
143  while (fgets(line, 1000, in_file) != NULL) {
144  if (strstr(line, "<event") != NULL) {
145  found_event = true;
146  break;
147  }
148  }
149  if (!found_event) {
150  fclose(in_file);
151  close_write(ostream);
152  return(0);
153  }
154 
155  int nup;
156 
157  fgets(line, 1000, in_file);
158  sscanf(line, "%d %*d %*f %*f %*f %*f", &nup);
159  for (int i = 0; i < nup; i++) {
160  struct stdhep_entry *temp = new struct stdhep_entry;
161  fgets(line, 1000, in_file);
162  int icolup0, icolup1;
163  double phep0 = 100.0;
164  char blah[1000];
165  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]));
166  //int status = sscanf(line,"%d %d %d %d %*d %*d %s",&(temp->idhep),&istup,&(temp->jmohep[0]),&(temp->jmohep[1]),blah);
167  switch (temp->isthep) {
168  case 1:
169  case 2:
170  break;
171  case -1:
172  temp->isthep = 3;
173  break;
174  default:
175  temp->isthep = 0;
176  }
177  switch (temp->idhep) {
178  case 611:
179  temp->idhep = 11;
180  break;
181  case -611:
182  temp->idhep = -11;
183  break;
184  }
185  for (int j = 0; j < 4; j++) temp->vhep[j] = 0.0;
186  for (int j = 0; j < 2; j++) temp->jdahep[j] = 0;
187  new_event.push_back(*temp);
188  }
189  remove_nulls(&new_event);
190  set_jdahep(&new_event);
191  if (decay_length>0) displace_vertex(&new_event,r,decay_length);
192  write_stdhep(&new_event,nevhep);
193  write_file(ostream);
194  nevhep++;
195  }
196 }
197 
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