Apply weight to each events

hi, im trying to apply weights to each of my events for each branch sample

void W(){
  gStyle->SetOptStat(1111);// untk print no of events

 // root file
  TFile *f_sig20 = new TFile("/Users/applestudio/Desktop/rootFILES/4LAnalyzer_h125_4e_zd20.root");
  //TFile *f_sig40 = new TFile("/Users/applestudio/Desktop/rootFILES/4LAnalyzer_h125_4e_zd40.root");
 // TFile *f_sig60 = new TFile("/Users/applestudio/Desktop/rootFILES/4LAnalyzer_h125_4e_zd60.root");
  TFile *f_bkg_zz4lep = new TFile("/Users/applestudio/Desktop/rootFILES/4LAnalyzer_Background_ZZ4LEP500k.root");
  TFile *f_bkg_pph4l = new TFile("/Users/applestudio/Desktop/rootFILES/4LAnalyzer_Background_pph4l500k.root");

  TTree *sig20 = (TTree*) f_sig20->Get("mZb_4e");
  TTree *bkg_zz4lep = (TTree*) f_bkg_zz4lep->Get("mZb_4e");
  TTree *bkg_pph4l = (TTree*) f_bkg_pph4l->Get("mZb_4e");
  /*TH1F *h_sig20 = (TH1F*)f_sig20->Get("mZb_4e")->Clone("h_sig20");
  TH1F *h_sig40 = (TH1F*)f_sig40->Get("mZb_4e")->Clone("h_sig40");
  TH1F *h_sig60 = (TH1F*)f_sig60->Get("mZb_4e")->Clone("h_sig60");
  TH1F *h_bkg_zz4lep = (TH1F*)f_bkg_zz4lep->Get("mZb_4e")->Clone("h_bkg_zz4lep");
  TH1F *h_bkg_pph4l = (TH1F*)f_bkg_pph4l->Get("mZb_4e")->Clone("h_bkg_pph4l");*/
    //250fb = 250000pb
    float lumi = 250000;
    double nevents = 25000;
    double backevents = 500000;
    double BR_SMHiggs = 1.25e-4;
    double xs = 48.58;
    double ATLASUP = 0.01;//based on ATLAS CL upper limit = 10e-3
    double xs_zz4lep = 0.02836;//from mg5
    double xs_pph4lep = 0.0010652;//from mg5
 // normalize using weight to SM Higgs crossection & BR (total events 25k)
    double signal_weight = xs*ATLASUP*BR_SMHiggs*lumi/nevents;
    cout << "signal_weight =" << signal_weight << ".\n";
    double zz4lep_weight = xs_zz4lep*lumi/backevents;
    cout << "zz4lep_weight =" << zz4lep_weight << ".\n";
    double pph4l_weight = xs_pph4lep*lumi/backevents;
    cout << "pph4l_weight =" << pph4l_weight << ".\n";
 //applying weight to first sample branch and creating new branch for weighted events
    TH1F *sig20w = new TH1F("sig20w","sig20w",50,12.0,120.0);
    Long64_t nentries = sig20->GetEntries();
    for(Long64_t i=0; i<nentries; i++)
      sig20w->Fill(sig20->GetEntry(i), signal_weight);
    TH1F *zz4lep_w = new TH1F("zz4lep_w","zz4lep_w",50,12.0,120.0);
    Long64_t Zentries = bkg_zz4lep->GetEntries();
    for(Long64_t i=0; i<Zentries; i++)
      zz4lep_w->Fill(bkg_zz4lep->GetEntry(i), zz4lep_weight);
    TH1F *pph4l_w = new TH1F("pph4l_w","pph4l_w",50,12.0,120.0);
    Long64_t Pentries = bkg_pph4l->GetEntries();
    for(Long64_t i=0; i<Pentries; i++)
      pph4l_w->Fill(bkg_pph4l->GetEntry(i), pph4l_weight);
  //stack normalized samples
  TCanvas *c = new TCanvas("c","c");
 // h_sig40->SetFillColor(kCyan-9);
//  h_sig60->SetFillColor(kYellow-9);
  THStack *hs = new THStack("hs", "z_{b} mass");
  hs->GetXaxis()->SetTitle("mass of z_{b} (4e channel)");
  hs->GetYaxis()->SetTitle("Number of Events");

  /*TLegend *leg = new TLegend(0.9,0.7,0.7,0.9);
  leg->AddEntry("h_sig20","mZ_{b} = 20GeV","f");
  leg->AddEntry("h_sig40","mZ_{b} = 40GeV","f");
  leg->AddEntry("h_sig60","mZ_{b} = 60GeV","f");


however this is my output

signal_weight =0.00060725.
zz4lep_weight =0.01418.
pph4l_weight =0.0005326.

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[<unknown binary>] (no debug info)
[<unknown binary>] (no debug info)
[<unknown binary>] (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TRint::HandleTermInput() (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TUnixSystem::CheckDescriptors() (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TSystem::InnerLoop() (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TSystem::Run() (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TApplication::Run(bool) (no debug info)
[/usr/local/Cellar/root/6.22.02/lib/root/] TRint::Run(bool) (no debug info)
[/usr/local/Cellar/root/6.22.02/bin/root.exe] main (no debug info)
[/usr/lib/system/libdyld.dylib] start (no debug info)
[<unknown binary>] (no debug info)

im not sure where i went wrong,

Please read tips for efficient and successful posting and posting code

_ROOT Version:6.22
Platform: Not Provided
Compiler: Not Provided


are you sure this line does what you want it to do?

The TTree::GetEntry() returns the number of bytes, not some meaningful value written in some branch inside that TTree of yours.

i went through Event-by-event weighting and saving as a branch at TTree but it doesnt seem like a good example

Long64_t nentries = tree1->GetEntries(); //get total number of enteries
 for(Long64_t i=0; i< nentries; i++){ // loop over all of them
	 tree1->GetEntry(i); // Load the data for TTree entry number "i" from branch
	 D_ETA_w = D_ETA * weight;

i dont understand how


correlates with

D_ETA_w = D_ETA * weight;

also in this case it draws the tree, but n my case i want to weight each event, so im not sure what variable is should fill in,
h->Fill(variable, weight)
and stack the histograms,

do you have any suggestion how i may fix this?


the first line, tree1->GetEntry(i) basically makes the jump to the next entry in your TTree. Whatever variables you use after this point will be populated with information from this new entry. So by doing D_ETA_w = D_ETA * weight in that example they reread the values D_ETA and weight from that new entry and calculate their product. So in your case instead of

sig20w->Fill(sig20->GetEntry(i), signal_weight);

you need to fill some value (I don’t know which one, but definitely not the sig20->GetEntry(i) as it makes no sense). The weight seems to be correct as it’s just a constant which does not depend on whatever you may have in your TTree.