Read in all files in folder and reweight events

I have a directory of files with random names but which can be grouped by their ‘JZ’ number. So for example
JZ1_something1.root
JZ1_something2.root
JZ2_somethingDifferent1.root
JZ3_somethingDifferentAgain1.root…

And so on, with up to 20 files per JZ tag. I need to reweight the momentum by a different factor for each JZ slice - this isn’t a per event weight, I’ve done those, it’s per slice.

I want to write a C++ macro to do this in root, and basically have no idea where to start. The only way I can think is read in the file names and then do ‘if filename contains 1_*.root …’ then give it the factor to multiply by in each case, that kind of idea, but don’t know where to start to implement that.

I could just paste in all the file names but can’t think how that would help either. If I could get the names then I know what to do:

But I can’t figure out how to give it the inputName of every file in the folder. Its at line 2 where I hit problems, because the JZ%s at the start is predictable but then there are multiple files with the same JZ%s files which all have something different afterwards.

char inputName[200];
sprintf(inputName,"J%s_something.root",sample.c_str());
TFile f1(inputName,"update");

    f1.cd();
    TH1F* h_events = (TH1F*) gDirectory->Get("h_events");
    double evn = h_events->GetEntries();
    double xs;
    double eff;

   if (input.Contains("JZ3_******.root")){ xs = 1;  eff = 1; } 
   if(input.Contains("JZ3_******.root")){xs=; eff =1;}
   double reweight = xs*eff/evn;

    double EvweightLC;   double new_weightLC;
    double EvweightEM;   double new_weightEM;

    TTree *LC = (TTree*)f1.Get("AntiKt4LCTopoJets");
    TTree *EM = (TTree*)f1.Get("AntiKt4EMTopoJets");

    TBranch* newBranchLC = LC->Branch("new_EventWeight",&new_weightLC, "new_weightLC/D");
    TBranch* newBranchEM = EM->Branch("new_EventWeight",&new_weightEM, "new_weightEM/D");

    LC->SetBranchAddress("EventWeight",&EvweightLC);
    EM->SetBranchAddress("EventWeight",&EvweightEM);

    int entriesLC = LC->GetEntries();
    int entriesEM = EM->GetEntries();

    for (int i=0; i<entriesLC; i++) {
      LC->GetEntry(i);
      new_weightLC = EvweightLC*reweight;
      newBranchLC->Fill();
    }
    LC->Write("",TObject::kOverwrite);


      for (int i=0; i<entriesEM; i++) {
        EM->GetEntry(i);
        new_weightEM = EvweightEM*reweight;
        newBranchEM->Fill();
      }
      EM->Write("",TObject::kOverwrite);

}

Sorry, I think I haven’t explained that well at all! I’d really appreciate any help and happy to clarify the confusing bits. :slight_smile: Thanks!

I do not know if that’s what you are looking for but TString provides all the tools to compare character strings.