# Macro is not working

**URL:** <https://root-forum.cern.ch/t/macro-is-not-working/40383>\
**Category:** Newbie\
**Created:** [July 12, 2020, 7:30am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383 "2020-07-12T07:30:46Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:01pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/21 "2020-07-12T20:01:25Z")

</div>

As I said, move the creation of the root file in the loop

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [July 12, 2020, 8:02pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/22 "2020-07-12T20:02:11Z")

</div>

@bellenot  
But how and where should I execute the loop??

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:09pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/23 "2020-07-12T20:09:04Z")

</div>

Maybe you should try a bit by yourself… Anyway, here it is:

```auto
#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;
   }
}

```

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [July 12, 2020, 8:21pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/24 "2020-07-12T20:21:01Z")

</div>

Thanks @bellenot  
It still shows only and only dat files just like before.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:21pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/25 "2020-07-12T20:21:52Z")

</div>

Sorry, I don’t understand what you mean

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [July 12, 2020, 8:27pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/26 "2020-07-12T20:27:57Z")

</div>

@bellenot

 ![s](https://root-forum.cern.ch/uploads/default/original/3X/e/9/e92bd362262fd31023dc4a6a096cac4d1705a71e.png)  
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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:36pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/27 "2020-07-12T20:36:59Z")

</div>

Did you check in the directory where the macro is?

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [July 12, 2020, 8:40pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/28 "2020-07-12T20:40:57Z")

</div>

Thanks @bellenot  
Sorry yes there was a mistake from my side.

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [July 13, 2020, 7:28am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/29 "2020-07-13T07:28:18Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 13, 2020, 8:20am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/30 "2020-07-13T08:20:59Z")

</div>

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.

```auto
   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…

[Previous page](https://root-forum.cern.ch/t/macro-is-not-working/40383.md?page=1)
