Add the trees of a root chain with different weights

Hello

I want to create a chain with several files, each one representing a different physics channel, with its own cross-section. I need to weight each channel with Luminosity, cross-section and the total number of generated events.

The way I am doing it right now is :

I have got .C and .h file using MakeClass() method .
[color=red]“myEvent” : Tree name
"zjet1DBS_15to20.root" & “zjet2DBS_15to20.root” : filenames[/color]

In .h file, before adding the root files in the chain (inside the constructor), I do the following for each file :

TFile f1 = TFile::Open(“zjet2DBS_15to20.root”,“UPDATE”);
tree = (TTree
)f1->Get(“myEvent”);
tree->SetWeight(0.4);
tree->AutoSave();

TFile f2 = TFile::Open(“zjet2DBS_15to20.root”,“UPDATE”);
tree = (TTree
)f2->Get(“myEvent”);
tree->SetWeight(0.6);
tree->AutoSave();

Then I add the file as follows :

Chain = new TChain(“myEvent”);
Chain->Add(“zjet1DBS_15to20.root”);
Chain->Add(“zjet2DBS_15to20.root”);

Now if I loop over events in my .C file and plot a histogram, I don’t see any changes due to setweight method. But if I print the weight of each file using fChain->GetWeight() method, it shows the correct new set values i.e. 0.4 and 0.6 respectively.

Could anyone please help me out in resolving this issue?

Thanks in advance,

Shilpi Jain

Now if I loop over events in my .C file and plot a histogramHow do you do that? Are you using a technique that makes use of the TTree weight?

Cheers,
Philippe

Hello,

Thanks much for the reply.
I am attaching my .C and .h files. These have been produced using a MakeClass().
In my .h, before adding the root files to chain, I open every root file in the constructor and call SetWeight() method to set the weight of the tree.
To loop over events in my .C file I first access the GetEntriesFast() method to get number of entries in the TChain and then start looping over it.
Then inside the loop I call the GetEntry() method to fill the branch variables and thus I have got access over a tree variable.
Then I plot a histogram using hist->Fill() and then hist->Draw() methods.
Is doing that way making a difference?
I checked that just now, if I do TTree->Draw(“variable>>hist”), then weight is showing up in the histogram and if I do hist->Fill() and then Draw() then weight is not showing up.
Could you please tell me why is it happening?
Thanks & Regards,
Shilpi
zgam_0jet.h (34.7 KB)
zgam_0jet.C (1.64 KB)

Hi,

If you are filling the histogram directly you must also explicitly pass the weight (i.e the histogram has no way to know which TTree the data you are passing is coming from):hist_genphotonpt->Fill(gen_Zdaughtereta[0],fChain->GetTree()->GetWeight());

Cheers,
Philippe.

Hello,

Thanks much for the help. Its working now :slight_smile:

Thanks & Regards,
Shilpi

Hello,

I have another question to ask. I am opening root files at the beginning, setting their weights, then before closing the files( after I have plotted the hitos), I am resetting their weights to 1.

If I again run the same code, then it shows every weight = 1.

If I don’t reset the weights back to 1 at the end, then on rerunning the code, it takes proper weights.
Is it a problem to set and reset the weights very frequently?
I am attaching my .h and .C files

Thanks & Regards,
Shilpi
zgam_0jet_DBS.C (6.79 KB)
zgam_0jet_DBS.h (39.3 KB)

Hi,

A priori, it is not a problem. However personally I would be concerned about losing track on whether (and which) the weights have been set or not set.

Cheers,
Philippe.

Hello,

Thanks again for the reply.
But I am facing problems with this.
As I have mentioned, if I at the beginning (before adding root trees) set the weights to some value and then at the end (after plotting everything) reset the weights back to 1, then if I see the histos, all histos show weights = 1.

But if I do not reset the weights to 1 at the end, then it takes proper weights as I have set at the beginning.

Could you help me resolving this issue?

Thanks & Regards,
Shilpi

