Store and process histograms from two files

I have two root files each with a matrix (not trees), I have to process those matrices and compare the histograms I created.
First I open the first file:

Then I do the projection for my specific selection:

Of course this create the new histogram in the current directory, I tried to decouple my new histogram from the current directory with:

and then I open the other file:

and do the projection of this one

I need to compare and analyze those two projections so I would like to access them at the same time and not going back and forth because I have many other matrices and projections to look, but even if I decouple the first one I can not see it (.ls) and plot it again. Since I have to do several operations with the histograms I could either find a way to save them in another root file but I’ve been trying to do several similar things for a while now and could not find a solution.

Thanks a lot for the help.


// ... TH1D *m1_ang20 = m1_py->ProjectionY("m1_ang20", 20, 21); if (m1_ang20) m1_ang20->SetDirectory(gROOT); // (gROOT) or (0) // ... TH1D *m2_ang20 = m2_py->ProjectionY("m2_ang20", 20, 21); if (m2_ang20) m2_ang20->SetDirectory(gROOT); // (gROOT) or (0) // ...

Thanks, now I can draw the together with no problem, but also I want to save them in a new root file, I do:

TFile file("all_matrix.root","NEW");

but the file is always empty … what I’m doing wrong?

Try: TFile file("all_matrix.root", "RECREATE"); m1_ang20->Write(); m2_ang20->Write(); file->Close();