Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4All_single_particle.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4All_single_particle.C
2  const int nEvents = 10,
3  const char * inputFile = NULL,
4  const char * outputFile = "SvtxClusters.root",
5  const char * embed_input_file = NULL, //"Hijing_G4Hits.root",
6  const int which_tracking = 15,
7  const bool do_embedding = false,
8  const int particle_pid = 211
9  )
10 {
11  //===============
12  // Input options
13  //===============
14 
15  // Either:
16  // read previously generated g4-hits files, in this case it opens a DST and skips
17  // the simulations step completely. The G4Setup macro is only loaded to get information
18  // about the number of layers used for the cell reco code
19  //
20  // In case reading production output, please double check your G4Setup_sPHENIX.C and G4_*.C consistent with those in the production macro folder
21  // E.g. /sphenix/sim//sim01/production/2016-07-21/single_particle/spacal2d/
22  const bool readhits = false;
23  // Or:
24  // read files in HepMC format (typically output from event generators like hijing or pythia)
25  const bool readhepmc = false; // read HepMC files
26  // Or:
27  // Use pythia
28  const bool runpythia8 = false;
29  const bool runpythia6 = false;
30  // else
31  // Use particle generator (default simple generator)
32  // or gun/ very simple generator
33  const bool usegun = false;
34  // And
35  // Further choose to embed newly simulated events to a previous simulation. Not compatible with `readhits = true`
36  // In case embedding into a production output, please double check your G4Setup_sPHENIX.C and G4_*.C consistent with those in the production macro folder
37  // E.g. /sphenix/sim//sim01/production/2016-07-21/single_particle/spacal2d/
38  //const bool do_embedding = true;
39 
40  //======================
41  // What to run
42  //======================
43 
44  bool do_bbc = true;
45 
46  bool do_pipe = true;
47 
48  bool do_svtx = true;
49  bool do_svtx_cell = true;
50  bool do_svtx_cluster = true;
51  bool do_svtx_track = false;
52  bool do_svtx_eval = false;
53 
54 // if(which_tracking == 14 || which_tracking == 15) {
55 // do_svtx_track = false;
56 // do_svtx_eval = false;
57 // }
58 
59  bool do_preshower = false;
60 
61  bool do_cemc = false;
62  bool do_cemc_cell = do_cemc && true;
63  bool do_cemc_twr = do_cemc_cell && true;
64  bool do_cemc_cluster = do_cemc_twr && true;
65  bool do_cemc_eval = do_cemc_cluster && true;
66 
67  bool do_hcalin = false;
68  bool do_hcalin_cell = do_hcalin && true;
69  bool do_hcalin_twr = do_hcalin_cell && true;
70  bool do_hcalin_cluster = do_hcalin_twr && true;
71  bool do_hcalin_eval = do_hcalin_cluster && true;
72 
73  bool do_magnet = false;
74 
75  bool do_hcalout = false;
76  bool do_hcalout_cell = do_hcalout && true;
77  bool do_hcalout_twr = do_hcalout_cell && true;
78  bool do_hcalout_cluster = do_hcalout_twr && true;
79  bool do_hcalout_eval = do_hcalout_cluster && true;
80 
81  bool do_global = false;
82  bool do_global_fastsim = false;
83 
84  bool do_jet_reco = false;
85  bool do_jet_eval = false;
86 
87  bool do_dst_compress = false;
88 
89  //Option to convert DST to human command readable TTree for quick poke around the outputs
90  bool do_DSTReader = false;
91  //---------------
92  // Load libraries
93  //---------------
94 
95  gSystem->Load("libfun4all.so");
96  gSystem->Load("libg4detectors.so");
97  gSystem->Load("libphhepmc.so");
98  gSystem->Load("libg4testbench.so");
99  gSystem->Load("libg4hough.so");
100  gSystem->Load("libg4calo.so");
101  gSystem->Load("libg4eval.so");
102 
103  // establish the geometry and reconstruction setup
104  gROOT->LoadMacro("G4Setup_sPHENIX.C");
105  G4Init(do_svtx,do_preshower,do_cemc,do_hcalin,do_magnet,do_hcalout,do_pipe,which_tracking);
106 
107  int absorberactive = 1; // set to 1 to make all absorbers active volumes
108  // const string magfield = "1.5"; // if like float -> solenoidal field in T, if string use as fieldmap name (including path)
109  const string magfield = "/phenix/upgrades/decadal/fieldmaps/sPHENIX.2d.root"; // if like float -> solenoidal field in T, if string use as fieldmap name (including path)
110  const float magfield_rescale = 1.4/1.5; // scale the map to a 1.4 T field
111 
112  //---------------
113  // Fun4All server
114  //---------------
115 
117  //se->Verbosity(100);
118  // just if we set some flags somewhere in this macro
120  // By default every random number generator uses
121  // PHRandomSeed() which reads /dev/urandom to get its seed
122  // if the RANDOMSEED flag is set its value is taken as seed
123  // You ca neither set this to a random value using PHRandomSeed()
124  // which will make all seeds identical (not sure what the point of
125  // this would be:
126  // rc->set_IntFlag("RANDOMSEED",PHRandomSeed());
127  // or set it to a fixed value so you can debug your code
128  // rc->set_IntFlag("RANDOMSEED", 12345);
129 
130  //-----------------
131  // Event generation
132  //-----------------
133 
134  if (readhits)
135  {
136  // Get the hits from a file
137  // The input manager is declared later
138 
139  if (do_embedding)
140  {
141  cout <<"Do not support read hits and embed background at the same time."<<endl;
142  exit(1);
143  }
144 
145  }
146  else if (readhepmc)
147  {
148  // this module is needed to read the HepMC records into our G4 sims
149  // but only if you read HepMC input files
150  HepMCNodeReader *hr = new HepMCNodeReader();
151  se->registerSubsystem(hr);
152  }
153  else if (runpythia8)
154  {
155  gSystem->Load("libPHPythia8.so");
156 
157  PHPy8JetTrigger *theTrigger = new PHPy8JetTrigger();
158  //theTrigger->Verbosity(10);
159  theTrigger->SetEtaHighLow(-0.6, 0.6);
160  theTrigger->SetJetR(.4);
161  theTrigger->SetMinJetPt(20);
162 
163  PHPythia8* pythia8 = new PHPythia8();
164  // see coresoftware/generators/PHPythia8 for example config
165  pythia8->set_config_file("phpythia8.cfg");
166  pythia8->register_trigger(theTrigger);
167  se->registerSubsystem(pythia8);
168 
169  HepMCNodeReader *hr = new HepMCNodeReader();
170  hr->Embed(10);
171  se->registerSubsystem(hr);
172  }
173  else if (runpythia6)
174  {
175  gSystem->Load("libPHPythia6.so");
176 
177  PHPythia6 *pythia6 = new PHPythia6();
178  pythia6->set_config_file("phpythia6.cfg");
179  se->registerSubsystem(pythia6);
180 
181  HepMCNodeReader *hr = new HepMCNodeReader();
182  se->registerSubsystem(hr);
183  }
184  else
185  {
186  // toss low multiplicity dummy events
188  //gen->set_seed(1710374143);
189  // mu+,e+,proton,pi+,kaon+,Upsilon
190  gen->add_particles(particle_pid,1);
191 // gen->add_particles("pi+",5);
192 // gen->add_particles("pi-",5);
193  if (readhepmc || do_embedding)
194  {
195  gen->set_reuse_existing_vertex(true);
196  gen->set_existing_vertex_offset_vector(0.0, 0.0, 0.0);
197  }
198  else
199  {
203  gen->set_vertex_distribution_mean(0.0, 0.0, 0.0);
204  gen->set_vertex_distribution_width(0.0, 0.0, 0.0);
205  }
207  gen->set_vertex_size_parameters(0.0, 0.0);
208  gen->set_eta_range(0,0);
209  gen->set_phi_range(-1 * TMath::Pi(), 1 * TMath::Pi());
210 // gen->set_eta_range(0, 0);
211 // gen->set_phi_range(0, 0);
212  gen->set_pt_range(0.1, 5);
213  gen->Embed(10);
214  gen->Verbosity(0);
215  if (! usegun)
216  {
217  se->registerSubsystem(gen);
218  }
219  else
220  {
221  PHG4ParticleGun *gun = new PHG4ParticleGun();
222  // gun->set_name("anti_proton");
223  gun->set_name("geantino");
224  gun->set_vtx(0, 0, 0);
225  gun->set_mom(10, 0, 0.01);
226  // gun->AddParticle("geantino",1.7776,-0.4335,0.);
227  // gun->AddParticle("geantino",1.7709,-0.4598,0.);
228  // gun->AddParticle("geantino",2.5621,0.60964,0.);
229  // gun->AddParticle("geantino",1.8121,0.253,0.);
230  // se->registerSubsystem(gun);
232  pgen->set_name("geantino");
233  pgen->set_z_range(0,0);
234  pgen->set_eta_range(0.01,0.01);
235  pgen->set_mom_range(10,10);
236  pgen->set_phi_range(5.3/180.*TMath::Pi(),5.3/180.*TMath::Pi());
237  se->registerSubsystem(pgen);
238  pgen = new PHG4ParticleGenerator();
239  pgen->set_name("geantino");
240  pgen->set_z_range(0,0);
241  pgen->set_eta_range(0.01,0.01);
242  pgen->set_mom_range(10,10);
243  pgen->set_phi_range(-0.2/180.*TMath::Pi(),0.2/180.*TMath::Pi());
244  se->registerSubsystem(pgen);
245  }
246  }
247 
248  if (!readhits)
249  {
250  //---------------------
251  // Detector description
252  //---------------------
253 
254  G4Setup(absorberactive, magfield, TPythia6Decayer::kAll,
255  do_svtx, do_preshower, do_cemc, do_hcalin, do_magnet, do_hcalout, do_pipe, magfield_rescale);
256  }
257 
258  //---------
259  // BBC Reco
260  //---------
261 
262  if (do_bbc)
263  {
264  gROOT->LoadMacro("G4_Bbc.C");
265  BbcInit();
266  Bbc_Reco();
267  }
268  //------------------
269  // Detector Division
270  //------------------
271 
272  if (do_svtx_cell) Svtx_Cells();
273 
274  if (do_cemc_cell) CEMC_Cells();
275 
276  if (do_hcalin_cell) HCALInner_Cells();
277 
278  if (do_hcalout_cell) HCALOuter_Cells();
279 
280  //-----------------------------
281  // CEMC towering and clustering
282  //-----------------------------
283 
284  if (do_cemc_twr) CEMC_Towers();
285  if (do_cemc_cluster) CEMC_Clusters();
286 
287  //-----------------------------
288  // HCAL towering and clustering
289  //-----------------------------
290 
291  if (do_hcalin_twr) HCALInner_Towers();
292  if (do_hcalin_cluster) HCALInner_Clusters();
293 
294  if (do_hcalout_twr) HCALOuter_Towers();
295  if (do_hcalout_cluster) HCALOuter_Clusters();
296 
297  if (do_dst_compress) ShowerCompress();
298 
299  //--------------
300  // SVTX tracking
301  //--------------
302 
303  if(which_tracking == 14 || which_tracking == 15) {
304  if (do_svtx_cluster) Svtx_Clustering();
305  if (do_svtx_track) Svtx_Tracking();
306  } else {
307  if (do_svtx_cluster || do_svtx_track) Svtx_Reco();
308  }
309 
310  //-----------------
311  // Global Vertexing
312  //-----------------
313 
314  if (do_global)
315  {
316  gROOT->LoadMacro("G4_Global.C");
317  Global_Reco();
318  }
319 
320  else if (do_global_fastsim)
321  {
322  gROOT->LoadMacro("G4_Global.C");
323  Global_FastSim();
324  }
325 
326  //---------
327  // Jet reco
328  //---------
329 
330  if (do_jet_reco)
331  {
332  gROOT->LoadMacro("G4_Jets.C");
333  Jet_Reco();
334  }
335 
336  // HF jet trigger moudle
337  //assert (gSystem->Load("libHFJetTruthGeneration") == 0);
338  //{
339  // if (do_jet_reco)
340  // {
341  // HFJetTruthTrigger * jt = new HFJetTruthTrigger(
342  // "HFJetTruthTrigger.root", 5 , "AntiKt_Truth_r04");
343  // //jt->Verbosity(HFJetTruthTrigger::VERBOSITY_MORE);
344  // jt->set_pt_min(20);
345  // se->registerSubsystem(jt);
346  // }
347  //}
348 
349  //----------------------
350  // Simulation evaluation
351  //----------------------
352 
353  if (do_svtx_eval) Svtx_Eval("g4svtx_eval.root");
354 
355  if (do_cemc_eval) CEMC_Eval("g4cemc_eval.root");
356 
357  if (do_hcalin_eval) HCALInner_Eval("g4hcalin_eval.root");
358 
359  if (do_hcalout_eval) HCALOuter_Eval("g4hcalout_eval.root");
360 
361  if (do_jet_eval) Jet_Eval("g4jet_eval.root");
362 
363  //--------------
364  // IO management
365  //--------------
366 
367  if (readhits)
368  {
369  // Hits file
370  Fun4AllInputManager *hitsin = new Fun4AllDstInputManager("DSTin");
371  hitsin->fileopen(inputFile);
372  se->registerInputManager(hitsin);
373  }
374  if (do_embedding)
375  {
376  if (embed_input_file == NULL)
377  {
378  cout << "Missing embed_input_file! Exit";
379  exit(3);
380  }
381 
382  Fun4AllDstInputManager *in1 = new Fun4AllNoSyncDstInputManager("DSTinEmbed");
383  in1->AddFile(embed_input_file); // if one use a single input file
384  //in1->AddListFile(embed_input_file); // RecommendedL: if one use a text list of many input files
385  se->registerInputManager(in1);
386  }
387  if (readhepmc)
388  {
390  se->registerInputManager( in );
391  se->fileopen( in->Name().c_str(), inputFile );
392  }
393  else
394  {
395  // for single particle generators we just need something which drives
396  // the event loop, the Dummy Input Mgr does just that
398  se->registerInputManager( in );
399  }
400 
401  if (do_DSTReader)
402  {
403  //Convert DST to human command readable TTree for quick poke around the outputs
404  gROOT->LoadMacro("G4_DSTReader.C");
405 
406  G4DSTreader( outputFile, //
407  /*int*/ absorberactive ,
408  /*bool*/ do_svtx ,
409  /*bool*/ do_preshower ,
410  /*bool*/ do_cemc ,
411  /*bool*/ do_hcalin ,
412  /*bool*/ do_magnet ,
413  /*bool*/ do_hcalout ,
414  /*bool*/ do_cemc_twr ,
415  /*bool*/ do_hcalin_twr ,
416  /*bool*/ do_magnet ,
417  /*bool*/ do_hcalout_twr
418  );
419  }
420 
421  Fun4AllDstOutputManager *out = new Fun4AllDstOutputManager("DSTOUT", outputFile);
422  if (do_dst_compress) DstCompress(out);
423  se->registerOutputManager(out);
424 
425  //-----------------
426  // Event processing
427  //-----------------
428  if (nEvents < 0)
429  {
430  return;
431  }
432  // if we run the particle generator and use 0 it'll run forever
433  if (nEvents == 0 && !readhits && !readhepmc)
434  {
435  cout << "using 0 for number of events is a bad idea when using particle generators" << endl;
436  cout << "it will run forever, so I just return without running anything" << endl;
437  return;
438  }
439 
440  se->run(nEvents);
441 
442  //-----
443  // Exit
444  //-----
445 
446  se->End();
447 
448  //getchar();
449 
450  std::cout << "All done" << std::endl;
451  //delete se;
452  //gSystem->Exit(0);
453 }