# 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:** 20\
**Page:** 1

<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, 7:30am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/1 "2020-07-12T07:30:46Z")

</div>

Hi All,  
My macro is not working. Please see below what is required to run it.  
[eff\_6.root](https://root-forum.cern.ch/uploads/short-url/eBRNIMldM4Cw2fZzyzX4YTWNDRN.root) (216 Bytes) [A.C](https://root-forum.cern.ch/uploads/short-url/made7SelGFIT14TyZc7ZyigBLbe.C) (1.2 KB)

Thanks.

---

<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, 9:24am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/2 "2020-07-12T09:24:26Z")

</div>

Hi,  
Rename your macro `Ajay.C`, so you can execute it like `root -l Ajay.C`, and add a protection in case the file is not found, something like this:

```auto
void Ajay() 
{
   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;
   }

```

And the `eff_6.root` file is not valid…

---

<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, 11:54am UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/3 "2020-07-12T11:54:28Z")

</div>

Ok @bellenot  
Does the above macro requires a directory where the root file is situated or not??  
Also check the following macro which I used to convert from .dat to root file.  
[Ascii2root.C](https://root-forum.cern.ch/uploads/short-url/xlqkPR0wUROGG2QTY1dZ1aeKBLg.C) (976 Bytes)

---

<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, 12:20pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/4 "2020-07-12T12:20:28Z")

</div>

> [@arorastacy](#):
>
> Does the above macro requires a directory where the root file is situated or not??

As you want, I simply tried with the ROOT file being in the same directory than the macro

> [@arorastacy](#):
>
> Also check the following macro which I used to convert from .dat to root file

Can you also post your data file (or a subset)

---

<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, 12:23pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/5 "2020-07-12T12:23:30Z")

</div>

ok @bellenot  
the data file is [eff\_6.dat](https://root-forum.cern.ch/uploads/short-url/bmOHBzfnKhLSt4htQgHQlf0yNeo.txt) (83.4 KB)

---

<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, 2:24pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/6 "2020-07-12T14:24:22Z")

</div>

Add these line:

```auto
   h->Write();
   a->Close();
   delete a;

```

at the end of your `Ascii2root.C` macro

---

<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, 2:42pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/7 "2020-07-12T14:42:21Z")

</div>

I have done what you have suggested above but it shows the following error  
Processing Ascii2root.C…  
root [1] .x A.C  
Error in TFile::TFile: file .root does not exist  
#0 0x00007f7bbbdad6e7 in \_\_GI\_\_\_waitpid (pid=23430, stat\_loc=stat\_loc  
entry=0x7ffcd2e2fd88, options=options  
entry=0) at …/sysdeps/unix/sysv/linux/waitpid.c:30  
#1 0x00007f7bbbd18107 in do\_system (line=) at …/sysdeps/posix/system.c:149  
#2 0x00007f7bbc9c1b23 in TUnixSystem::Exec (shellcmd=, this=0x559a6c40c7c0) at /home/gourav/Downloads/root6/core/unix/src/TUnixSystem.cxx:2119  
#3 TUnixSystem::StackTrace (this=0x559a6c40c7c0) at /home/gourav/Downloads/root6/core/unix/src/TUnixSystem.cxx:2413  
#4 0x00007f7bb71b9005 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/gourav/Downloads/MyRoot/lib/libCling.so  
#5 0x00007f7bb71b8a0b in cling\_runtime\_internal\_throwIfInvalidPointer () from /home/gourav/Downloads/MyRoot/lib/libCling.so  
#6 0x00007f7bbd17d0aa in ?? ()  
#7 0x0000000000000000 in ?? ()  
Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.  
Execution of your code was aborted.  
In file included from input\_line\_38:1:  
/home/gourav/Downloads/MyRoot/macros/Eu152 source/Efficiencydat/Files/A.C:17:20: warning: null passed to a callee that requires a non-null argument [-Wnonnull]  
TH1D _h2 = (TH1D_)f-\>Get(“h”)-\>Clone();

---

<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, 2:49pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/8 "2020-07-12T14:49:10Z")

</div>

Here is the error:

