00001
00002
00003
00004
00005
00006
00007
00008 #include "MyTriggerSvc.hh"
00009
00010 #include "G4UnitsTable.hh"
00011 #include "myglobals.hh"
00012 #include "G4Event.hh"
00013
00014 #include "MyDetectorManager.hh"
00015 #include "MonitorSD.hh"
00016 #include "KillerSD.hh"
00017 #include "MyString2Anything.hh"
00018 #include "McTruthSvc.hh"
00019
00020 #include <sstream>
00021 #include <fstream>
00022 #include <string.h>
00023 #include <iostream>
00024
00025 #include "DEBUG.hh"
00026
00027 MyTriggerSvc* MyTriggerSvc::fMyTriggerSvc = 0;
00028
00029 MyTriggerSvc::MyTriggerSvc()
00030 :pMyDetectorManager(0),myMonitorSD(0),myMonitorSD2(0),myMcTruthSvc(0),myKillerSD(0)
00031 {
00032 if (fMyTriggerSvc){
00033 G4Exception("MyTriggerSvc::MyTriggerSvc()","Run0031",
00034 FatalException, "MyTriggerSvc constructed twice.");
00035 }
00036 fMyTriggerSvc = this;
00037 }
00038
00039 MyTriggerSvc::~MyTriggerSvc()
00040 {
00041 printf("~MyTriggerSvc\n");
00042 }
00043
00044 MyTriggerSvc* MyTriggerSvc::GetMyTriggerSvc(){
00045 if ( !fMyTriggerSvc ){
00046 fMyTriggerSvc = new MyTriggerSvc;
00047 }
00048 return fMyTriggerSvc;
00049 }
00050
00051 void MyTriggerSvc::SetMyTrigger( G4String filename ){
00052 ReSet();
00053 std::ifstream fin_card(filename);
00054 if(!fin_card){
00055 std::cout<<"In MyTriggerSvc::ReadOutputCard, cannot open "<<filename<<"!!!"<<std::endl;
00056 G4Exception("MyTriggerSvc::ReadOutputCard()",
00057 "InvalidSetup", FatalException,
00058 "cannot find output card");
00059 }
00060 std::stringstream buf_card;
00061 G4String s_card;
00062 int n_TRIGGER_section_symbol = 0;
00063 G4String TRIGGERSECTIONNAME = "TRIGGERSECTION";
00064 while(getline(fin_card,s_card)){
00065 buf_card.str("");
00066 buf_card.clear();
00067 buf_card<<s_card;
00068
00069
00070 const char* c_card = s_card.c_str();
00071 int length = strlen(c_card);
00072 int offset = 0;
00073 for ( ; offset < length; offset++ ){
00074 if ( c_card[offset] != ' ' ) break;
00075 }
00076 if ( c_card[offset] == '#' || (c_card[offset] == '/' && c_card[offset+1] == '/') || length - offset == 0 ){
00077 continue;
00078 }
00079
00080 G4String name, unit;
00081 buf_card>>name;
00082
00083 if ( n_TRIGGER_section_symbol == 0 ){
00084 if ( name == TRIGGERSECTIONNAME ){
00085 n_TRIGGER_section_symbol++;
00086 }
00087 }
00088 else if ( n_TRIGGER_section_symbol == 1 ){
00089 int para;
00090 buf_card>>para;
00091 if ( name == TRIGGERSECTIONNAME ){
00092 n_TRIGGER_section_symbol++;
00093 }
00094 else if ( name == "minM_Hits" ){
00095 minM_Hits = para;
00096 }
00097 else if ( name == "minV_Hits" ){
00098 minV_Hits = para;
00099 }
00100 else if ( name == "minEleMom" ){
00101 std::string unit;
00102 buf_card>>unit;
00103 minEleMom = para*MyString2Anything::get_U(unit);
00104 }
00105 else if ( name == "minAntipNum" ){
00106 minAntipNum = para;
00107 }
00108 else if ( name == "minTracks" ){
00109 minTracks = para;
00110 }
00111 else{
00112 std::cout<<"In MyTriggerSvc::SetMyTrigger, unknown name: "<<name<<" in file "<<filename<<std::endl;
00113 std::cout<<"Will ignore this line!"<<std::endl;
00114 }
00115 }
00116
00117 if ( n_TRIGGER_section_symbol > 1 ){
00118 break;
00119 }
00120 }
00121 buf_card.str("");
00122 buf_card.clear();
00123 if ( n_TRIGGER_section_symbol<= 1 ){
00124 std::cout<<"*****************WARNING********************"<<std::endl;
00125 std::cout<<"In MyTriggerSvc::ReadOutputCard, failed to find enough section seperators \""<<TRIGGERSECTIONNAME<<"\" for TRIGGER in file "<<filename<<std::endl;
00126 std::cout<<"Will use default settings."<<std::endl;
00127 std::cout<<"********************************************"<<std::endl;
00128 }
00129 fin_card.close();
00130 ShowOutCard();
00131 pMyDetectorManager = MyDetectorManager::GetMyDetectorManager();
00132 G4VSensitiveDetector* myVSD = 0;
00133 G4VSensitiveDetector* myVSD2 = 0;
00134 if ( minM_Hits != -1 ){
00135 myVSD = pMyDetectorManager->GetSD("","M/MonitorSD");
00136 if (myVSD){
00137 myMonitorSD = dynamic_cast<MonitorSD*> (myVSD);
00138 std::cout<<"myVSD @ ["<<(void*) myVSD<<"]"<<std::endl;
00139 std::cout<<"myMonitorSD @ ["<<(void*) myMonitorSD<<"]"<<std::endl;
00140 }
00141 }
00142 if ( minV_Hits != -1 ){
00143 myVSD = pMyDetectorManager->GetSD("","V/MonitorSD");
00144 if (myVSD){
00145 myMonitorSD = dynamic_cast<MonitorSD*> (myVSD);
00146 std::cout<<"myVSD @ ["<<(void*) myVSD<<"]"<<std::endl;
00147 std::cout<<"myMonitorSD @ ["<<(void*) myMonitorSD<<"]"<<std::endl;
00148 }
00149 }
00150 if ( minEleMom != -1*MeV ){
00151 myMcTruthSvc = McTruthSvc::GetMcTruthSvc();
00152 }
00153 if ( minAntipNum != -1 || minTracks != -1 ){
00154 myMcTruthSvc = McTruthSvc::GetMcTruthSvc();
00155 }
00156 }
00157
00158 bool MyTriggerSvc::TriggerIt( const G4Event* evt ){
00159
00160 if ( minM_Hits != -1 ){
00161 int nHits = 0;
00162 if (myMonitorSD){
00163 nHits = myMonitorSD->Get_nHits();
00164 }
00165 if ( nHits < minM_Hits ) return false;
00166 }
00167 if ( minV_Hits != -1 ){
00168 int nHits = 0;
00169 if (myMonitorSD){
00170 nHits = myMonitorSD->Get_nHits();
00171 }
00172 if ( nHits < minV_Hits ) return false;
00173 }
00174 if ( minEleMom != -1*MeV ){
00175 bool foundit = false;
00176 int nTracks = myMcTruthSvc->get_nTracks();
00177 for ( int i = 0; i < nTracks; i++ ){
00178 int pid = myMcTruthSvc->get_pid(i);
00179 double px = myMcTruthSvc->get_px(i)*GeV;
00180 double py = myMcTruthSvc->get_py(i)*GeV;
00181 double pz = myMcTruthSvc->get_pz(i)*GeV;
00182 double pa = sqrt(px*px+py*py+pz*pz);
00183 if ( pid == 11 && pa > minEleMom ) foundit = true;
00184 }
00185 if (!foundit) return false;
00186 }
00187 if ( minAntipNum != -1 ){
00188 bool foundit = false;
00189 int nTracks = myMcTruthSvc->get_nTracks();
00190 for ( int i = 0; i < nTracks; i++ ){
00191 int pid = myMcTruthSvc->get_pid(i);
00192 if ( pid == -2212 ) foundit = true;
00193 }
00194 if (!foundit) return false;
00195 }
00196 if ( minTracks!= -1 ){
00197 int nTracks = myMcTruthSvc->get_nTracks();
00198 if (nTracks<minTracks) return false;
00199 }
00200
00201
00202 return true;
00203 }
00204
00205 void MyTriggerSvc::ReSet(){
00206 minM_Hits = -1;
00207 minV_Hits = -1;
00208 minEleMom = -1*MeV;
00209 minAntipNum = -1;
00210 minTracks = -1;
00211 }
00212
00213 void MyTriggerSvc::ShowOutCard(){
00214 std::cout<<"*************************Trigger settings"<<"***************************"<<std::endl;
00215 std::cout<<"minM_Hits = "<<minM_Hits<<std::endl;
00216 std::cout<<"minV_Hits = "<<minV_Hits<<std::endl;
00217 std::cout<<"minEleMom= "<<minEleMom/MeV<<", MeV"<<std::endl;
00218 std::cout<<"minAntipNum= "<<minAntipNum<<std::endl;
00219 std::cout<<"minTracks = "<<minTracks<<std::endl;
00220 std::cout<<"******************************************************************************"<<std::endl;
00221 }