Filling maps and vectors in a single tree

I have been trying to fill a single tree with branches as vectors and maps.
However, when I try to read the tree, it shows segmentation fault.
Any help will be appreciated.
Thanks!

Example code:

//-----------------------------------------------------------------------------
#include "TFile.h"
#include "TTree.h"
#include "vector"

void check1() {

//Write --------------------------------------------------------

  std::vector<double> U1j;
  map<string, double> ampl ;
  //double mm;

  TFile* fileout = new TFile("readcheck.root", "RECREATE");
  TTree* tree = new TTree("Norm","");
  tree->Branch("U1j", &U1j);
  //tree->Branch("mm", &mm, "mm/D");
  tree->Branch("ampl", &ampl);

  for(Int_t i=0; i<5; i++) 
     { ampl.clear();
       U1j.clear();
       ampl.insert(pair<string, double>("kstar892",1.0*i));
       ampl.insert(pair<string, double>("rho770",2.0*i));  
       ampl.insert(pair<string, double>("omega782",3.0*i));
       //mm = i*10.0;
       for(int j=0; j<3; j++)
          { U1j.push_back(i*20.0);
          }
       tree->Fill();
     }

  fileout->Write();
  fileout->Close();

//Read -----------------------------------------------------------

  std::vector<double> *U1j = 0 ;
  map<string, double> *Ampl ;
  //double MM;

  TFile* filein = new TFile("readcheck.root");
  TTree* t1;
  t1 = (TTree*)filein->Get("Norm");
  t1->SetBranchAddress("U1j",&U1j);
  t1->SetBranchAddress("ampl",&Ampl);
  //t1->SetBranchAddress("mm", &MM);

  for(int i=0; i<t1->GetEntries(); i++)
     { t1->GetEntry(i);
       cout<<"Amplitude[kstar892]: "<<(*Ampl)["kstar892"]<<endl;
       for(int j=0; j<3; j++)
          { cout<<"U1j[j]: "<<(*U1j)[j]<<endl;;
          }

       cout<<endl;
     }

}
//------------------------------------------------------------------------------

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


//-----------------------------------------------------------------------------
#include "TFile.h"
#include "TTree.h"

#include <vector>
#include <string>
#include <map>
#include <utility> // std::pair
#include <iostream>

//Write --------------------------------------------------------
void check1_write() {
  std::vector<double> U1j;
  std::map<std::string, double> ampl;
  //double mm;

  TFile *fileout = TFile::Open("readcheck.root", "RECREATE");
  TTree *tree = new TTree("Norm","");
  tree->Branch("U1j", &U1j);
  //tree->Branch("mm", &mm, "mm/D");
  tree->Branch("ampl", &ampl);

  for(Int_t i=0; i<5; i++) 
     { ampl.clear();
       U1j.clear();
       ampl.insert(std::pair<std::string, double>("kstar892",1.0*i));
       ampl.insert(std::pair<std::string, double>("rho770",2.0*i));  
       ampl.insert(std::pair<std::string, double>("omega782",3.0*i));
       //mm = i*10.0;
       for(int j=0; j<3; j++)
          { U1j.push_back(i*20.0);
          }
       tree->Fill();
     }

  fileout->Write();
  fileout->Close();

}

//Read -----------------------------------------------------------
void check1_read() {
  std::vector<double> *U1j = 0 ;
  std::map<std::string, double> *Ampl = 0 ;
  //double MM;

  TFile *filein = TFile::Open("readcheck.root");
  TTree *t1;
  filein->GetObject("Norm", t1);
  t1->SetBranchAddress("U1j",&U1j);
  t1->SetBranchAddress("ampl",&Ampl);
  //t1->SetBranchAddress("mm", &MM);

  for(int i=0; i<t1->GetEntries(); i++)
     { t1->GetEntry(i);
       std::cout<<"Amplitude[kstar892]: "<<(*Ampl)["kstar892"]<<std::endl;
       for(int j=0; j<3; j++)
          { std::cout<<"U1j[j]: "<<(*U1j)[j]<<std::endl;;
          }

       std::cout<<std::endl;
     }

}

//Write + Read --------------------------------------------------------
void check1() {
  check1_write();
  check1_read();
}
//------------------------------------------------------------------------------

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Thank you, Wile. This helps.
I see that initializing map makes the difference i.e., std::map<std::string, double> *Ampl = 0;
Thanks, again!
Anita

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