00001
00002
00003
00004
00005
00006
00007
00008 #include "EventHeaderSvc.hh"
00009
00010 #include <string>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <sstream>
00014
00015 #include "myglobals.hh"
00016 #include "G4Event.hh"
00017 #include "Randomize.hh"
00018
00019 #include "MyRoot.hh"
00020
00021 EventHeaderSvc* EventHeaderSvc::fEventHeaderSvc = 0;
00022
00023 EventHeaderSvc::EventHeaderSvc()
00024 {
00025 if (fEventHeaderSvc){
00026 G4Exception("EventHeaderSvc::EventHeaderSvc()","Run0031",
00027 FatalException, "EventHeaderSvc constructed twice.");
00028 }
00029 fEventHeaderSvc = this;
00030 R0 = 0;
00031 R1 = 0;
00032 ipx = 0;
00033 ipy = 0;
00034 ipz = 0;
00035 ix=iy=iz=0;
00036 }
00037
00038 EventHeaderSvc::~EventHeaderSvc()
00039 {
00040 printf("~EventHeaderSvc\n");
00041 }
00042
00043 EventHeaderSvc* EventHeaderSvc::GetEventHeaderSvc(){
00044 if ( !fEventHeaderSvc ){
00045 fEventHeaderSvc = new EventHeaderSvc;
00046 }
00047 return fEventHeaderSvc;
00048 }
00049
00050 void EventHeaderSvc::SetBranch(){
00051 if(flag_run_num) MyRoot::GetMyRoot()->SetBranch("run_num" , &run_num);
00052 if(flag_evt_num) MyRoot::GetMyRoot()->SetBranch("evt_num" , &evt_num);
00053 if(flag_R0) MyRoot::GetMyRoot()->SetBranch("R0" , &R0);
00054 if(flag_R1) MyRoot::GetMyRoot()->SetBranch("R1" , &R1);
00055 if(flag_ipx) MyRoot::GetMyRoot()->SetBranch("i_px" , &ipx);
00056 if(flag_ipy) MyRoot::GetMyRoot()->SetBranch("i_py" , &ipy);
00057 if(flag_ipz) MyRoot::GetMyRoot()->SetBranch("i_pz" , &ipz);
00058 if(flag_position) MyRoot::GetMyRoot()->SetBranch("i_x" , &ix);
00059 if(flag_position) MyRoot::GetMyRoot()->SetBranch("i_y" , &iy);
00060 if(flag_position) MyRoot::GetMyRoot()->SetBranch("i_z" , &iz);
00061 if(flag_weight) MyRoot::GetMyRoot()->SetBranch("weight" , &weight);
00062 if(flag_pid) MyRoot::GetMyRoot()->SetBranch("i_particleName", &i_particleName);
00063 }
00064
00065 void EventHeaderSvc::ReadOutputCard(G4String filename){
00066 ReSet();
00067 std::ifstream fin_card(filename);
00068 if(!fin_card){
00069 std::cout<<"In EventHeaderSvc::ReadOutputCard, cannot open "<<filename<<"!!!"<<std::endl;
00070 G4Exception("EventHeaderSvc::ReadOutputCard()",
00071 "InvalidSetup", FatalException,
00072 "cannot find output card");
00073 }
00074 std::stringstream buf_card;
00075 std::string s_card;
00076 int n_output_section_symbol = 0;
00077 while(getline(fin_card,s_card)){
00078 buf_card.str("");
00079 buf_card.clear();
00080 buf_card<<s_card;
00081
00082
00083 const char* c_card = s_card.c_str();
00084 int length = strlen(c_card);
00085 int offset = 0;
00086 for ( ; offset < length; offset++ ){
00087 if ( c_card[offset] != ' ' ) break;
00088 }
00089 if ( c_card[offset] == '#' || (c_card[offset] == '/' && c_card[offset+1] == '/') || length - offset == 0 ){
00090 continue;
00091 }
00092
00093 std::string name;
00094 buf_card>>name;
00095 if ( n_output_section_symbol == 0 ){
00096 if ( name == "EVENTHEADER_SECTION" ){
00097 n_output_section_symbol++;
00098 }
00099 }
00100 else if ( n_output_section_symbol == 1 ){
00101 if ( name == "EVENTHEADER_SECTION" ){
00102 n_output_section_symbol++;
00103 }
00104 else if( name == "evt_num" ) flag_evt_num = true;
00105 else if( name == "run_num" ) flag_run_num = true;
00106 else if( name == "R0" ) flag_R0 = true;
00107 else if( name == "R1" ) flag_R1 = true;
00108 else if( name == "ipx" ) flag_ipx = true;
00109 else if( name == "ipy" ) flag_ipy = true;
00110 else if( name == "ipz" ) flag_ipz = true;
00111 else if( name == "weight" ) flag_weight = true;
00112 else if( name == "position" ) flag_position = true;
00113 else if( name == "pid" ) flag_pid = true;
00114 else{
00115 std::cout<<"In EventHeaderSvc::ReadOutputCard, unknown name: "<<name<<" in file "<<filename<<std::endl;
00116 std::cout<<"Will ignore this line!"<<std::endl;
00117 }
00118 }
00119
00120 if ( n_output_section_symbol > 1 ){
00121 break;
00122 }
00123 }
00124 buf_card.str("");
00125 buf_card.clear();
00126 if ( n_output_section_symbol <= 1 ){
00127 std::cout<<"*****************WARNING********************"<<std::endl;
00128 std::cout<<"In EventHeaderSvc::ReadOutputCard, failed to find enough section seperators for output in file "<<filename<<std::endl;
00129 std::cout<<"Will use default settings."<<std::endl;
00130 std::cout<<"********************************************"<<std::endl;
00131 }
00132 fin_card.close();
00133 ShowOutCard();
00134 }
00135
00136 void EventHeaderSvc::ReSet(){
00137 flag_evt_num = false;
00138 flag_run_num = false;
00139 flag_R0 = false;
00140 flag_R1 = false;
00141 flag_R1 = false;
00142 flag_ipx = false;
00143 flag_ipy = false;
00144 flag_ipz = false;
00145 flag_position = false;
00146 flag_pid = false;
00147 }
00148
00149 void EventHeaderSvc::ShowOutCard(){
00150 std::cout<<"*************************Output settings for EventHeaderSvc***************************"<<std::endl;
00151 std::cout<<"output evt_num?"<<(flag_evt_num?" yes":" no")<<std::endl;
00152 std::cout<<"output run_num?"<<(flag_run_num?" yes":" no")<<std::endl;
00153 std::cout<<"output R0?"<<(flag_R0?" yes":" no")<<std::endl;
00154 std::cout<<"output R1?"<<(flag_R1?" yes":" no")<<std::endl;
00155 std::cout<<"output ipx?"<<(flag_ipx?" yes":" no")<<std::endl;
00156 std::cout<<"output ipy?"<<(flag_ipy?" yes":" no")<<std::endl;
00157 std::cout<<"output ipz?"<<(flag_ipz?" yes":" no")<<std::endl;
00158 std::cout<<"output initial position?"<<(flag_position?" yes":" no")<<std::endl;
00159 std::cout<<"output weight?"<<(flag_weight?" yes":" no")<<std::endl;
00160 std::cout<<"output particle type?"<<(flag_pid?" yes":" no")<<std::endl;
00161 }
00162
00163 void EventHeaderSvc::SetValue(const G4Event* evt, int runNb, double w){
00164 run_num = runNb;
00165 evt_num = evt->GetEventID();
00166 weight = w;
00167 }
00168 void EventHeaderSvc::SetSeedsValue(){
00169 R0 = CLHEP::HepRandom::getTheSeeds()[0];
00170 R1 = CLHEP::HepRandom::getTheSeeds()[1];
00171 }
00172 void EventHeaderSvc::SetInitialMomentum(double tipx, double tipy, double tipz){
00173 ipx = tipx;
00174 ipy = tipy;
00175 ipz = tipz;
00176 }