HPS-MC
lhe_tridents.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; /* The event number */
87  stdhep_event 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 
123  in_file = fopen(argv[optind], "r");
124 
125  // initialize the RNG
126  const gsl_rng_type * T;
127  gsl_rng * r;
128  gsl_rng_env_setup();
129 
130  T = gsl_rng_mt19937;
131  r = gsl_rng_alloc (T);
132  gsl_rng_set(r, rseed);
133 
134  int ostream = 0;
135 
136  open_write(argv[optind+1], ostream, 1000);
137  nevhep = 1;
138 
139  printf("Applying decay length of %f mm\n", decay_length > 0 ? decay_length : 0.0);
140 
141  while (true) {
142  char line[1000];
143  bool found_event = false;
144  while (fgets(line, 1000, in_file) != NULL) {
145  if (strstr(line, "<event") != NULL) {
146  found_event = true;
147  break;
148  }
149  }
150  if (!found_event) {
151  fclose(in_file);
152  close_write(ostream);
153  return(0);
154  }
155 
156  int nup;
157 
158  fgets(line, 1000, in_file);
159  sscanf(line, "%d %d %lf %*f %*f %*f", &nup, &new_event.idruplh, &new_event.eventweightlh);
160 
161  for (int i = 0; i < nup; i++) {
162  struct stdhep_entry *temp = new struct stdhep_entry;
163  fgets(line, 1000, in_file);
164  //int icolup0,icolup1;
165  //double phep0 = 100.0;
166  char blah[1000];
167  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]));
168  //int status = sscanf(line,"%d %d %d %d %*d %*d %s",&(temp->idhep),&istup,&(temp->jmohep[0]),&(temp->jmohep[1]),blah);
169  switch (temp->isthep) {
170  case 1:
171  case 2:
172  break;
173  case -1:
174  temp->isthep = 3;
175  break;
176  default:
177  temp->isthep = 0;
178  }
179  switch (temp->idhep) {
180  case 611:
181  temp->idhep = 11;
182  break;
183  case -611:
184  temp->idhep = -11;
185  break;
186  }
187  for (int j = 0; j < 4; j++) temp->vhep[j] = 0.0;
188  for (int j = 0; j < 2; j++) temp->jdahep[j] = 0;
189  new_event.particles.push_back(*temp);
190  }
191  remove_nulls(&new_event.particles);
192  set_jdahep(&new_event.particles);
193  if (decay_length > 0) displace_vertex(&new_event.particles, r ,decay_length);
194  new_event.nevhep = nevhep;
195  new_event.has_hepev4 = true;
196  write_stdhep(&new_event);
197  write_file(ostream);
198  nevhep++;
199  }
200 }
201 
int main(int argc, char **argv)
Definition: lhe_tridents.cc:84
void set_jdahep(vector< stdhep_entry > *event)
Definition: lhe_tridents.cc:33
void remove_nulls(vector< stdhep_entry > *event)
Definition: lhe_tridents.cc:12
void displace_vertex(vector< stdhep_entry > *event, gsl_rng *r, double decay_length)
Definition: lhe_tridents.cc:48
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
vector< stdhep_entry > particles
Definition: stdhep_util.hh:19
double eventweightlh
Definition: stdhep_util.hh:23