Hi, I’ve got a function that takes as an argument a const reference to a TVectorD. I use a const reference because I don’t want to copy the data, but I also don’t want to modify it. The function is supposed to draw the TVectorD onto a canvas (along with some lines), save to a file, then return nothing. When I try to compile the code, I get an error about how Draw is not const:
/Users/jfcaron/Projects/Proto2BeamTest2/./CC_algorithms.C:29:5: error: member function 'Draw' not viable: 'this'
argument has type 'const TVectorD' (aka 'const TVectorT<Double_t>'), but function is not marked const
quantity.Draw();
^~~~~~~~
/Users/jfcaron/Software/custom_root/compiled/include/root/TVectorT.h:182:9: note: 'Draw' declared here
void Draw (Option_t *option=""); // *MENU*
^
Is there a good reason why the TVectorD::Draw method is not const? Does it actually modify the TVectorD? It’d be nice if we could make it const, then it would work regardless of the constness of my own function arguments. Here is my function to see what I am doing:
#include "TVectorD.h"
#include "TString.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TLine.h"
#include <vector>
void makeplot(const TVectorD& quantity, const std::vector<Int_t>& cluster_positions,Double_t thresh,TString label)
{
TCanvas c1("c1",label.Data());
c1.cd();
quantity.Draw();
TLine threshline;
threshline.SetLineColor(kRed);
threshline.SetLineWidth(1);
Int_t n_samples = quantity.GetNoElements();
threshline.DrawLine(0,-1.0*thresh,n_samples,-1.0*thresh);
gPad->Modified();
gPad->Update();
c1.SaveAs(TString::Format("plots/%s_%f.pdf",label.Data(),thresh));
c1.SaveAs(TString::Format("plots/%s_%f.root",label.Data(),thresh));
}
A workaround would also be appreciated in the meantime. I guess I could just pass the TVectorD as a non-const reference and promise myself that I won’t change it, but hopefully there is a more C+±ey way to do it.
Jean-François