How to add vector<TLorentzVector> into a new clone tree correctly?

Hello,
I am trying to add a new branch of vector into a new file with a new clone tree. but it shows an error about: Error in <TTree::Branch: The class requested (vector) for the branch “Vec_Lv_jets” is an instance of an stl collection and does not have a compiled CollectionProxy. Please generate the dictionary for this collection (vector) to avoid to write corrupted data.

#define addIndexVecJets_cxx
#include "addIndexVecJets.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TLorentzVector.h>
#include <vector>
#ifdef __MAKECINT__ 
#pragma link C++ class vector<TLorentzVector>+;
#endif
void addIndexVecJets::Loop()
{
//   In a ROOT session, you can do:
//      root> .L addIndexVecJets.C
//      root> addIndexVecJets t
//      root> t.GetEntry(12); // Fill t data members with entry number 12
//      root> t.Show();       // Show values of entry 12
//      root> t.Show(16);     // Read and show values of entry 16
//      root> t.Loop();       // Loop on all entries
//

//     This is the loop skeleton where:
//    jentry is the global entry number in the chain
//    ientry is the entry number in the current Tree
//  Note that the argument to GetEntry must be:
//    jentry for TChain::GetEntry
//    ientry for TTree::GetEntry and TBranch::GetEntry
//
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fChain->SetBranchStatus("*",0);  // disable all branches
//    fChain->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fChain->GetEntry(jentry);       //read all branches
//by  b_branchname->GetEntry(ientry); //read only this branch
   if (fChain == 0) return;
   std::vector<TLorentzVector> Vec_Lv_jets;
   Long64_t nentries = fChain->GetEntriesFast();
   TFile Newfile("test.root","recreate");
   auto NewTree = fChain->CloneTree(0);
   NewTree->Branch("Vec_Lv_jets",&Vec_Lv_jets);
   Long64_t nbytes = 0, nb = 0;
   gInterpreter->GenerateDictionary("vector<TLorentzVector>", "vector");
   for (Long64_t jentry=0; jentry<nentries;jentry++) 
   {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      // if (Cut(ientry) < 0) continue;
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet1_pt,
                                       FullyResolved_Jet1_eta,
                                       FullyResolved_Jet1_phi,
                                       FullyResolved_Jet1_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet2_pt,
                                       FullyResolved_Jet2_eta,
                                       FullyResolved_Jet2_phi,
                                       FullyResolved_Jet2_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet3_pt,
                                       FullyResolved_Jet3_eta,
                                       FullyResolved_Jet3_phi,
                                       FullyResolved_Jet3_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet4_pt,
                                       FullyResolved_Jet4_eta,
                                       FullyResolved_Jet4_phi,
                                       FullyResolved_Jet4_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet5_pt,
                                       FullyResolved_Jet5_eta,
                                       FullyResolved_Jet5_phi,
                                       FullyResolved_Jet5_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet6_pt,
                                       FullyResolved_Jet6_eta,
                                       FullyResolved_Jet6_phi,
                                       FullyResolved_Jet6_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet7_pt,
                                       FullyResolved_Jet7_eta,
                                       FullyResolved_Jet7_phi,
                                       FullyResolved_Jet7_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet8_pt,
                                       FullyResolved_Jet8_eta,
                                       FullyResolved_Jet8_phi,
                                       FullyResolved_Jet8_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet9_pt,
                                       FullyResolved_Jet9_eta,
                                       FullyResolved_Jet9_phi,
                                       FullyResolved_Jet9_M);
      Vec_Lv_jets.push_back(TLorentzVector(0,0,0,0));
      Vec_Lv_jets.back().SetPtEtaPhiM(FullyResolved_Jet10_pt,
                                       FullyResolved_Jet10_eta,
                                       FullyResolved_Jet10_phi,
                                       FullyResolved_Jet10_M);
      NewTree->Fill();
      Vec_Lv_jets.clear();
   }
   // NewTree->AutoSave();
   NewTree->Write();
   Newfile.Write();
   Newfile.Close();
}

Try to move the “GenerateDictionary” call to a place before creating the new branch.
If it doesn’t help, create a “rootlogon.C” file with this call (so that ROOT loads this dictionary before it runs your macro).

Thanks for your advice Wile, it now works since I added “++” when compile .L macro.C++.

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