Merging ROOT.RooWorkspace

Hi, I am trying to perform the merging of two workspaces which I had to separate due to workspaces constraints while trying to write a big workspace.

I am having two issues while merging it. The first is that I need to merge a field names .allData() which I do by looping over ws.allData().

    # Import all data from both workspaces
    dataList1 = ws1.allData()
    for data in dataList1:
        getattr(merged_ws, 'import')(data, ROOT.RooFit.RecycleConflictNodes())```

But once I try to merge it I keep getting these Erros:

    [#0] ERROR:InputArguments -- RooWorkspace::import(tagsDumper) ERROR: unrecognized command: RecycleConflictNodes
    [#0] ERROR:ObjectHandling -- RooWorkspace::import(tagsDumper) ERROR dataset with name ggh_in_125_13TeV_worst_resolution_energyErrShiftDown01sigma already exists in workspace, import aborted

The second one is that I want the merged workspace to have the same structure as the ones I am merging. And on the two workspaces I am able to merge, I do this:

    file1 = ROOT.TFile.Open(file_path1, "READ")
    file2 = ROOT.TFile.Open(file_path2, "READ")
    
    # Navigate to the tagsDumper directory
    dir1 = file1.Get("tagsDumper")
    dir2 = file2.Get("tagsDumper")
    
    # Access the RooWorkspace from the tagsDumper directory
    ws1 = dir1.Get("cms_hgg_13TeV")
    ws2 = dir2.Get("cms_hgg_13TeV")

But I cannot do the same with the merged workspace. The full code I am using is avaliable below:

    import ROOT

    def list_objects_in_root_file(file_path):
        file = ROOT.TFile.Open(file_path, "READ")
        file.ls()  # This will list all objects in the ROOT file
        file.Close()

    def merge_workspaces(file_path1, file_path2, output_file_path):
        # Open the ROOT files
        file1 = ROOT.TFile.Open(file_path1, "READ")
        file2 = ROOT.TFile.Open(file_path2, "READ")
        
        # Navigate to the tagsDumper directory
        dir1 = file1.Get("tagsDumper")
        dir2 = file2.Get("tagsDumper")
        
        # Access the RooWorkspace from the tagsDumper directory
        ws1 = dir1.Get("cms_hgg_13TeV")
        ws2 = dir2.Get("cms_hgg_13TeV")

        # Check if workspaces are correctly loaded
        if not isinstance(ws1, ROOT.RooWorkspace) or not isinstance(ws2, ROOT.RooWorkspace):
            print("Failed to load RooWorkspace from one of the files.")
            return

        # Open a new ROOT file to store the merged workspace
        output_file = ROOT.TFile(output_file_path, "RECREATE")
        merged_ws = ROOT.RooWorkspace("tagsDumper", "tagsDumper")

        # Import all variables and PDFs
        getattr(merged_ws, 'import')(ws1.allVars(), ROOT.RooFit.RecycleConflictNodes())
        getattr(merged_ws, 'import')(ws1.allPdfs(), ROOT.RooFit.RecycleConflictNodes())
        getattr(merged_ws, 'import')(ws2.allVars(), ROOT.RooFit.RecycleConflictNodes())
        getattr(merged_ws, 'import')(ws2.allPdfs(), ROOT.RooFit.RecycleConflictNodes())

        # Import all data from both workspaces
        dataList1 = ws1.allData()
        for data in dataList1:
            getattr(merged_ws, 'import')(data, ROOT.RooFit.RecycleConflictNodes())

        dataList2 = ws2.allData()
        for data in dataList2:
            getattr(merged_ws, 'import')(data, ROOT.RooFit.RecycleConflictNodes())
            
        # Print all data from the merged workspace
        all_data = merged_ws.allData()
        for data in all_data:
            print(data.GetName())

        # Write the merged workspace to file and close everything
        merged_ws.Write()
        output_file.Close()
        file1.Close()
        file2.Close()

    # Example usage
    merge_workspaces("../input_output_2022postEE/out_play/Chunk1_output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_out.root", 
                    "../input_output_2022postEE/out_play/Chunk2_output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_out.root", 
                    "../input_output_2022postEE/ws_GG2H_out/output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_out.root")

    merge_workspaces("../input_output_2022postEE/in_play/Chunk1_output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_in.root", 
                    "../input_output_2022postEE/in_play/Chunk2_output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_in.root", 
                    "../input_output_2022postEE/ws_GG2H_in/output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_in.root")


    ## Checking again!
    read_file = ROOT.TFile("../input_output_2022postEE/ws_GG2H_in/output_GluGluHToGG_M125_13TeV_amcatnloFXFX_pythia8_GG2H_in.root", "READ")
    print(read_file.ls())
    dir1 = read_file.Get("tagsDumper")
    # Access the RooWorkspace from the tagsDumper directory
    ws1 = dir1.Get("cms_hgg_13TeV")
    print(ws1.allData())
    print("worked!!")

Is there anything I could do to solve/avoid these issues?

Best,
Caio

_ROOT Version: 6.12_07 (due to CMS environment)


Hi Caio,

Welcome to the ROOT community!
I am adding in the loop @jonas , expert and lead developer of RooFit.

Cheers,
Danilo

1 Like

Hi @Danilo , @Jonas I am just pining you in case you forgot to have a look.

Best,
Caio