// Overall, it makes the code simpler, more readable, and more stable: less magic buffer-size number etc. # include # include # include # include # include # include # include //# include # include # include # include # include //# include //For Sleep() #include #include "TGraph.h" #include "TAxis.h" # include "TROOT.h" # include "TFile.h" # include "TTree.h" # include "TBrowser.h" # include "TH1.h" # include "TH2.h" # include "TH3.h" # include "TRandom.h" # include using namespace std; int main(){ // controls const char *inputFileName = "Sodium_22.Spe"; bool WantToFit = true; // switch for fitting const char *GraphTitle = "Sodium"; const char *GraphXLabel = "Energy (MeV)"; const char *GraphYLabel = "Counts"; const char *FitFunc = "gaus";//"[0]*x + [1]"; const char *DrawOption = "AP" ; // see https://root.cern.ch/doc/master/classTGraphPainter.html int xmin = 0; int xmax = 0; // create the coordinate arrays vector energy; vector counts; ifstream inFile(inputFileName); if(inFile.is_open()){ cout<<"Input File was opened successfully"<>liney) { energy.emplace_back(tempEnergy); counts.emplace_back(liney); tempEnergy++; cout << "Energy: " << tempEnergy << " Counts: " << liney << endl; } cout << "Read " << energy.size() << " lines\n"; xmax = energy.size() + 1; cout << "Creating canvas" << endl; // create the canvas TCanvas *c1 = new TCanvas("c1",GraphTitle,200,10,700,500); c1->SetGrid(); // create the TGraphErrors and draw it TGraphErrors *gr = new TGraphErrors(energy.size(),&energy[0],&counts[0]); // change title and axes titles here gr->SetTitle("A1;Energy MeV;Number of Counts"); gr->SetTitle(GraphTitle); gr->GetYaxis()->SetTitle(GraphXLabel); gr->GetYaxis()->SetTitle(GraphYLabel); gr->SetMarkerColor(4); gr->SetMarkerStyle(21); gr->SetMarkerSize(0.2); // to set axis limits // TAxis *axis = gr->GetXaxis(); // axis->SetLimits(x[0],x[5]); gr->Draw(DrawOption); // Define Fit Function if (WantToFit){ TF1 *f1 = new TF1("f1", FitFunc, 90, 125); TF1 *f2 = new TF1("f2", FitFunc, 128, 597); TF1 *f3 = new TF1("f3", FitFunc, 600, 700); //f->SetParNames("A0","A1","A2"); //f->SetParameters(1,1); gr->Fit(f1, "EX0"); cout << "Fitting..." << endl; } // c1->Update(); return 0; }