/scratch0/jsnuveri/BDSIM/BDSIMgit/bdsim/src/BDSOutputASCII.cc

00001 #include "BDSOutputASCII.hh"
00002 #include "BDSDebug.hh"
00003 #include "BDSExecOptions.hh"
00004 #include "BDSUtilities.hh"       // for BDS::non_alpha
00005 #include <cmath>
00006 #include <ctime>
00007 #include <string>
00008 #include <fstream>
00009 #include <iostream>
00010 #include <iomanip>
00011 #include <sys/stat.h>
00012 
00013 
00014 BDSOutputASCII::BDSOutputASCII():BDSOutputBase()
00015 {
00016   time_t currenttime;
00017   time(&currenttime);
00018   timestring = asctime(localtime(&currenttime));
00019   timestring = timestring.substr(0,timestring.size()-1);
00020   
00021   // generate filenames
00022   basefilename = BDSExecOptions::Instance()->GetOutputFilename();
00023   // lots of files - make a directory with the users permissions
00024   int status = mkdir(basefilename.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
00025   if (status == 0) {
00026     basefilename = basefilename + "/" + basefilename;
00027   } else { // directory creation didn't succeed
00028     G4cerr << __METHOD_NAME__ << "WARNING: directory " << basefilename << " was not created" << G4endl;
00029   }
00030     
00031   filename = basefilename + ".txt"; //main output filename - for samplers
00032   G4String filenamePrimaries  = basefilename + ".primaries.txt"; // primaries
00033   G4String filenameELoss      = basefilename + ".eloss.txt";     // energy loss hits
00034   G4String filenamePLoss      = basefilename + ".ploss.txt";     // primary loss hits
00035 
00036 #ifdef BDSDEBUG
00037   G4cout << __METHOD_NAME__ << "Output format ASCII - filenames:" << G4endl;
00038   G4cout << "Output                 : " << filename           << G4endl;
00039   G4cout << "Primaries              : " << filenamePrimaries  << G4endl;
00040   G4cout << "Energy Loss Hits       : " << filenameELoss      << G4endl;
00041   G4cout << "Primary Loss Hits      : " << filenamePLoss      << G4endl;
00042 #endif
00043   std::stringstream headerstream;
00044   headerstream << std::left << std::setprecision(10) << std::fixed
00045                << std::setw(6)  << "PDGID"    << " "
00046                << std::setw(15) << "E[GeV]"   << " "
00047                << std::setw(15) << "X[mum]"   << " "
00048                << std::setw(15) << "Y[mum]"   << " "
00049                << std::setw(15) << "Z[mum]"   << " "
00050                << std::setw(20) << "S[m]"     << " "
00051                << std::setw(15) << "Xp[rad]"  << " "
00052                << std::setw(15) << "Yp[rad]"  << " "
00053                << std::setw(6)  << "NEvent"   << " "
00054                << std::setw(15) << "Weight"   << " "
00055                << std::setw(9)  << "ParentID" << " "
00056                << std::setw(8)  << "TrackID"  << " "
00057                << std::setw(5)  << "Turn"
00058                << G4endl;
00059   G4String headerstring = headerstream.str();
00060 
00061   // main output file initialisation
00062   ofMain.open(filename.c_str());
00063   ofMain       << "### BDSIM output - created "<< timestring << G4endl;
00064   ofMain       << headerstring;
00065 
00066   // primaries output file initialisation
00067   ofPrimaries.open(filenamePrimaries.c_str());
00068   ofPrimaries  << "### BDSIM primaries output - created "<< timestring << G4endl;
00069   ofPrimaries  << headerstring;
00070 
00071   // energy loss hits output file initialisation
00072   ofELoss.open(filenameELoss.c_str());
00073   ofELoss      << "### BDSIM energy loss hits output - created " << timestring <<G4endl;
00074   ofELoss      << headerstring;
00075 
00076   // primary loss hits output file initialisation
00077   ofPLoss.open(filenamePLoss.c_str());
00078   ofPLoss      << "### BDSIM primary loss hits output - created " << timestring <<G4endl;
00079   ofPLoss      << headerstring;
00080 }
00081 
00082 BDSOutputASCII::~BDSOutputASCII()
00083 {
00084   if (ofMain.is_open()) {
00085     ofMain.flush();
00086     ofMain.close();
00087   }
00088   if (ofPrimaries.is_open()) {
00089     ofPrimaries.flush();
00090     ofPrimaries.close();
00091   }
00092   if (ofELoss.is_open()) {
00093     ofELoss.flush();
00094     ofELoss.close();
00095   }
00096   if (ofPLoss.is_open()) {
00097     ofPLoss.flush();
00098     ofPLoss.close();
00099   }
00100 }
00101 
00102 void BDSOutputASCII::WriteAsciiHit(std::ofstream* outfile, G4int PDGType, G4double Mom, G4double X, G4double Y, G4double Z, G4double S, G4double XPrime, G4double YPrime, G4int EventNo, G4double Weight, G4int ParentID, G4int TrackID, G4int TurnsTaken)
00103 {
00104   *outfile << std::left << std::setprecision(10) << std::fixed
00105            << std::setw(6)  << PDGType              << " "
00106            << std::setw(15) << Mom/CLHEP::GeV       << " "
00107            << std::setw(15) << X/CLHEP::micrometer  << " "
00108            << std::setw(15) << Y/CLHEP::micrometer  << " "
00109            << std::setw(15) << Z/CLHEP::micrometer  << " "
00110            << std::setw(20) << S/CLHEP::m           << " "
00111            << std::setw(15) << XPrime/CLHEP::radian << " "
00112            << std::setw(15) << YPrime/CLHEP::radian << " "
00113            << std::setw(6)  << EventNo              << " "
00114            << std::setw(15) << Weight               << " "
00115            << std::setw(9)  << ParentID             << " "
00116            << std::setw(8)  << TrackID              << " "
00117            << std::setw(5)  << TurnsTaken
00118            << G4endl;
00119 }
00120 
00121 void BDSOutputASCII::WritePrimary(G4String /*samplerName*/, G4double E,G4double x0,G4double y0,G4double z0,G4double xp,G4double yp,G4double /*zp*/,G4double /*t*/,G4double weight,G4int PDGType, G4int nEvent, G4int TurnsTaken){
00122   WriteAsciiHit(&ofPrimaries, PDGType, E, x0, y0, z0, /*s=*/0.0, xp, yp, nEvent, weight, 0, 1, TurnsTaken);
00123   ofPrimaries.flush();
00124 }
00125 
00126 void BDSOutputASCII::WriteHits(BDSSamplerHitsCollection *hc)
00127 {
00128   for (G4int i=0; i<hc->entries(); i++)
00129     {
00130       WriteAsciiHit(
00131                     &ofMain,
00132                     (*hc)[i]->GetPDGtype(),
00133                     (*hc)[i]->GetMom(),
00134                     (*hc)[i]->GetX(),
00135                     (*hc)[i]->GetY(),
00136                     (*hc)[i]->GetZ(),
00137                     (*hc)[i]->GetS(),
00138                     (*hc)[i]->GetXPrime(),
00139                     (*hc)[i]->GetYPrime(),
00140                     (*hc)[i]->GetEventNo(),
00141                     (*hc)[i]->GetWeight(),
00142                     (*hc)[i]->GetParentID(),
00143                     (*hc)[i]->GetTrackID(),
00144                     (*hc)[i]->GetTurnsTaken()
00145                     );
00146     }
00147   ofMain.flush();
00148 }
00149 
00150 // write a trajectory to a root/ascii file
00151 // TODO: ASCII file not implemented - JS
00152 void BDSOutputASCII::WriteTrajectory(std::vector<BDSTrajectory*> &/*TrajVec*/){
00153   G4cout << __METHOD_NAME__ << "WARNING trajectory writing not implemented for ASCII output" << G4endl;
00154 }
00155 
00156 // make energy loss histo
00157 void BDSOutputASCII::WriteEnergyLoss(BDSEnergyCounterHitsCollection* hc)
00158 {
00159   for (G4int i = 0; i < hc->entries(); i++)
00160     {
00161       // write the hits to the eloss file
00162       WriteAsciiHit(
00163                     &ofELoss,
00164                     (*hc)[i]->GetPartID(),
00165                     (*hc)[i]->GetEnergy(),
00166                     (*hc)[i]->GetX(),
00167                     (*hc)[i]->GetY(),
00168                     (*hc)[i]->GetZ(),
00169                     (*hc)[i]->GetS(),
00170                     0.0,//(*hc)[i]->GetXPrime(),
00171                     0.0,//(*hc)[i]->GetYPrime(),
00172                     0,//(*hc)[i]->GetEventNo(),
00173                     (*hc)[i]->GetWeight(),
00174                     0,//(*hc)[i]->GetParentID(),
00175                     0,//(*hc)[i]->GetTrackID(),
00176                     (*hc)[i]->GetTurnsTaken()
00177                     );
00178     }
00179   ofELoss.flush();
00180 }
00181 
00182 void BDSOutputASCII::WritePrimaryLoss(BDSEnergyCounterHit* hit)
00183 {
00184   //phist->Fill(hit->GetS()/CLHEP::m); //no weighting by energy - done in external analysis
00185 
00186   WriteAsciiHit(
00187                 &ofPLoss,
00188                 hit->GetPartID(),
00189                 hit->GetEnergy(),
00190                 hit->GetX(),
00191                 hit->GetY(),
00192                 hit->GetZ(),
00193                 hit->GetS(),
00194                 0.0,//hit->GetXPrime(),
00195                 0.0,//hit->GetYPrime(),
00196                 0,//hit->GetEventNo(),
00197                 hit->GetWeight(),
00198                 0,//hit->GetParentID(),
00199                 0,//hit->GetTrackID(),
00200                 hit->GetTurnsTaken()
00201                 );
00202   ofPLoss.flush();
00203 }
00204 
00205 void BDSOutputASCII::WritePrimaryHit(BDSEnergyCounterHit* /*hit*/)
00206 {}
00207 
00208 void BDSOutputASCII::WriteHistogram(BDSHistogram1D* histogramIn)
00209 {
00210   //prepare file name
00211   G4String title = histogramIn->GetName();
00212   title = BDS::PrepareSafeName(title);
00213   G4String filename = basefilename + "." + title + ".hist.txt";
00214 #ifdef BDSDEBUG
00215   G4cout << __METHOD_NAME__ << "filename determined to be: " << filename << G4endl;
00216 #endif
00217   
00218   //open file and write header info
00219   std::ofstream histOS;
00220   histOS.open(filename.c_str());
00221   histOS << *histogramIn << " - created " << timestring << G4endl;
00222   histOS << std::left << std::setprecision(10) << std::fixed
00223          << std::setw(20) << "SLower[m]" << " "
00224          << std::setw(20) << "SUpper[m]" << " "
00225          << std::setw(20) << "Value"     << G4endl;
00226   histOS << "Underflow: " << histogramIn->GetUnderflowBin()->GetValue() << G4endl;
00227   histOS << "Overflow:  " << histogramIn->GetOverflowBin()->GetValue()  << G4endl;
00228   
00229   //iterate over bins and fill them in
00230   histOS << std::scientific;
00231   for (histogramIn->first();!histogramIn->isDone();histogramIn->next())
00232     {
00233       histOS << std::setw(20) << histogramIn->currentBin()->GetLowerEdge() << " ";
00234       histOS << std::setw(20) << histogramIn->currentBin()->GetUpperEdge() << " ";
00235       histOS << std::setw(20) << histogramIn->currentBin()->GetValue() << G4endl;
00236     }
00237   histOS.close();
00238 }
00239 
00240 void BDSOutputASCII::Commit()
00241 {
00242   ofMain.flush();
00243   ofPrimaries.flush();
00244   ofELoss.flush();
00245   ofPLoss.flush();
00246   // Multiple file writing not implemented for ascii
00247 }
00248 
00249 void BDSOutputASCII::Write()
00250 {
00251   ofMain.flush();
00252   ofMain.close();
00253   ofPrimaries.flush();
00254   ofPrimaries.close();
00255   ofELoss.flush();
00256   ofELoss.close();
00257   ofPLoss.flush();
00258   ofPLoss.close();
00259 }

Generated on 28 Jun 2015 for BDSIM by  doxygen 1.4.7