Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simulateTimestamps.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file simulateTimestamps.C
1 #include <TTree.h>
2 #include <TFile.h>
3 #include <gsl/gsl_randist.h>
4 #include <gsl/gsl_rng.h>
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 // cppcheck-suppress unknownMacro
10 R__LOAD_LIBRARY(libgslcblas.so)
11 R__LOAD_LIBRARY(libgsl.so)
12 
13 //_______________________________________________________________
15 {
16 
17  // random generator
18  auto rng = gsl_rng_alloc(gsl_rng_mt19937);
19  //gsl_rng_set( rng, time(0) );//time(0) is the seed
20  gsl_rng_set( rng, 1 );
21 
22  // time interval (s) between two bunch crossing
23  /* value copied from generators/phhepmc/Fun4AllHepMCPileupInputManager.cc */
24  static constexpr double deltat_crossing = 106e-9;
25 
26  // triggered rate (Hz)
27  // static constexpr double trigger_rate = 1e5; // Hijing 100kHz
28  static constexpr double trigger_rate = 5e4; // Hijing 50kHz
29  //static constexpr double trigger_rate = 3e6; // pp collisions
30 
31  // mean number of collision per crossing
32  static constexpr double mu = trigger_rate*deltat_crossing;
33 
34  // print configuration
35  printf("SimulateCollisions - deltat_crossing: %f \n", deltat_crossing );
36  printf("SimulateCollisions - trigger_rate: %f \n" , trigger_rate);
37  printf("SimulateCollisions - mu: %f \n" , mu);
38 
39  // number of requested triggers (should correspond approximately to 1/10 second of data taking)
40  // static constexpr uint ntrigtot = 1e4;
41  //static constexpr uint ntrigtot = 6e6;
42  static constexpr uint ntrigtot = 1e6;
43  // write all timestamps to a file
44  FILE *fptr;
45  fptr = fopen("./data/timestamps_50kHz_1M.txt","w");
46  fprintf(fptr,"// bunchcrossin id; time (ns)\n");
47  fprintf(fptr,"// assuming 106ns between bunches\n" );
48 
49 
50  // running collision time
51  int64_t bunchcrossing = 0;
52  float time = 0;
53  float timeD = 0;
54  TTree *timestamps = new TTree("timestamps","beamcrossings timestamps");
55  timestamps -> Branch("bc", &bunchcrossing, "bc/I");
56  timestamps -> Branch("t", &time, "t/F");
57  timestamps -> Branch("dt", &timeD, "dt/F");
58 
59  // keep track of the last collision time
60  double previous_trigger_time = 0;
61 
62  // generate triggers
63  for( int itrig = 0; itrig < ntrigtot; )
64  {
65  ++ bunchcrossing;
66  time += deltat_crossing;
67 
68  auto ntrig = gsl_ran_poisson( rng, mu );
69  for( uint i = 0; i < ntrig; ++i )
70  {
71  fprintf(fptr,"%d %d\n",int(bunchcrossing), int(time*1e9) );
72  timeD = (time-previous_trigger_time)*1e9;
73  timestamps ->Fill();
74  previous_trigger_time = time;
75  ++itrig;
76  if( itrig%100000==0 ) printf("SimulateCollisions - itrig: %d \n", itrig );
77  }
78  }
79 
80  // printout last trigger time
81  printf("SimulateCollisions - last trigger time: %f \n", time );
82 
83  fclose(fptr);
84  //out.close();
85  gsl_rng_free(rng);
86 
87  TFile *outf = new TFile("./data/timestamps_50kHz_1M_t.root","recreate");
88  timestamps -> Write();
89 
90  outf -> Write();
91  outf -> Close();
92 
93 }