Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:12:47

0001 
0002 int Fun4All_G4_sPHENIX(
0003                const int nEvents = 10000,
0004                const char * inputFile = "/sphenix/sim//sim01/production/2016-07-21/single_particle/spacal2d/fieldmap/G4Hits_sPHENIX_e-_eta0_8GeV-0002.root",
0005                const char * outputFile = "G4sPHENIXCells.root",
0006            const char * embed_input_file = "/sphenix/sim/sim01/production/2016-07-12/sHijing/spacal2d/G4Hits_sPHENIX_sHijing-0-4.4fm.list"
0007                )
0008 {
0009 
0010   //===============
0011   // Input options
0012   //===============
0013 
0014   // Either:
0015   // read previously generated g4-hits files, in this case it opens a DST and skips
0016   // the simulations step completely. The G4Setup macro is only loaded to get information
0017   // about the number of layers used for the cell reco code
0018   //
0019   // In case reading production output, please double check your G4Setup_sPHENIX.C and G4_*.C consistent with those in the production macro folder
0020   // E.g. /sphenix/sim//sim01/production/2016-07-21/single_particle/spacal2d/
0021   const bool readhits = false;
0022   // Or:
0023   // read files in HepMC format (typically output from event generators like hijing or pythia)
0024   const bool readhepmc = false; // read HepMC files
0025   // Or:
0026   // Use particle generator
0027   const bool runpythia8 = true;
0028   const bool runpythia6 = false;
0029   // And
0030   // Further choose to embed newly simulated events to a previous simulation. Not compatible with `readhits = true`
0031   // 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
0032   // E.g. /sphenix/sim//sim01/production/2016-07-21/single_particle/spacal2d/
0033   const bool do_embedding = false;
0034 
0035   //======================
0036   // What to run
0037   //======================
0038 
0039   bool do_bbc = false;
0040 
0041   bool do_pipe = false;
0042 
0043   bool do_svtx = false;
0044   bool do_svtx_cell = false;
0045   bool do_svtx_track = false;
0046   bool do_svtx_eval = false;
0047 
0048   bool do_preshower = false;
0049 
0050   bool do_cemc = false;
0051   bool do_cemc_cell = false;
0052   bool do_cemc_twr = false;
0053   bool do_cemc_cluster = false;
0054   bool do_cemc_eval = false;
0055 
0056   bool do_hcalin = false;
0057   bool do_hcalin_cell = false;
0058   bool do_hcalin_twr = false;
0059   bool do_hcalin_cluster = false;
0060   bool do_hcalin_eval = false;
0061 
0062   bool do_magnet = false;
0063 
0064   bool do_hcalout = false;
0065   bool do_hcalout_cell = false;
0066   bool do_hcalout_twr = false;
0067   bool do_hcalout_cluster = false;
0068   bool do_hcalout_eval = false;
0069 
0070   bool do_global = false;
0071   bool do_global_fastsim = false;
0072 
0073   bool do_jet_reco = true;
0074   bool do_jet_eval = false;
0075 
0076   bool do_dst_compress = false;
0077 
0078   //Option to convert DST to human command readable TTree for quick poke around the outputs
0079   bool do_DSTReader = true;
0080   //---------------
0081   // Load libraries
0082   //---------------
0083 
0084   gSystem->Load("libfun4all.so");
0085   gSystem->Load("libg4detectors.so");
0086   gSystem->Load("libphhepmc.so");
0087   gSystem->Load("libg4testbench.so");
0088   gSystem->Load("libg4hough.so");
0089   gSystem->Load("libg4calo.so");
0090   gSystem->Load("libg4eval.so");
0091 
0092   // establish the geometry and reconstruction setup
0093   gROOT->LoadMacro("G4Setup_sPHENIX.C");
0094   G4Init(do_svtx,do_preshower,do_cemc,do_hcalin,do_magnet,do_hcalout,do_pipe);
0095 
0096   int absorberactive = 1; // set to 1 to make all absorbers active volumes
0097     const string magfield = "0"; // if like float -> solenoidal field in T, if string use as fieldmap name (including path)
0098 //  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)
0099   const float magfield_rescale = 1.4/1.5; // scale the map to a 1.4 T field
0100 
0101   //---------------
0102   // Fun4All server
0103   //---------------
0104 
0105   Fun4AllServer *se = Fun4AllServer::instance();
0106   se->Verbosity(1);
0107   // just if we set some flags somewhere in this macro
0108   recoConsts *rc = recoConsts::instance();
0109   // By default every random number generator uses
0110   // PHRandomSeed() which reads /dev/urandom to get its seed
0111   // if the RANDOMSEED flag is set its value is taken as seed
0112   // You ca neither set this to a random value using PHRandomSeed()
0113   // which will make all seeds identical (not sure what the point of
0114   // this would be:
0115   //  rc->set_IntFlag("RANDOMSEED",PHRandomSeed());
0116   // or set it to a fixed value so you can debug your code
0117   // rc->set_IntFlag("RANDOMSEED", 12345);
0118 
0119   //-----------------
0120   // Event generation
0121   //-----------------
0122 
0123   if (readhits)
0124     {
0125       // Get the hits from a file
0126       // The input manager is declared later
0127 
0128       if (do_embedding)
0129        {
0130          cout <<"Do not support read hits and embed background at the same time."<<endl;
0131          exit(1);
0132        }
0133 
0134     }
0135   else if (readhepmc)
0136     {
0137       // this module is needed to read the HepMC records into our G4 sims
0138       // but only if you read HepMC input files
0139       HepMCNodeReader *hr = new HepMCNodeReader();
0140       se->registerSubsystem(hr);
0141     }
0142   else if (runpythia8)
0143     {
0144       gSystem->Load("libPHPythia8.so");
0145 
0146 
0147       PHPy8JetTrigger *theTrigger = new PHPy8JetTrigger();
0148 //      theTrigger->Verbosity(10);
0149       theTrigger->SetEtaHighLow(-1, 1);
0150       theTrigger->SetJetR(.4);
0151       theTrigger->SetMinJetPt(25);
0152 
0153       PHPythia8* pythia8 = new PHPythia8();
0154       // see coresoftware/generators/PHPythia8 for example config
0155       pythia8->set_config_file("phpythia8.cfg");
0156 
0157       pythia8->beam_vertex_parameters(0,0,0,0,0,5);
0158       pythia8->register_trigger(theTrigger);
0159 //      pythia8->set_trigger_AND();
0160 
0161       se->registerSubsystem(pythia8);
0162       pythia8->print_config();
0163 //      pythia8->Verbosity(10);
0164 
0165       HepMCNodeReader *hr = new HepMCNodeReader();
0166       se->registerSubsystem(hr);
0167     }
0168   else if (runpythia6)
0169     {
0170       gSystem->Load("libPHPythia6.so");
0171 
0172       PHPythia6 *pythia6 = new PHPythia6();
0173       pythia6->set_config_file("phpythia6.cfg");
0174       se->registerSubsystem(pythia6);
0175 
0176       HepMCNodeReader *hr = new HepMCNodeReader();
0177       se->registerSubsystem(hr);
0178     }
0179   else
0180     {
0181       // toss low multiplicity dummy events
0182       PHG4SimpleEventGenerator *gen = new PHG4SimpleEventGenerator();
0183       gen->add_particles("e-",1); // mu+,e+,proton,pi+,Upsilon
0184       // gen->add_particles("e+",5); // mu-,e-,anti_proton,pi-
0185       if (readhepmc || do_embedding) {
0186     gen->set_reuse_existing_vertex(true);
0187     gen->set_existing_vertex_offset_vector(0.0,0.0,0.0);
0188       } else {
0189     gen->set_vertex_distribution_function(PHG4SimpleEventGenerator::Uniform,
0190                            PHG4SimpleEventGenerator::Uniform,
0191                            PHG4SimpleEventGenerator::Uniform);
0192     gen->set_vertex_distribution_mean(0.0,0.0,0.0);
0193     gen->set_vertex_distribution_width(0.0,0.0,5.0);
0194       }
0195       gen->set_vertex_size_function(PHG4SimpleEventGenerator::Uniform);
0196       gen->set_vertex_size_parameters(10.0,0.0);
0197       gen->set_eta_range(-0.1, 0.1);
0198       gen->set_phi_range(-1.0*TMath::Pi(), 1.0*TMath::Pi());
0199       gen->set_pt_range(24, 24);
0200       gen->Embed(1);
0201       gen->Verbosity(0);
0202       se->registerSubsystem(gen);
0203     }
0204 
0205   if (!readhits)
0206     {
0207       //---------------------
0208       // Detector description
0209       //---------------------
0210 
0211       G4Setup(absorberactive, magfield, TPythia6Decayer::kAll,
0212           do_svtx, do_preshower, do_cemc, do_hcalin, do_magnet, do_hcalout, do_pipe, magfield_rescale);
0213     }
0214 
0215   //---------
0216   // BBC Reco
0217   //---------
0218 
0219   if (do_bbc)
0220     {
0221       gROOT->LoadMacro("G4_Bbc.C");
0222       BbcInit();
0223       Bbc_Reco();
0224     }
0225   //------------------
0226   // Detector Division
0227   //------------------
0228 
0229   if (do_svtx_cell) Svtx_Cells();
0230 
0231   if (do_cemc_cell) CEMC_Cells();
0232 
0233   if (do_hcalin_cell) HCALInner_Cells();
0234 
0235   if (do_hcalout_cell) HCALOuter_Cells();
0236 
0237   //-----------------------------
0238   // CEMC towering and clustering
0239   //-----------------------------
0240 
0241   if (do_cemc_twr) CEMC_Towers();
0242   if (do_cemc_cluster) CEMC_Clusters();
0243 
0244   //-----------------------------
0245   // HCAL towering and clustering
0246   //-----------------------------
0247 
0248   if (do_hcalin_twr) HCALInner_Towers();
0249   if (do_hcalin_cluster) HCALInner_Clusters();
0250 
0251   if (do_hcalout_twr) HCALOuter_Towers();
0252   if (do_hcalout_cluster) HCALOuter_Clusters();
0253 
0254   if (do_dst_compress) ShowerCompress();
0255 
0256   //--------------
0257   // SVTX tracking
0258   //--------------
0259 
0260   if (do_svtx_track) Svtx_Reco();
0261 
0262   //-----------------
0263   // Global Vertexing
0264   //-----------------
0265 
0266   if (do_global)
0267     {
0268       gROOT->LoadMacro("G4_Global.C");
0269       Global_Reco();
0270     }
0271 
0272   else if (do_global_fastsim)
0273     {
0274       gROOT->LoadMacro("G4_Global.C");
0275       Global_FastSim();
0276     }
0277 
0278   //---------
0279   // Jet reco
0280   //---------
0281 
0282   if (do_jet_reco)
0283     {
0284       gROOT->LoadMacro("G4_Jets.C");
0285       Jet_Reco();
0286     }
0287   //----------------------
0288   // Simulation evaluation
0289   //----------------------
0290 
0291   if (do_svtx_eval) Svtx_Eval("g4svtx_eval.root");
0292 
0293   if (do_cemc_eval) CEMC_Eval("g4cemc_eval.root");
0294 
0295   if (do_hcalin_eval) HCALInner_Eval("g4hcalin_eval.root");
0296 
0297   if (do_hcalout_eval) HCALOuter_Eval("g4hcalout_eval.root");
0298 
0299   if (do_jet_eval) Jet_Eval("g4jet_eval.root");
0300 
0301 
0302 
0303   //-------------- 
0304   // IO management
0305   //--------------
0306 
0307   if (readhits)
0308     {
0309       // Hits file
0310       Fun4AllInputManager *hitsin = new Fun4AllDstInputManager("DSTin");
0311       hitsin->fileopen(inputFile);
0312       se->registerInputManager(hitsin);
0313     }
0314   if (do_embedding)
0315     {
0316       if (embed_input_file == NULL)
0317         {
0318           cout << "Missing embed_input_file! Exit";
0319           exit(3);
0320         }
0321 
0322       Fun4AllDstInputManager *in1 = new Fun4AllNoSyncDstInputManager("DSTinEmbed");
0323       //      in1->AddFile(embed_input_file); // if one use a single input file
0324       in1->AddListFile(embed_input_file); // RecommendedL: if one use a text list of many input files
0325       se->registerInputManager(in1);
0326     }
0327   if (readhepmc)
0328     {
0329       Fun4AllInputManager *in = new Fun4AllHepMCInputManager( "DSTIN");
0330       se->registerInputManager( in );
0331       se->fileopen( in->Name().c_str(), inputFile );
0332     }
0333   else
0334     {
0335       // for single particle generators we just need something which drives
0336       // the event loop, the Dummy Input Mgr does just that
0337       Fun4AllInputManager *in = new Fun4AllDummyInputManager( "JADE");
0338       se->registerInputManager( in );
0339     }
0340 
0341   //temp lines for QA modules
0342     {
0343 
0344       gSystem->Load("libqa_modules");
0345 
0346         if (do_jet_reco)
0347           {
0348             QAG4SimulationJet * calo_jet7 = new QAG4SimulationJet(
0349                 "AntiKt_Truth_r07",QAG4SimulationJet:: kProcessTruthSpectrum);
0350             se->registerSubsystem(calo_jet7);
0351 
0352             QAG4SimulationJet * calo_jet7 = new QAG4SimulationJet(
0353                 "AntiKt_Truth_r04", QAG4SimulationJet::kProcessTruthSpectrum);
0354             se->registerSubsystem(calo_jet7);
0355 
0356             QAG4SimulationJet * calo_jet7 = new QAG4SimulationJet(
0357                 "AntiKt_Truth_r02",QAG4SimulationJet:: kProcessTruthSpectrum);
0358             se->registerSubsystem(calo_jet7);
0359           }
0360       }
0361 
0362     // HF jet trigger moudle
0363       assert (gSystem->Load("libHFJetTruthGeneration") == 0);
0364       {
0365         if (do_jet_reco)
0366           {
0367             HFJetTruthTrigger * jt = new HFJetTruthTrigger(
0368                 "HFJetTruthTrigger.root",5 , "AntiKt_Truth_r07");
0369 //            jt->Verbosity(HFJetTruthTrigger::VERBOSITY_MORE);
0370             se->registerSubsystem(jt);
0371 
0372             HFJetTruthTrigger * jt = new HFJetTruthTrigger(
0373                 "HFJetTruthTrigger.root",5 , "AntiKt_Truth_r04");
0374 //            jt->Verbosity(HFJetTruthTrigger::VERBOSITY_MORE);
0375             se->registerSubsystem(jt);
0376 
0377             HFJetTruthTrigger * jt = new HFJetTruthTrigger(
0378                 "HFJetTruthTrigger.root",5 , "AntiKt_Truth_r02");
0379 //            jt->Verbosity(HFJetTruthTrigger::VERBOSITY_MORE);
0380             se->registerSubsystem(jt);
0381           }
0382       }
0383 
0384   // HF jet analysis moudle
0385     if (gSystem->Load("libslt") == 0)
0386     {
0387 
0388 
0389       if (do_jet_reco)
0390         {
0391           SoftLeptonTaggingTruth * slt = new SoftLeptonTaggingTruth(
0392               "AntiKt_Truth_r07");
0393           se->registerSubsystem(slt);
0394 
0395           SoftLeptonTaggingTruth * slt = new SoftLeptonTaggingTruth(
0396               "AntiKt_Truth_r04");
0397 //          slt->Verbosity(1);
0398           se->registerSubsystem(slt);
0399 
0400           SoftLeptonTaggingTruth * slt = new SoftLeptonTaggingTruth(
0401               "AntiKt_Truth_r02");
0402           se->registerSubsystem(slt);
0403         }
0404     }
0405 
0406 
0407   if (do_DSTReader)
0408     {
0409       //Convert DST to human command readable TTree for quick poke around the outputs
0410       gROOT->LoadMacro("G4_DSTReader.C");
0411 
0412       G4DSTreader( outputFile, //
0413           /*int*/ absorberactive ,
0414           /*bool*/ do_svtx ,
0415           /*bool*/ do_preshower ,
0416           /*bool*/ do_cemc ,
0417           /*bool*/ do_hcalin ,
0418           /*bool*/ do_magnet ,
0419           /*bool*/ do_hcalout ,
0420           /*bool*/ do_cemc_twr ,
0421           /*bool*/ do_hcalin_twr ,
0422           /*bool*/ do_magnet  ,
0423           /*bool*/ do_hcalout_twr
0424           );
0425     }
0426 
0427 
0428    Fun4AllDstOutputManager *out = new Fun4AllDstOutputManager("DSTOUT", outputFile);
0429    if (do_dst_compress) DstCompress(out);
0430    se->registerOutputManager(out);
0431 
0432   //-----------------
0433   // Event processing
0434   //-----------------
0435   if (nEvents < 0)
0436     {
0437       return;
0438     }
0439   // if we run the particle generator and use 0 it'll run forever
0440   if (nEvents == 0 && !readhits && !readhepmc)
0441     {
0442       cout << "using 0 for number of events is a bad idea when using particle generators" << endl;
0443       cout << "it will run forever, so I just return without running anything" << endl;
0444       return;
0445     }
0446 
0447   se->run(nEvents);
0448 
0449   {
0450 
0451     gSystem->Load("libqa_modules");
0452     QAHistManagerDef::saveQARootFile(string(outputFile) + "_qa.root");
0453 
0454       if (gSystem->Load("libslt") == 0)
0455         {
0456           SoftLeptonTaggingTruth::getHistoManager()->dumpHistos(
0457               string(outputFile) + "_slt.root");
0458         }
0459   }
0460 
0461   //-----
0462   // Exit
0463   //-----
0464   gSystem->Exec("ps -o sid,ppid,pid,user,comm,vsize,rssize,time");
0465   se->End();
0466   std::cout << "All done" << std::endl;
0467   delete se;
0468   gSystem->Exit(0);
0469 }