25 #include <RecEventFile.h>
27 #include <DetectorGeometry.h>
28 #include <EyeGeometry.h>
30 #include <EventInfo.h>
32 #include <GenShower.h>
33 #include <FdGenShower.h>
34 #include <TelescopeGeometry.h>
35 #include <FdTelescopeData.h>
36 #include <FdApertureLight.h>
37 #include <FdGeometry.h>
38 #include <FdApertureLight.h>
40 #define BOOST_SYSTEM_NO_DEPRECATED
41 #include <boost/algorithm/string.hpp>
42 #include <boost/lexical_cast.hpp>
43 #include <boost/filesystem.hpp>
57 const double energyGeV,
58 const double thetaDeg,
60 const double arrangDeg,
61 const double obslevCM,
65 map<string,string> mapping;
66 mapping[
"@RUNNR@"] = boost::lexical_cast<std::string>(iRun);
67 mapping[
"@ENERGY@"] = boost::lexical_cast<std::string>(energyGeV);
68 mapping[
"@THETA@"] = boost::lexical_cast<std::string>(thetaDeg);
69 mapping[
"@PHI@"] = boost::lexical_cast<std::string>(phiDeg);
70 mapping[
"@SEED1@"] = boost::lexical_cast<std::string>(iRun*3+1);
71 mapping[
"@SEED2@"] = boost::lexical_cast<std::string>(iRun*3+2);
72 mapping[
"@SEED3@"] = boost::lexical_cast<std::string>(iRun*3+3);
73 mapping[
"@ARRANG@"] = boost::lexical_cast<std::string>(arrangDeg);
74 mapping[
"@OBSLEV@"] = boost::lexical_cast<std::string>(obslevCM);
75 mapping[
"@COREX@"] = boost::lexical_cast<std::string>(corexCM);
76 mapping[
"@COREY@"] = boost::lexical_cast<std::string>(coreyCM);
78 oname <<
"DAT" << setw(6) << setfill(
'0') << iRun <<
".inp";
80 cout <<
"output steering card " << (steerdir / oname.str()).
string() <<
" already exists!" << endl;
83 ifstream input(
"DATxxxxxx_inp.template");
84 ofstream output((steerdir / oname.str()).
string().c_str());
85 while (input.good() && output.good()) {
88 for (
const auto& replace : mapping)
89 boost::replace_all(line, replace.first, replace.second);
90 output << line << endl;
99 const vector<string>& inputs)
101 const double degree = 180 / M_PI;
103 map<string,map<int,int>> mapADST;
104 map<int,pair<string,int>> mapCORS;
106 cout <<
"checking lock: " << (steerdir /
"ShowerInfo2CORSIKA.txt-LOCK").
string() << endl;
107 bool isLocked =
true;
109 while (
boost::filesystem::exists(steerdir /
"ShowerInfo2CORSIKA.txt-LOCK")) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }
110 std::ofstream lock ((steerdir /
"ShowerInfo2CORSIKA.txt-LOCK").
string());
111 isLocked &= lock.good();
113 isLocked &= lock.good();
115 isLocked &= lock.good();
117 cout <<
"locked: " << (steerdir /
"ShowerInfo2CORSIKA.txt-LOCK").
string() << endl;
120 infile.open ((steerdir /
"ShowerInfo2CORSIKA.txt").
string());
123 while(std::getline(infile, line)) {
124 string adst = line.substr(line.find(
" file=")+6, line.find(
" ", line.find(
" file=")+6)-line.find(
" file=")-6);
125 string evt_id = line.substr(line.find(
" id=")+4, line.find(
" ", line.find(
" id=")+4)-line.find(
" id=")-4);
126 int evt = atoi((evt_id.substr(evt_id.find(
":Use_")+5, evt_id.length()-evt_id.find(
":Use_")-5)).c_str());
127 int cors = atoi((line.substr(line.find(
" corsikaID=")+11, line.find(
" ", line.find(
" corsikaID=")+11)-line.find(
" corsikaID=")-11)).c_str());
129 adst = adst.substr(adst.rfind(
'/')+1, adst.length()-adst.rfind(
'/')-1);
130 mapADST[adst][evt] = cors;
131 mapCORS[cors] = std::make_pair(adst, evt);
139 outfile.open ((steerdir /
"ShowerInfo2CORSIKA.txt").
string(), std::ofstream::app);
141 RecEvent* theRecEvent =
new RecEvent();
142 DetectorGeometry* theGeometry = 0;
143 theGeometry =
new DetectorGeometry();
146 unsigned int countFile = 0;
147 unsigned int countEvent = 0;
148 unsigned int countSelected = 0;
149 unsigned int countSelectedNew = 0;
150 unsigned int countSelectedAndReco = 0;
151 unsigned int countHeatFOV = 0;
152 unsigned int countMiss = 0;
154 for (
const auto& input : inputs) {
161 RecEventFile dataFile(input.c_str());
162 dataFile.SetBuffers(&theRecEvent);
163 dataFile.ReadDetectorGeometry(*theGeometry);
164 dataFile.ReadFileInfo(theInfo);
167 double ObservationLevelMeter = 1300;
168 double diffangleDeg = -85.767;
170 unsigned int countInFile = 0;
174 const string EventID = theRecEvent->GetEventId();
175 const int EventID_int = atoi((EventID.substr(EventID.find(
":Use_")+5, EventID.length()-EventID.find(
":Use_")-5)).c_str());
180 TVector3 shower_axis = theRecEvent->GetGenShower().GetAxisSiteCS();
184 TVector3 shower_core = theRecEvent->GetGenShower().GetCoreAtAltitudeSiteCS(ObservationLevelMeter);
190 double hottestTelPhotons = -1;
191 for (RecEvent::EyeIterator eyeIt= theRecEvent->EyesBegin();
192 eyeIt !=theRecEvent->EyesEnd();
194 const int eyeId = eyeIt->GetEyeId();
197 for (FDEvent::TelescopeDataConstIterator telIt = eyeIt->TelescopeDataBegin();
198 telIt != eyeIt->TelescopeDataEnd(); ++telIt) {
199 const FdTelescopeData& telData = telIt->second;
200 const FdApertureLight& theGenlight = telData.GetGenApertureLight();
201 const vector<double>& vecTotal = theGenlight.GetTotalLightAtAperture();
202 double totalPhotonsTel = 0;
203 for (
const auto& itot : vecTotal) {totalPhotonsTel += itot;}
204 if (totalPhotonsTel > hottestTelPhotons) {
206 hottestTel = telIt->first;
207 hottestTelPhotons = totalPhotonsTel;
213 double genCherPhotons = 0;
214 double genTotalPhotons = 0;
215 double genMeanAngle = -1;
217 if (hottestTelPhotons>0) {
219 const FDEvent& eye = theRecEvent->GetEye(hottestEye);
220 const FdTelescopeData& telData = eye.GetTelescopeData(hottestTel);
222 const int eyeId = eye.GetEyeId();
225 const FdApertureLight& theGenlight = telData.GetGenApertureLight();
226 const vector<double>& vecCher = theGenlight.GetCherLightAtAperture();
227 const vector<double>& vecTotal = theGenlight.GetTotalLightAtAperture();
228 for (
const auto& icher : vecCher) {genCherPhotons += icher;}
229 for (
const auto& itot : vecTotal) {genTotalPhotons += itot;}
231 genMeanAngle = theGenlight.GetMeanAngle() *
degree;
242 int Prim = theRecEvent->GetGenShower().GetPrimary();
243 double ZenithDeg = theRecEvent->GetGenShower().GetAxisSiteCS().Theta() *
degree;
244 double EnergyGeV = (theRecEvent->GetGenShower().GetEnergy())/
pow(10,9);
245 double Azimuth_correctedDeg = theRecEvent->GetGenShower().GetAxisSiteCS().Phi() *
degree;
246 Azimuth_correctedDeg += diffangleDeg + 180;
247 if (Azimuth_correctedDeg<0) Azimuth_correctedDeg += 360;
248 if (Azimuth_correctedDeg>360) Azimuth_correctedDeg -= 360;
249 double x_posCM =(shower_core.X()) *
pow(10,2);
250 double y_posCM =(shower_core.Y()) *
pow(10,2);
262 string input_test = input, input_test_alt;
263 if (input.find(
"presel") == string::npos) {
264 input_test_alt = input.substr(0, input.find(
"adst.root")) +
"presel_adst.root";
266 input_test_alt = input.substr(0, input.rfind(
"presel_adst.root")) +
"adst.root";
270 input_test = input_test.substr(input_test.rfind(
'/')+1, input_test.length()-input_test.rfind(
'/')-1);
271 input_test_alt = input_test_alt.substr(input_test_alt.rfind(
'/')+1, input_test_alt.length()-input_test_alt.rfind(
'/')-1);
274 if (mapADST.count(input_test)>0 && mapADST[input_test].count(EventID_int)>0) {
276 cout << input <<
" evt=" << EventID_int <<
" already converted to CORSIKA steering card (" << mapADST[input_test][EventID_int] <<
")" << endl;
278 }
else if (mapADST.count(input_test_alt)>0 && mapADST[input_test_alt].count(EventID_int)>0) {
280 cout << input <<
" evt=" << EventID_int <<
" already converted to CORSIKA steering card (" << mapADST[input_test_alt][EventID_int] <<
")" << endl;
284 while(mapCORS.count(corsikaID)>0) {
296 Azimuth_correctedDeg,
298 ObservationLevelMeter*100,
302 outfile << Prim <<
" " << EnergyGeV <<
" " << ZenithDeg <<
" "<< Azimuth_correctedDeg <<
" "<< x_posCM <<
" " << y_posCM <<
" "<<
" "<<ObservationLevelMeter*100. <<
" "<< diffangleDeg <<
" file=" << input <<
" id="<<EventID <<
" eye=" << selectedEye <<
" tot=" << genTotalPhotons <<
" ch=" << genCherPhotons <<
" ViewAngle=" << genMeanAngle <<
" corsikaID=" << corsikaID <<
" EventID_int=" << EventID_int << endl;
305 mapADST[input][EventID_int] = corsikaID;
306 mapCORS[corsikaID] = make_pair(input,EventID_int);
316 cout <<
"Finished: countFile=" << countFile <<
" countEvent=" << countEvent
317 <<
" countSelected=" << countSelected <<
" countSelectedNew=" << countSelectedNew
318 <<
" countSelectedAndReco=" << countSelectedAndReco
319 <<
" countHeatFOV=" << countHeatFOV
320 <<
" countMiss=" << countMiss <<
" (w/ chFrac>0.5)"
323 boost::filesystem::remove( steerdir /
"ShowerInfo2CORSIKA.txt-LOCK");
324 cout <<
"unlocked: " << (steerdir /
"ShowerInfo2CORSIKA.txt-LOCK").
string() << endl;
334 cout <<
" specify steerdir AND input file" << endl;
337 const string steerdir(argv[1]);
338 boost::filesystem::path dir(steerdir);
339 if (!boost::filesystem::is_directory(dir)) {
340 cout <<
" specify steerdir AND input file" << endl;
341 cout << dir <<
" is not a directory" << endl;
345 vector<string> inputs;
346 for (
int i=1; i<argc-1; ++i)
347 inputs.push_back(
string(argv[i+1]));
void writeSteeringCard(boost::filesystem::path steerdir, const int iRun, const double energyGeV, const double thetaDeg, const double phiDeg, const double arrangDeg, const double obslevCM, const double corexCM, const double coreyCM)
double pow(const double x, const unsigned int i)
int main(int argc, char *argv[])
void ADST2CORSIKA(boost::filesystem::path steerdir, const vector< string > &inputs)
const bool uniqueFileNames