Writing a root macro

I want to write a root macro with the following codes

int i ;
double total_electrons = 0;
for (i=1;i<11;i++)
{
total_electrons+=histogram_name->GetBinContent(i)*i;
std::cout<<"electron_no"<<total_electrons;
}

My root file is “newFile.root” and it has 5 histograms. I want to run the root macro for 3 of the histograms namely, el_no, e+_no and e-_no. Can someone help me how to write the macro? I am writing this macro in a separate file.

Thanks!

You should create a file named for instance mymacro.C.
The file should be something like:

void mymacro()
{
   TFile *f = new TFile("newFile.root");
   TH?? * histogram_name = (TH??) f ->Get("el_no") // replace TH?? by the appropriate type of histogram
   int i ;
   double total_electrons = 0;
   for (i=1;i<11;i++) {
      total_electrons+=histogram_name->GetBinContent(i)*i;
      std::cout<<"electron_no"<<total_electrons;
   }
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.