As I said, move the creation of the root file in the loop
Maybe you should try a bit by yourself… Anyway, here it is:
#include "Riostream.h"
void Ascii2root(const char *dirname = "data")
{
if (!(dirname && dirname[0])) return; // just a precaution
void *dirp = gSystem->OpenDirectory(dirname);
if (!dirp) return;
char *direntry;
int j = 0;
//loop on all entries of this directory
while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
if (direntry[0] == '.') continue; //forget the "." and ".." special cases
TString afile = TString::Format("%s/%s",dirname,direntry);
TString rfile = afile;
rfile.ReplaceAll(".dat", ".root");
auto a = TFile::Open(rfile.Data(),"RECREATE");
std::cout << "Reading " << afile.Data() << std::endl;
TGraph *g = new TGraph(afile.Data());
Int_t n = g->GetN(); // get the no. of points
if (n < 2) { delete g; continue; } // just a precaution
// g->Sort(); // just a precaution
Double_t *x = new Double_t[(n + 1)];
x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0;
x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0;
for (Int_t i = 1; i < n; i++) {
x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0;
}
TH1D *h = new TH1D(TString::Format("h%d",++j).Data(), "spectrum;x-value(Channels);#counts", n, x);
delete [] x; // no longer needed
h->FillN(n, g->GetX(), g->GetY());
delete g; // no longer needed
h->Sumw2(kFALSE); // make sure "bin_error = sqrt(bin_content)"
//h->ResetStats(); // reset the statistics including the number of entries
//h->Print("All");
//h->Draw();
h->Write();
delete h;
a->Close();
delete a;
}
}
Sorry, I don’t understand what you mean
In this image if you carefully at the files you can see one root file i.e. eff_6.root which i got from eff_6.dat.
Also you can see there are other dat files e.g. eff_28.dat …
All dat files should be converted into .root file just as eff_28.dat into eff_28.root.
But when I open TBrowser it only shows all dat files and only one root file i.e. eff_6.root.
Hopefully you understood what I am asking.
Did you check in the directory where the macro is?
@bellenot
I have used TText for writing on the spectrum like below:
and it shows
But i want to write peak energy for all the peaks just like
Please suggest how should I modify code to get above.
Here it is. Up to you to adjust to your need, and next time open a new topic since this is unrelated to this topic.
TFile *f = TFile::Open("eff_6.root", "READ"); //Open a root file
if ((!f) || f->IsZombie()) {
std::cout << "Failed to open eff_6.root!" << std::endl;
return;
}
TH1F *h = (TH1F*)f->Get("h");
h->Draw();
TText *t = new TText();
t->SetTextAlign(12);
t->SetTextColor(kRed+2);
t->SetTextFont(43);
t->SetTextSize(20);
t->SetTextAngle(90);
//Use TSpectrum to find the peak candidates
TSpectrum *spectrum = new TSpectrum(256);
int nfound = spectrum->Search(h, 2, "", 100);
double *xpeaks = spectrum->GetPositionX();
for (int p=0;p<nfound;p++) {
Float_t xp = xpeaks[p];
Int_t bin = h->GetXaxis()->FindBin(xp);
Float_t yp = h->GetBinContent(bin);
t->DrawText(xp, yp, TString::Format("%1.3f", xp).Data());
}
And you should definitively try to write your own code instead of asking other people to do it for you…