> [@arorastacy](#):
>
> Error in TFile::TFile: file .root does not exist

As I previously said:

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

```

---

<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, 2:53pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/9 "2020-07-12T14:53:45Z")

</div>

To make it clear, here is the [Ajay.C](https://root-forum.cern.ch/uploads/short-url/8Ux8O2cNe21GXVEiNuaDSV5Xl0W.C) (1.3 KB) macro, and the [Ascii2root.C](https://root-forum.cern.ch/uploads/short-url/9qYWNBz2GBTn61BtEzR6azpFrZR.C) (1.0 KB). You can obviously change the file name & path

---

<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, 3:19pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/10 "2020-07-12T15:19:15Z")

</div>

Thanks @bellenot it worked.  
Now, I have a question related to get the area under the founded peaks? How can i proceed further??

---

<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, 3:23pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/11 "2020-07-12T15:23:09Z")

</div>

Maybe with [TGraph::Integral()](https://root.cern.ch/doc/master/classTGraph.html#a06b6dfc69d4fe9336ade71a47864f974)?

---

<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, 5:26pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/12 "2020-07-12T17:26:55Z")

</div>

@bellenot  
To calculate the area, I was using the function gausn(0) + pol(2) and writing the ranges for each of the peak in histogram. But this is so cumbersome and I am looking for more reliable approach.  
Is there any other way??

---

<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, 5:55pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/13 "2020-07-12T17:55:00Z")

</div>

Probably… If the integral() doesn’t fit your needs, maybe @moneta has a better idea…

---

<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, 6:22pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/14 "2020-07-12T18:22:17Z")

</div>

ok @bellenot  
One more question is that as I am having 50 .dat files. I already made [Ascii2root.C](https://root-forum.cern.ch/uploads/short-url/9qYWNBz2GBTn61BtEzR6azpFrZR.C) (1.0 KB)  
which is writing my code into .root extension.  
what can I change in my C program to convert all those .dat files with different names into .root files??  
I tried to read tree tutorials but unable to understand.

---

<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, 6:39pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/15 "2020-07-12T18:39:02Z")

</div>

Put all your `.dat` files in a common directory and then you can loop over all the files, something like this:

```auto
   void *dirp = gSystem->OpenDirectory(dirname);
   if (!dirp) return;
   char *direntry;
   //loop on all entries of this directory
   while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
      TString afile = TString::Format("%s/%s",dirname,direntry);
      TGraph *g = new TGraph(afile.Data());
      [...]
   }

```

---

<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, 6:58pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/16 "2020-07-12T18:58:40Z")

</div>

Thanks @bellenot  
Since I am a newbie to root. I am unable to think where it should be written in the macro.  
Can you please give me the upgraded macro??

---

<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, 7:08pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/17 "2020-07-12T19:08:58Z")

</div>

```auto
#include "Riostream.h"

void Ascii2root(const char *dirname = "data")
{
   if (!(dirname && dirname[0])) return; // just a precaution
   auto a = TFile::Open("eff_6.root","RECREATE");

   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);
      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, 7:22pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/18 "2020-07-12T19:22:52Z")

</div>

Thanks @bellenot.  
Your code is working quite well but there is one problem that (just like eff\_6.dat file there are many other file in .dat) and it is only able to write all the files into the tree in .dat extension and only writing eff\_6.root in that tree.  
Now what I want is that it should write all the file in .root extension so that on clicking each root file it displays a histogram just like eff\_6.root file.

Also when I click on eff\_6.root it gives me the following error.  
root [0]  
Processing RootTree.C…  
root [1] new TBrowser  
(TBrowser \*) 0x559dee552620  
root [2] Error in TFile::ReadBuffer: error reading all requested bytes from file eff\_6.root, got 216 of 300  
Error in TFile::Init: eff\_6.root failed to read the file type data.  
(TFile \*) 0x559dee55a7a0

\_

---

<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, 7:32pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/19 "2020-07-12T19:32:48Z")

</div>

> [@arorastacy](#):
>
> and it is only able to write all the files into the tree in .dat extension and only writing eff\_6.root in that tree.

Sorry, I don’t understand this sentence… The macro save all histograms in the same root file (there is no tree in the file). So you want to create as many root file as there are `.dat` files? Then simply create and close the ROOT fine inside the loop  
And the macro I posted produce valid data: ![image](https://root-forum.cern.ch/uploads/default/original/3X/c/2/c2ebd4a74b9303b0d9dc1d2bef0aa98694d8c224.png)

---

<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, 7:51pm UTC](https://root-forum.cern.ch/t/macro-is-not-working/40383/20 "2020-07-12T19:51:30Z")

</div>

@bellenot  
It shows:

 ![s](https://root-forum.cern.ch/uploads/default/original/3X/e/9/e92bd362262fd31023dc4a6a096cac4d1705a71e.png)  
Just like eff\_6.root file, other dat files also should be converted in root files.

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