I'm trying to make histograms from a specific file I was given but I'm getting the following error:
Error in <TTree::SetBranchAddress>: The pointer type given "Double_t" (8) does not correspond to the type needed "vector<float>" by the branch: mu_phi
I'm guessing the issue is that the variables I'm trying to put in are in form of vectors but I have no idea how to fix it. The canvas I get after running this (all values whether it's eta, phi, energy...) just contain the number of entries, mean value and lines representing that and they all look the same.
I'm new at this so bear with me. Any help would be appreciated!
My script (without headers):
void script(){
TFile *file = TFile::Open("output_data.root");
TTree* tree = (TTree*)file->Get("nominal");
TChain* fchain = new TChain("nominal");
fchain->Add("output_data.root");
Double_t mu_pt, mu_e, mu_eta, mu_phi;
//set branches to variables
tree->SetBranchAddress("mu_pt", &mu_pt);
tree->SetBranchAddress("mu_e", &mu_e);
tree->SetBranchAddress("mu_eta", &mu_eta);
tree->SetBranchAddress("mu_phi", &mu_phi);
TH1D *muPt = new TH1D("muPt", ";p_{T} [GeV/c];Events", 100, 0., 200.);
TH1D *muEta = new TH1D("muEta", ";#eta;Events", 50, -3, 3);
TH1D *muE = new TH1D("muE", ";Energy;Events", 50, 0, 200);
TH1D *muPhi = new TH1D("muPhi", ";#phi;Events", 50, -4, 4);
TH1D *mass = new TH1D("mass", ";mass;Events", 50, 0, 200);
Long64_t nentries = fchain->GetEntries();
for (int i=0; i<nentries ; i++) {
Long64_t ientry = fchain->LoadTree(i);
if (ientry <0 ) break;
fchain->GetEntry(i);
muEta->Fill(mu_eta);
muPt->Fill(mu_pt);
muPhi->Fill(mu_phi);
muE->Fill(mu_e);
}
cout << "entries = " << nentries << endl;
TCanvas *c1 = new TCanvas("Eta","Eta",800 ,800);
c1->cd();
muEta->Draw();
}
ROOT Version: 6.26/10
Platform: Not Provided
Compiler: Not Provided
I tried switching Double_t for vector<float> but that gave me more errors.
error: no matching member function for call to 'Fill'
muEta->Fill(mu_eta);
~~~~~~~^~~~
/snap/root-framework/838/usr/local/include/TH1.h:219:21: note: candidate function not viable: no known conversion from 'vector<float>' to 'Double_t' (aka 'double') for 1st argument
virtual Int_t Fill(Double_t x);
^
/snap/root-framework/838/usr/local/include/TH1.h:220:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
virtual Int_t Fill(Double_t x, Double_t w);
^
/snap/root-framework/838/usr/local/include/TH1.h:221:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
virtual Int_t Fill(const char *name, Double_t w);
There probably is a different way to approach this whole problem I just can't find it.
Related
I am trying to plot the invariant mass of the Z boson from two decayed muons. I am using MadGraph and Root. MadGraph simulates the events (p p > Z > mu+ and mu-) and creates a .root file that contains the events. I called the simulation eventgeneration.
To use Root to analyze the data and draw the histogram, I have to write my code in C++. Here is the code:
#ifdef __CLING__
R__LOAD_LIBRARY(libDelphes)
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TH1F.h"
#include <iostream>
#include "classes/DelphesClasses.h"
#include "external/ExRootAnalysis/ExRootTreeReader.h"
#include "external/ExRootAnalysis/ExRootResult.h"
#else
class ExRootTreeReader;
class ExRootResult;
using namespace std;
#endif
void readEvents(const char *inputFile)
{
gSystem->Load("libDelphes");
TChain chain("Delphes");
chain.Add(inputFile);
ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
Long64_t numberOfEntries = treeReader->GetEntries();
TTree *tree = new TTree(&chain);
TClonesArray *branchMuon = treeReader->UseBranch("Muon");
//Long64_t numberOfMuons = branchMuon->GetEntries();
TClonesArray *branchElectron = treeReader->UseBranch("Electron");
Long64_t numberOfElectrons = branchElectron->GetEntries();
cout << "There are " << numberOfEntries << " entries in your ntuple" << endl;
UInt_t Muons;
Float_t MuonEta1;
Float_t MuonPhi1;
Float_t MuonPt1;
Float_t MuonEta2;
Float_t MuonPhi2;
Float_t MuonPt2;
tree->SetBranchAddress("MuonEta1", &MuonEta1);
tree->SetBranchAddress("MuonPhi1", &MuonPhi1);
tree->SetBranchAddress("MuonPt1", &MuonPt1);
tree->SetBranchAddress("MuonEta2", &MuonEta2);
tree->SetBranchAddress("MuonPhi2", &MuonPhi2);
tree->SetBranchAddress("MuonPt2", &MuonPt2);
TH1F *histDiMuonMass = new TH1F("mass", "M_{inv}(mu+[1], mu-[2]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
for(Int_t entry = 0; entry < numberOfEntries; ++entry)
{
treeReader->ReadEntry(entry);
Long64_t numberOfMuons = branchMuon->GetEntries();
//Muons += entry;
//Muon *muon1, *muon2, *muon3, *muon4;
TLorentzVector muon1;
TLorentzVector muon2;
muon1.SetPtEtaPhiM(MuonPt1, MuonEta1, MuonPhi1, 0.1);
muon2.SetPtEtaPhi(MuonPt2, MuonEta2, MuonPhi2, 0.1);
if(branchMuon->GetEntries() == 4)
{
TLorentzVector Z = muon1 + muon2;
histDiMuonMass->Fill(Z.M());
}
}
cout << "There are " << Muons << " entries in your ntuple" << endl;
histDiMuonMass->Draw();
}
Then in the root terminal I type: \
.x examples/readEvents.C("../eventgeneration/Events/run_01/tag_1_delphes_events.root")
However, I get the following error:
In file included from input_line_1563:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/readEvents.C:30:20: error: no matching constructor for initialization of 'TTree'
TTree *tree = new TTree(&chain);
^ ~~~~~~
/home/cucip/builddir/include/TTree.h:295:4: note: candidate constructor not viable: no known conversion from 'TChain *' to 'const TTree' for 1st argument; remove &
TTree(const TTree& tt) = delete;
^
/home/cucip/builddir/include/TTree.h:291:4: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
TTree();
^
/home/cucip/builddir/include/TTree.h:292:4: note: candidate constructor not viable: requires at least 2 arguments, but 1 was provided
TTree(const char* name, const char* title, Int_t splitlevel = 99, TDirectory* dir = gDirectory);
^
In file included from input_line_1563:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/readEvents.C:72:8: error: no member named 'SetPtEtaPhi' in 'TLorentzVector'
muon2.SetPtEtaPhi(MuonPt2, MuonEta2, MuonPhi2, 0.1);
Does anybody see what could cause this error and how I can fix it?
Many thanks!
By calling new TTree(&chain) you're trying to call a constructor for a TTree and you provide it with a TChain * (the type of chain is TChain, so &chain is a pointer to a TChain).
The constructors for TTree https://root.cern/doc/v618/classTTree.html take either no argument or two const char* (usually string literals or .Data() of a TString or .c_str() of a std::string or the output of ROOT's Form …).
It looks like you try to call the deleted constructor TTree(const TTree&tt)=delete, which doesn't work for several reasons:
you're providing a pointer to a TTree instead of a TTree by reference (not even sure from the top of my head if casting from TChain to TTree would work if the & wasn't there.
the constructor is deleted, so it doesn't actually exist and cannot be called.
However, from the looks of it, you want to use your TChain as a TTree*, which doesn't require any code from your side to begin with because a TChain already is a TTree by inheritance. So, you can use chain. instead of tree->, or (if having a pointer variable is really more desirable for reasons that I don't see in the example) create a TTree* (i.e. create only the pointer to a TTree, instead of creating the actual TTree on the heap and get a pointer to it from the new operator) by doing something like
TTree* tree = &chain;
What I'm trying to do with this program is I have a MySQL result table, and I am iterating through the rows. Each row has 5 columns and I want to store each field into the private data members of a class. Then once I have that class, I want to push_back the class into a vector of the class type. So in my main.cpp I have:
Wine wines("None", 0, 0, 0, "None");
Which is supposed to call this constructor:
Wine::Wine(string inputName, int inputVintage, int inputRating, int inputScore, string inputType) {
name = inputName;
vintage = inputVintage;
rating = inputRating;
score = inputScore;
type = inputType;
}
Back in the main:
vector<Wine> wineVector;
res = mysql_perform_query(conn, (char*)cmd.str().c_str()); //cmd is just a ostringstream holding the query.
loadResultsToVector(wines, wineVector, res);
Where loadResultsToVector is defined as:
void loadResultsToVector(Wine w, vector<Wine> wineVec, MYSQL_RES *results) {
MYSQL_ROW row;
while((row = mysql_fetch_row(results)) != NULL) {
w(row[0], row[1], row[2], row[3], row[4]); //Initializes Wine w, error occurs here.
wineVec.push_back(w);
}
mysql_free_result(results); //Clears result table
}
So when I try to initialize the class with the information from the row, I get this error:
no match to call to '(Wine) (char*&, char*&, char*&, char*&, char*&)'
w(row[0], row[1], row[2], row[3], row[4]); //Initializes Wine w, error occurs here.
^
Any ideas?
This is wrong:
Wine w,
// ...
w(row[0], row[1], row[2], row[3], row[4]);
compiler thinks that you want to call operator() on w object, or that w is a function. I think you want to call a constructor on Wine and initialize w parameter:
w = Wine(row[0], row[1], row[2], row[3], row[4]);
But then why make w a function parameter. This : Wine w(row[0], row[1], row[2], row[3], row[4]); should suffice.
Another problem is that your constructor expects different parameters. Its signature is : string, int, int, int, string, not four char*. So you need to do conversions.
I'm new to C++
class BlenderMesh{
public:
std::vector<double[3]> vertex_vectors;
std::vector<double[3]> normal_vectors;
std::vector<double[2]> uv_vectors;
std::vector<std::string> texture_list;
std::vector<int[6]> face_indices;
std::vector<int[3]> normal_indices;
std::vector<int[3]> uv_indices;
BlenderMesh();
~BlenderMesh();};
Error in code: mesh->vertex_vectors.push_back(vertex);
BlenderMesh::BlenderMesh(){}
BlenderMesh::~BlenderMesh(){}
BlenderMesh* mesh = new BlenderMesh();
PyObject* vertexVectors = PyList_GetItem(blenderObject,2);
unsigned int size_vertex_vectors = PyObject_Size(vertexVectors);
for (unsigned int i = 0; i < size_vertex_vectors; i++){
double vertex[3];
PyObject* pyVertex = PyList_GetItem(vertexVectors,i);
PyObject* vertexX = PyTuple_GetItem(pyVertex,0);
vertex[0] = PyFloat_AsDouble(vertexX);
PyObject* vertexY = PyTuple_GetItem(pyVertex,1);
vertex[1] = PyFloat_AsDouble(vertexY);
PyObject* vertexZ = PyTuple_GetItem(pyVertex,2);
vertex[2] = PyFloat_AsDouble(vertexZ);
mesh->vertex_vectors.push_back(vertex);
}
Console:
/usr/include/c++/4.9.2/ext/new_allocator.h:120:4: error: parenthesized initializer in array new [-fpermissive]
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
/usr/include/c++/4.9.2/ext/new_allocator.h:124:29: error: request for member '~double [3]' in '* __p', which is of non-class type 'double [3]'
destroy(_Up* __p) { __p->~_Up(); }
Well it has been four years but hopefully this helps someone. For some reason you are not able to create a vector object containing a native list of objects (maybe someone could explain why?).
So, for example, when creating a 18x2 vector containing 1'ns for example:
std::vector<double[2]> my_vector(18, {1.0, 1.0});
So instead you should to the following:
std::vector<vector<double>> my_vector(18, vector<double>(2,{1.0, 1.0});
In C++11, a std::array can be used:
std::vector<std::array<double,2>> my_vector(18, std::array<double,2>{1.0, 1.0});
I am ver new to VTK.
This is part of my code:
vtkDataSetReader *rdr = vtkDataSetReader::New();
rdr->SetFileName("proj7b.vtk");
rdr->SetScalarsName("hardyglobal");
rdr->Update();
int dims[3];
vtkRectilinearGrid *rgrid = (vtkRectilinearGrid *) rdr->GetOutput();
rgrid->GetDimensions(dims);
vtkDataArray *dataArray;
dataArray = vtkDoubleArray::New();
dataArray = rgrid->GetPointData()->GetScalars()->GetVoidPointer(0);
for(i=0;i<10;i++)
{
cout<<"here----------"<<endl;
cout<<" "<<dataArray[i]<<" ";
}
I want to read the data into my vtkDataArray. But this code cannot be compile:
proj7b.cxx:525:15: error:
assigning to 'vtkDataArray *' from incompatible type 'void *'
dataArray = rgrid->GetPointData()->GetScalars()->GetVoidPointer(0);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Is there anyone knows how to read data into vtkDataArray?
Vtk provides the method for it, you don't need to use the "low level" method:
rgrid->GetPointData()->GetArray(0)
or
rgrid->GetPointData()->GetArray("arrayname")
It works the same way for FieldData and CellData ( http://www.vtk.org/doc/release/6.2/html/classvtkFieldData.html)
What you get is a vtkArray, not a simple c++ array, so you will have to read it like:
cout<<" "<<dataArray->GetValue(i) <<" ";
There are a lot of examples in the wiki http://www.vtk.org/Wiki/VTK/Examples/Cxx
please see this page: https://cloud.tencent.com/developer/ask/sof/148655
void doSomething(vtkSmartPointer<vtkDataArray> dataArray)
{
vtkIdType numTuples = dataArray->GetNumberOfTuples();
for (vtkIdType tupleIdx = 0; tupleIdx < numTuples; ++tupleIdx)
{
double* tuple = dataArray->GetTuple(tupleIdx);
for (int j = 0; j < /*¿¿¿???*/; ++j)
{
double var = tuple[j];
// Do something with var
// Carefull don't go out of bounds
}
}
}
I have
size_t sums[4] = {0, 0, 0, 0};
and a text box in which I am trying to display the value that I get after I've done some operations on it. The value is in sums[i] , however now I want to display the value in the textbox, I am doing this:
*TextBox4 << "hello" << size_t sums;
frame->Connect(TEXT_BOX4, wxEVT_COMMAND_BUTTON_CLICKED | wxEVT_COMMAND_ENTER, (wxObjectEventFunction) & MyFrame::OnGenerateKey);
TextBox4 = new wxTextCtrl(this, TEXT_BOX4, wxT(""), wxPoint(200, 200), wxSize(200, 20), wxTE_PROCESS_ENTER | wxTE_READONLY | wxTE_LEFT, wxDefaultValidator, wxT("WxTextBox4"));
however it generates the following errors
base.cpp:212:22: error: ambiguous overload for 'operator<<' in '*((MyFrame*)this)->MyFrame::TextBox4 << sum'
base.cpp:212:22: note: candidates are:
C:\wxWidgets-2.8.12\include/wx/textctrl.h:419:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(const wxString&)
C:\wxWidgets-2.8.12\include/wx/textctrl.h:420:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(int)
C:\wxWidgets-2.8.12\include/wx/textctrl.h:421:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(long int)
C:\wxWidgets-2.8.12\include/wx/textctrl.h:422:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(float)
C:\wxWidgets-2.8.12\include/wx/textctrl.h:423:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(double)
C:\wxWidgets-2.8.12\include/wx/textctrl.h:424:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(wxChar)
Please help me.
First of all you can not print a static array like that, you need to iterate over it. Second - you should only print sums without the type:
*TextBox4 << "hello" << sums;
Still this may not be enough to help you - I need more context.