USe multiple input instead one

Dear Expert,

I have several input root files from different directories, and I would like to use all of them in my code. I have already tested my code with one file, and it works. However, when I try to add another one, it doesn’t work. Please find below a section of my code where I use the one input file and another section where I tried to add the other file.

With one file:

#ifdef inclphoton_cxx
inclphoton::inclphoton(TTree *tree) : fChain(0) 
{

   if (tree == 0) {
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("/userdata/inclusive/data15/user.data15.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000001.ANALYSIS.root");
      if (!f || !f->IsOpen()) {
         f = new TFile("/userdata/inclusive/data15/user.data15.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000001.ANALYSIS.root");
      }
      f->GetObject("analysis",tree);

   }
   Init(tree);
}
With two files:

// Declare the names of input files
std::vector<std::string> fileNames = {
     "/userdata/inclusive/data15/user.data15.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000001.ANALYSIS.root",
              "/userdata/inclusive/data15/user..data15.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000002.ANALYSIS.root",
              };


inclphoton::inclphoton(const std::vector<std::string>& fileNames, TTree *tree) : fChain(0)
{
    if (!tree) {
        for (const auto& fileName : fileNames) {
            TFile *f = TFile::Open(fileName.c_str());
            if (!f || !f->IsOpen()) {
                std::cerr << "Error opening file: " << fileName << std::endl;
                continue;
            }
            f->GetObject("analysis", tree);
        }
    }
    Init(tree);
}

Can you please help me to solve this error.

Cheers,
Sandra

Hi Sandra,

What is exactly the error? Is the second file alone working?

D

Hi Danilo,

thanks for your answer!
please find below the error message:

Processing inclphoton.cpp...
input_line_20:2:2: error: no matching constructor for initialization of 'inclphoton'
 inclphoton() /* '.x' tries to invoke a function with the same name as the macro */
 ^
/home/inclusivePhotonAnalysis/inclphoton.h:10:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class inclphoton {
      ^
/home/inclusivePhotonAnalysis/inclphoton.h:285:13: note: candidate constructor not viable: requires at least argument 'fileNames', but no arguments were provided
inclphoton::inclphoton(const std::vector<std::string>& fileNames, TTree *tree) : fChain(0)
// Declare the names of input files
std::vector<std::string> fileNames = {
     "/userdata/inclusive/data15/user.data15.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000001.ANALYSIS.root",
              "/userdata/inclusive/data15/user..data15.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.26552633._000002.ANALYSIS.root",
              };

I changed the declaration of "TFile *f" (***); I put it before the loop and I added the following instruction "TH1::AddDirectory(kFALSE);" but I have got this error message  knowing that I have root 6:24 in m terminal:

/home/sbatlamo/inclusivePhotonAnalysis/inclphoton.cpp:14:6: error: no type named 'AddDirectory' in 'TH1'
TH1::AddDirectory(kFALSE);
~~~~~^




(****)
inclphoton::inclphoton(const std::vector<std::string>& fileNames, TTree *tree) : fChain(0)
{
    if (!tree) {
        TFile *f;
        for (const auto& fileName : fileNames) {
            f = TFile::Open(fileName.c_str());
            if (!f || !f->IsOpen()) {
                std::cerr << "Error opening file: " << fileName << std::endl;
                continue;
            }
            f->GetObject("analysis", tree);
        }
    }
    Init(tree);
}

Hi Sandra,

The first error you report has to do with the code you are executing, which apparently has issues. There is a constructor which is invoked w/o arguments, but the implementation provided requires one. This error should be solved before anything else, and has little to do with ROOT unfortunately.

My advice, which is very general and not strictly related to ROOT, would be to try and reduce the code to a minimal reproducer to debug what is going wrong.

Cheers,
D

Hi Danilo,

I resolved the issue by using the vector<string> outside of the function. Additionally, I addressed the problem related to TH1::AddDirectory(kFALSE). However, a new issue has surfaced. While the code runs without errors, it only processes the last input file and does not provide access to the new TBrowser. I’m currently investigating this issue.

Please find below part of my code (in header file)

std::vector<std::string> fileNames = {
    "/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000001.ANALYSIS.root",
    "/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000002.ANALYSIS.root"
};

inclphoton::inclphoton(TTree *tree) : fChain(0)
{
    //TH1::AddDirectory(kFALSE);
    if (!tree) {
        TFile *f;
        for (const auto& fileName : fileNames) {
           std::cout << "Processing file: " << fileName << std::endl;
           f = TFile::Open(fileName.c_str());
            if (!f || !f->IsOpen()) {
                std::cerr << "Error opening file: " << fileName << std::endl;
                continue;
            }
            f->ls();
            f->GetObject("analysis", tree);
            std::cout<< "////////////////////////////////////////////"<<std::endl;
            std::cout << "Total entries in fChain: " << tree->GetEntries() << std::endl;

            //delete f;
        }
    }
    Init(tree);
}

I have tested the code with only two files so far, but in a broader context, there are a total of 200 files.

Best,
Sandra

Dear Expert,
Can someone give me a guidance to solve my issue.

Your help is very appreciated!

Cheers,
Sandra

Hello Sandra,

Thanks for your perseverance in debugging. This again seems a generic issue of the code you probably inherited from someone else and not ROOT.
Why is only the last file processed? Are there any printouts coming from your code above? It really seems this is not strictly ROOT related…

Cheers,
D

Hello Danilo,

Yes I have these printouts about the information from the files:

Processing file: /userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000001.ANALYSIS.root
TFile**		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000001.ANALYSIS.root	
 TFile*		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000001.ANALYSIS.root	
  KEY: TTree	analysis;1	My analysis ntuple
////////////////////////////////////////////
Total entries in fChain: 997
Processing file: /userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000002.ANALYSIS.root
TFile**		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000002.ANALYSIS.root	
 TFile*		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000002.ANALYSIS.root	
  KEY: TTree	analysis;1	My analysis ntuple
////////////////////////////////////////////
Total entries in fChain: 120
Processing file: /userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000003.ANALYSIS.root
TFile**		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000003.ANALYSIS.root	
 TFile*		/userdata/sbatlamo/inclusivePhoton/data15/user.jterron.data15periodd.n181.periodD.DAOD_STDM2.grp23_v01_p3984_ANALYSIS_ntuples/user.jterron.26552633._000003.ANALYSIS.root	
  KEY: TTree	analysis;1	My analysis ntuple

And this printout is just for last file:
Pt :330.671
Pt :352.939
Pt :340.315
Pt :287.036
Pt :277.789
Pt :254.917
Pt :257.617
Pt :371.323
Pt :378.931
Pt :261.207

Cheers,
S

Hi Sandra,

Thanks. It is really hard to figure out what printed those Pts… The code is not available to us. I perhaps would suggest to use gdb to put a break point and check under what conditions the processing function is invoked.

Best,
D

Hi Danilo,

Please find below the link where I use gdb:
header file:

source file:

Cheers,
S

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