[quote]As I have mentioned, if I at the beginning (before adding root trees) set the weights to some value and then at the end (after plotting everything) reset the weights back to 1, then if I see the histos, all histos show weights = 1.[/quote]I don’t see the code you are referring to (in particular how you reset the trees weight). Reseting the TTree can a priori not have any effect on already filled histograms so there is something really odd here (which is not obvious from your code).

Cheers,
Philippe.

Hello,

Thanks again.
I think I need to check my code once again.
But is it advisable to change the weights of the trees?
I have also attached my .C and .h file.

Thanks,
Shilpi
zgam_0jet_DBS.h (39.3 KB)
zgam_0jet_DBS.C (6.79 KB)

Hi,

[quote]But is it advisable to change the weights of the trees? [/quote]I would not. I would find a different technique.

Anyway, your code looks okay. Is it mis-behaving, if so, how does it mis-behave? (be specific).

Cheers,
Philippe.

Hello,

This code does not set the weights correctly sometimes (if I rerun it).
This code sometimes show the weight as 1 in the histograms and sometimes as the values which I have given in the beginning (which is 1 for first root tree and 0.3 for the second).
So for the second tree it sometimes (if I rerun the code) take the weight as 1 and not 0.3.

But now I think I should not go with this setweight method because I ran this code 7 or 8 times and it has destroyed one tree. In one of rootfiles, it can no more find any tree. If I open that root file in TBrowser, it says R__unzip error.

Thanks much for your advice and help :slight_smile:

Thanks,
Shilpi

Hi,
I am trying to do something like that.

[quote=“Shilpi”]Hello

TFile f1 = TFile::Open(“zjet2DBS_15to20.root”,“UPDATE”);
tree = (TTree
)f1->Get(“myEvent”);
tree->SetWeight(0.4);
tree->AutoSave();

TFile f2 = TFile::Open(“zjet2DBS_15to20.root”,“UPDATE”);
tree = (TTree
)f2->Get(“myEvent”);
tree->SetWeight(0.6);
tree->AutoSave();
[/quote]

but I found it pretty dangerous infact opening the file in “update” mode, in case of any crash there is a risk
or corrupting the file, or, at last this is what I experienced.
When I try to read again the file I get:
R__unzip: error in header

is it possible?
is there another solution to the problem or a safe procedure to not corrupt the files?

cheers,
delo

Hi Delo,

Indeed if you update the ROOT file (well any file) there is a risk that if something goes wrong (physical error in the hard drive, random crash due to any problem in your code or the library you use), the result would be a corrupted file.

The only safe solution (short of never having bug in your code or never tickling bug in the library code :slight_smile:) is to make a backup copy of the file. You can for example using the shell command cp or even TFile::Cp to make the copy or you could open the file read only and copy the content into a new file. For example:TFile *f1 = TFile::Open("zjet2DBS_15to20.root","READ"); TTree *tree = 0; f1->GetObject("myEvent",tree); TFile *output = TFile::Open("zjet2DBS_15to20_v2.root","NEW"); TTree *newtree = tree->CloneTree(-1,"fast"); newtree->SetWeight(0.4); output->Write(); delete f1; delete output;

Cheers,
Philippe.

ok I understand,
but this works if you don’t have disk space problems and you can replicate your file.

The other problem is that sometimes it is not possible to open in update mode
(i.e. if you run on a file which lives in other user’s directory)

cheers,
delo

Hi Delo,

[quote]The other problem is that sometimes it is not possible to open in update mode
[/quote]In this case you should definitively use the technique I outlined.

[quote]but this works if you don’t have disk space problems and you can replicate your file.
[/quote]Yes, if you are low in disk space and not able to duplicate the file you will need to update the file. A priori the failure you described earlier should be rare.

Cheers,
Philippe.

If you don’t have enough disk space to rewrite the file, then I claim you don’t have the resources to do your job. You really (really) don’t want to open one root file and then try to modify it. There are all sorts of bad things that are likely to happen and you may not notice some of them. ROOT files are really WORMs (write once read many). Using them otherwise is really just asking for trouble.

Cheers,
Charles