Macro is not working

Hi All,
My macro is not working. Please see below what is required to run it.
eff_6.root (216 Bytes) A.C (1.2 KB)

Thanks.

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:

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…

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 (976 Bytes)

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

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

ok @bellenot
the data file is eff_6.dat (83.4 KB)

Add these line:

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

at the end of your Ascii2root.C macro

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();

Here is the error:

As I previously said:

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

To make it clear, here is the Ajay.C (1.3 KB) macro, and the Ascii2root.C (1.0 KB). You can obviously change the file name & path

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

Maybe with TGraph::Integral()?

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

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

ok @bellenot
One more question is that as I am having 50 .dat files. I already made Ascii2root.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.

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

   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());
      [...]
   }

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??

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

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

_

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

@bellenot
It shows:


Just like eff_6.root file, other dat files also should be converted in root files.