I’m trying to plot a TMultiGraph where each graph is a RHX vs seconds. This is the code I’m using to do it:
void drawColumns(const char* filename){
TTree* tree = new TTree;
tree->ReadFile(filename,"");
int nSensors = 8;
TMultiGraph* mg = new TMultiGraph;
for (int iSensor = 0; iSensor < nSensors; ++iSensor) {
int col = kRed;
tree->Draw(Form("RH%d:seconds>>h%d", iSensor+1, iSensor), "", "goff");
TGraph* g = (TGraph*) gDirectory->Get(Form("h%d", iSensor));
TGraph* cg = (TGraph*) g->Clone();
cg->SetLineColor(col);
cg->SetMarkerColor(col);
mg->Add(cg,"LP");
printf("cg = %x\n", cg);
}
TCanvas* c = new TCanvas("c");
mg->Draw("A");
}
Unfortunately the last line causes a segfault:
#5 0x0000000102b9f19a in TH2F::AddBinContent ()
#6 0x0000000102bceb0f in TMultiGraph::Paint ()
#7 0x0000000101721e5b in TPad::PaintModified ()
#8 0x0000000101707314 in TCanvas::Update ()
#9 0x000000010001ed5e in TCint::UpdateAllCanvases ()
Yes. But what I wonder is - why does this method work and does not segfault, while cloning the TGraph segfaults. If you toggle the segFault param in the getGraph function, when it is set to true, the code segfaults. When it’s false, the code works.
In your first post you were not calling gPad->GetPrimitive, but gDirectory->Get !!
When you do Tree.Draw(“x:y>h”) you fill a histogram. In your case this is not required since you can take directly the vector of points in tree.GetV1, V2, etc. No need to create a histogram and an intermediate graph.
Proceed as I indicated.