Writing 2D-histogram projections to a new ROOT File


_ROOT Version: 6.17/01
_Platform: linuxx8664gcc
_Compiler: gcc version 8.3.1 20190223


Hello,

I have a file (gg_all_4k_100ns.root) which contains 2D-histograms (his2D_clean etc). I take, say Y-projections/slices from one of the histograms. This gives me several spectra with name starting with “pY”. Now, I want to write ONLY these spectra to a new file. Can you please guide me on how to achieve this.

Please note that - I DO NOT want my original file to be modified!

Here is what I have been doing:

root [0] .L can2root_utils_cc.so
root [1] loadF("gg_all_4k_100ns.root");
root [2] .ls
TFile**		gg_all_4k_100ns.root	
 TFile*		gg_all_4k_100ns.root	
  KEY: TH2F	his2D;1	gamma-gamma matrix
  KEY: TH2F	his2D_rand;1	Random gamma-gamma matrix
  KEY: TH2F	his2D_sub_rand;1	his2D minus his2D_rand
  KEY: TH2F	his2D_2Dbgsub;1	2D background subtracted his2D
  KEY: TH2F	his2D_rand_2Dbgsub;1	2D background subtracted his2D_rand
  KEY: TH2F	his2D_sub_rand_2Dbgsub;1	2D background subtracted his2D_sub_rand
  KEY: TH2F	his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
root [3] setGateX("his2D_clean",528);
Gate on 528keV.  X-Axis limits are 1053 : 1061

Found 0 peaks 
root [4] setGateX("his2D_clean",442);
Gate on 442keV.  X-Axis limits are 881 : 889

Found 0 peaks 
root [5] setGateX("his2D_clean",670);
Gate on 670keV.  X-Axis limits are 1337 : 1345

Found 0 peaks 
root [6] setGateX("his2D_clean",451);
Gate on 451keV.  X-Axis limits are 899 : 907

Found 0 peaks 
root [7] .ls
TFile**		gg_all_4k_100ns.root	
 TFile*		gg_all_4k_100ns.root	
  OBJ: TH2F	his2D_clean	2Dbg and 2Drand_GG subtracted his2D : 0 at: 0x1a343f0
  OBJ: TH1D	pY_528_his2D_clean	2Dbg and 2Drand_GG subtracted his2D : 0 at: 0x1694810
  OBJ: TH1D	pY_442_his2D_clean	2Dbg and 2Drand_GG subtracted his2D : 0 at: 0x14fb650
  OBJ: TH1D	pY_670_his2D_clean	2Dbg and 2Drand_GG subtracted his2D : 0 at: 0x1b134d0
  OBJ: TH1D	pY_451_his2D_clean	2Dbg and 2Drand_GG subtracted his2D : 0 at: 0x1bdb220
  KEY: TH2F	his2D;1	gamma-gamma matrix
  KEY: TH2F	his2D_rand;1	Random gamma-gamma matrix
  KEY: TH2F	his2D_sub_rand;1	his2D minus his2D_rand
  KEY: TH2F	his2D_2Dbgsub;1	2D background subtracted his2D
  KEY: TH2F	his2D_rand_2Dbgsub;1	2D background subtracted his2D_rand
  KEY: TH2F	his2D_sub_rand_2Dbgsub;1	2D background subtracted his2D_sub_rand
  KEY: TH2F	his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
root [8] gSystem->Exec("rootcp gg_all_4k_100ns.root:pY* test.root");
WARNING: can't find pY* in gg_all_4k_100ns.root

I have defined different macros in my custom macro file : can2root_utils.cc

Any help is highly appreciated.

With best regards,

Ajay

Simply open a new file and write your histogram into it. This is very basic, what information is missing?

Thank you very much for your comment. I am still in a learning phase, and do not use ROOT quite often. So, I am sorry if you find my questions too naive.

The following method works. Is that what you suggest? But then it is too cumbersome, since I have to copy each and every histogram one-by-one manually. Is there more elegant way to achieve this?

root [1] loadF("gg_all_4k_100ns.root");
root [2] .ls
TFile**		gg_all_4k_100ns.root	
 TFile*		gg_all_4k_100ns.root	
  KEY: TH2F	his2D;1	gamma-gamma matrix
  KEY: TH2F	his2D_rand;1	Random gamma-gamma matrix
  KEY: TH2F	his2D_sub_rand;1	his2D minus his2D_rand
  KEY: TH2F	his2D_2Dbgsub;1	2D background subtracted his2D
  KEY: TH2F	his2D_rand_2Dbgsub;1	2D background subtracted his2D_rand
  KEY: TH2F	his2D_sub_rand_2Dbgsub;1	2D background subtracted his2D_sub_rand
  KEY: TH2F	his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
root [3] setGateX("his2D_clean",528);
Gate on 528keV.  X-Axis limits are 1053 : 1061

Found 0 peaks 
root [4] setGateX("his2D_clean",442);
Gate on 442keV.  X-Axis limits are 881 : 889

Found 0 peaks 
root [5] setGateX("his2D_clean",670);
Gate on 670keV.  X-Axis limits are 1337 : 1345

Found 0 peaks 
root [6] setGateX("his2D_clean",451);
Gate on 451keV.  X-Axis limits are 899 : 907

Found 0 peaks 
root [7] openF("histos.root");
root [8] TH1D* pY_528 = (TH1D*)RFile->Get("pY_528_his2D_clean");
root [9] pY_528->Write();
root [10] TH1D* pY_442 = (TH1D*)RFile->Get("pY_442_his2D_clean");
root [11] pY_442->Write();
root [12] TH1D* pY_670 = (TH1D*)RFile->Get("pY_670_his2D_clean");
root [13] pY_670->Write();
root [14] TH1D* pY_451 = (TH1D*)RFile->Get("pY_451_his2D_clean");
root [15] pY_451->Write();
root [16] .ls
TFile**		histos.root	
 TFile*		histos.root	
  KEY: TH1D	pY_528_his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
  KEY: TH1D	pY_442_his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
  KEY: TH1D	pY_670_his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
  KEY: TH1D	pY_451_his2D_clean;1	2Dbg and 2Drand_GG subtracted his2D
root [17] newf->Write();
root [18] .q

I hope I am able to convey my problem.

Regards,

Ajay

There are several ways. One could be something like:

TH1D *hist;
string names[] = {"pY_528_his2D_clean", "pY_442_his2D_clean", "pY_670_his2D_clean", "pY_451_his2D_clean"};
TFile *outfile = TFile::Open("histos_out.root", "RECREATE");
for (const string &name : names) {
   RFile->GetObject(name.c_str(), hist);
   outfile->cd();
   if (hist) hist->Write();
}

Thank you very much for your quick reply.

What if I have to copy several spectra starting with say pY_? Again putting them in a string names[] would be ugly. In that case I would like to use a wildcard i.e. to copy all the histograms which start with pY_.

OK, so one could loop over all the keys in the original file, check if its name starts with “pY_”, and if it is the case save it in the output file

Thank you very much for your reply. I have found the following workaround, which for my purpose is the easiest and quickest one.

(1) Use rootcp to copy all the content of gg_all_4k_100ns.root to some another file, say test.root

(2) Now open test.root using an UPDATE option and take projections/slices etc, and then save the resulting file.

(3) Again, use rootcp to copy the histograms from the above file starting with say pY_ to the desired file.

Regards,

Ajay

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