A way to catch MCNP histograms...
... with a shellscript?
You could add the following function to your .bashrc.
----------------------------------------------------------------------------
function mcnpplot() { sed -n "/$2/,/total/p" $1 | sed '1,3d' | sed '$d' | xgraph -lnx -bg white -fmtx %.1e -x Energy\ [MeV] -y Normalized\ Fluence\ [A.U.] -t "$1" ; } #usage: > mcnpplot myMCNP_outputFile "surface 51" # The 2nd argument corresponds to the string from which you want to get the data. # Do not forget the double quotes and the double space! ----------------------------------------------------------------------------
It requires xgraph to be installed.
... with Root?
Here's a basic ROOT function to extract histogram from a MCNP file:
----------------------------------------------------------------------------
#include <TH1D.h> #include <vector> ///// template <typename myType> myType* myRootFunctions_getArrayFromVector (vector<myType>* myVector) { Int_t nElt = myVector->size() ; myType *myArray = new myType [nElt] ; for (Int_t i=0 ; i<nElt ; i++) { myArray[i] = myVector->at(i) ; } return (myArray) ; } ///// TH1D* myRootFunctions_getMCNPhisto (TString finName, TString histoName, TString str_start, Int_t nJumps=2, TString str_stop="total") // read MCNP File // Call: TH1D* histo = myRootFunctions_getMCNPhisto ("myMCNPFile.out", "myHisto", " surface 51") { vector<Double_t> *vect_energy = new vector<Double_t>, *vect_tally = new vector<Double_t>, *vect_dtally_tally = new vector<Double_t> ; TString str_energy, str_tally, str_dtally_tally ; Char_t energy[20], tally[20], dtally_tally[20], dummy[300]="" ; FILE* fin = fopen(finName.Data(),"r") ; if ( fin == NULL ) { fprintf (stderr, "\n\n!!! Error in 'myRootFunctions_getMCNPhisto': '%s' cannot be opened or read. It may not exist!\n\n",finName.Data()) ; getchar() ; exit(1) ; } do { fgets (dummy,300,fin) ; } while ( TString(dummy).Contains(str_start)==kFALSE && !feof(fin) ) ; if ( feof(fin) ) { fprintf (stderr,"\n\n!!! Error in 'myRootFunctions_getMCNPhisto': string '%s' cannot be found!\n\n",str_start.Data()) ; getchar() ; exit(1) ; } for (Int_t i=0; i<nJumps; i++) { fgets (dummy,300,fin) ; } //fprintf (stdout,"\n%s\n",dummy) ; getchar() ; do { fscanf (fin,"%s %s %s",energy,tally,dtally_tally) ; str_energy = energy ; str_tally = tally ; str_dtally_tally = dtally_tally ; //fprintf (stdout,"\n%s %s %s",str_energy.Data(),str_tally.Data(),str_dtally_tally.Data()) ; if ( str_energy != str_stop ) { vect_energy->push_back(str_energy.Atof()) ; vect_tally->push_back(str_tally.Atof()) ; vect_dtally_tally->push_back(str_dtally_tally.Atof()) ; } } while ( str_energy != str_stop ) ; //fprintf (stdout,"\n") ; fclose (fin) ; Int_t nPoints = Int_t (vect_energy->size()), nBins = nPoints-1 ; TH1D* histo = new TH1D (histoName,histoName,nBins,myRootFunctions_getArrayFromVector<Double_t>(vect_energy)) ; for (Int_t binNumber=1; binNumber<=nBins; binNumber++) { histo->SetBinContent(binNumber,vect_tally->at(binNumber)) ; histo->SetBinError (binNumber,vect_tally->at(binNumber)*vect_dtally_tally->at(binNumber)) ; } delete vect_energy ; vect_energy = NULL ; delete vect_tally ; vect_tally = NULL ; delete vect_dtally_tally ; vect_dtally_tally = NULL ; return (histo) ; } ----------------------------------------------------------------------------
To use it, copy it in a separate file, let's say getmcnp.c and execute the following macro:
----------------------------------------------------------------------------
{ gROOT->Reset() ; gROOT->ProcessLine(".L getmcnp.c+") ; TH1D* histo = myRootFunctions_getMCNPhisto ("myMCNPfile.out", "myHistoName", "cell 4", 2, "total") ; histo->SetXTitle("Kinetic energy [MeV]") ; histo->GetXaxis()->CenterTitle() ; histo->SetYTitle("Normalized Fluence" ) ; histo->GetYaxis()->CenterTitle() ; histo->SetLineColor(kRed) ; histo->SetMinimum(0.) ; histo->Draw("hist e") ; } ----------------------------------------------------------------------------
If you use NEdit, please see here.