How to delete histogram from TList in root file


May I ask you some help about the following problem I face:

How can I remove a histogram from TList of a root file? The Structure of root as;

TFile**		Out_Data_Dimuon_PbPb_2018_pass1_LHC15o_AOD229_CMUL7_pdca_ptcorrected_withpileup_newalignment_latest.root	
 TFile*		Out_Data_Dimuon_PbPb_2018_pass1_LHC15o_AOD229_CMUL7_pdca_ptcorrected_withpileup_newalignment_latest.root	
  KEY: TList	Dimuon_Dimuon;1	Doubly linked list
  KEY: TList	Trigger_Dimuon;1	Doubly linked list

I want to remove an object “hPercentileminus” which is added to "Dimuon_Dimuon .

I am including the root file herewith.

Thanks in advance,

List"Out_Data_Dimuon_PbPb_2018_pass1_LHC15o_AOD229_CMUL7_pdca_ptcorrected_withpileup_newalignment_latest.root (2.7 MB) .

Hi @Om_Chaudhary,
sorry for the late reply, it seems like this post must have slipped through the cracks! Always feel free to ping us again if you don’t hear back in a few days.

If I understand your question correctly, you have a TList containing histograms in a TFile, and you would like to modify that TList “in place”, in the TFile, so that some histograms it contained are removed from the TFile. Is that correct?

If that’s it, @pcanal probably knows what the simplest way is.

What is your purpose/end-goal?


PS. Technically the answer to your question is, open the file in update mode, read the TList, Remove the object you need removed and write the list to the file again (with the option kOverwrite to avoid wasting space)
but this might not be the best to accomplish what you need.

1 Like

Hello pcanal.
it works for me. Actually I want to add two root files with “hadd”. One of them has an extra object that why I want to delete that one.

Thanks a lot…