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.
Related
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.
I can't seem to figure out how to transpose a tensor in LibTorch, the (C++ version of PyTorch).
torch::Tensor one_T = torch::rand({6, 6});
int main() {
std::cout << one_T.transpose << "\n";
}
My error...
/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note: mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(at::Dimname, at::Dimname) const’
53 | std::cout << one_T.transpose << "\n";
| ^~~~~~~~~
/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note: mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(int64_t, int64_t) const’ {aka ‘at::Tensor (at::Tensor::*)(long int, long int) const’}
In file included from /home/iii/tor/m_gym/libtorch/include/c10/util/Logging.h:28,
from /home/iii/tor/m_gym/libtorch/include/c10/core/TensorImpl.h:17,
from /home/iii/tor/m_gym/libtorch/include/ATen/core/TensorBody.h:20,
from /home/iii/tor/m_gym/libtorch/include/ATen/core/Tensor.h:3,
from /home/iii/tor/m_gym/libtorch/include/ATen/Tensor.h:3,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/function_hook.h:3,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/cpp_hook.h:2,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/variable.h:6,
one_T.transpose(0, 1)
The 0, 1 is the type of default transpose used in PyTorch and Numpy without being specified.
So I don't understand how to get this thing working.
I already read the Class reference but the only two references to get_date() return void. How can I get the date from a calendar widget?.
I now that I'm calling the method wrong and this cause the errors.
This is my Notebook.hpp
#pragma once
#include <gtkmm.h>
class NotebookMain : public Gtk::Window{
public:
NotebookMain();
virtual ~NotebookMain();
protected:
// Signal handlers
void on_notebook_switch_page(Gtk::Widget *page, guint page_num);
void on_button_add_appointment();
// Member widgets
Gtk::Notebook notebookMain;
Gtk::Label label1, labelTest;
Gtk::Box boxDatebook, boxCalendar
Gtk::Calendar calendar;
Gtk::Button buttonAddAppointment;
};
This is my Notebook.cpp
#include <iostream>
#include "../include/NotebookMain.hpp"
NotebookMain::NotebookMain() : label1("Hello from inside Tab 2!"),
labelTest("Hello from inside Appointments"){
this->set_title("Test");
this->set_default_size(640, 480);
// Create Calendar Box
boxCalendar.set_orientation(Gtk::ORIENTATION_VERTICAL);
boxCalendar.pack_start(calendar);
buttonAddAppointment.set_label("Add Appointment");
boxCalendar.pack_start(buttonAddAppointment);
// Create Appointments Box
boxAppointments.set_orientation(Gtk::ORIENTATION_VERTICAL);
boxAppointments.pack_start(labelTest);
// Create Datebok Box
boxDatebook.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
boxDatebook.pack_start(boxCalendar);
boxDatebook.pack_end(boxAppointments);
// Set Notebook
notebookMain.set_border_width(0);
// Add Notebook tabs
notebookMain.append_page(boxDatebook, "Datebook");
notebookMain.append_page(label1, "Tab 2");
// Switch tabs
notebookMain.signal_switch_page().connect(sigc::mem_fun(*this, &NotebookMain::on_notebook_switch_page));
// Get Date
buttonAddAppointment.signal_clicked().connect(sigc::mem_fun(*this, &NotebookMain::on_button_add_appointment));
// Add Main Window (Notebook)
this->add(notebookMain);
this->show_all_children();
}
NotebookMain::~NotebookMain(){}
void NotebookMain::on_notebook_switch_page(Gtk::Widget * /* page */, guint page_num){
std::cout << "Switched to tab with index : " << page_num << std::endl;
}
void NotebookMain::on_button_add_appointment(){
std::cout << "Button clicked\n";
std::cout << calendar.get_date() << "\n";
}
And the errors
src/NotebookMain.cpp: In member function ‘void NotebookMain::on_button_add_appointment()’:
src/NotebookMain.cpp:51:23: error: no matching function for call to ‘Gtk::Calendar::get_date()’
51 | calendar.get_date();
| ^
In file included from /usr/include/gtkmm-3.0/gtkmm.h:196,
from src/../include/NotebookMain.hpp:3,
from src/NotebookMain.cpp:3:
/usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note: candidate: ‘void Gtk::Calendar::get_date(guint&, guint&, guint&) const’
299 | void get_date(guint& year, guint& month, guint& day) const;
| ^~~~~~~~
/usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note: candidate expects 3 arguments, 0 provided
/usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note: candidate: ‘void Gtk::Calendar::get_date(Glib::Date&) const’
305 | void get_date(Glib::Date& date) const;
| ^~~~~~~~
/usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note: candidate expects 1 argument, 0 provided
make: *** [makefile:9: NotebookMain.o] Error 1
This solved the problem. I need to pass the arguments, and then those variables will stored the value from the calendar.
unsigned int year, month, day;
calendar.get_date(year, month, day);
Pro tip: Read the documentation right!.
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;
I have a text file that's got data written in JSON format.
The data looks something like this --
[
...
{
"volume": 93,
"id": "part-30",
"value": 19
},
{
"volume": 83,
"id": "part-31",
"value": 19
}
...
]
After referring to this and this
I've gotten to a point where I can read the "name" field of the following data structure.
So all other things aside, my code to read this object looks like this --
// read from parts list file to JSON object.
const char* file_name2( "parts_list.txt" );
ifstream is2( file_name2 );
json_spirit::Value value2;
read( is2, value2 );
// const Object& addr_array = value.get_obj();
vector<Value> jsonObj2 = value2.get_array();
vector<Value>::iterator it;
vector<RobotParts> final;
for(it = jsonObj2.begin(); it!=jsonObj2.end(); it++)
{
auto valObj = it->get_obj();
RobotParts rpObj = RobotParts();
for(auto vo : valObj)
{
if(vo.name_=="volume"){
string s = vo.value_;
}
}
final.push_back(rpObj);
}
cout << final.size() << endl;
return 0;
But this line here --> vo.value_; seems to be creating a lot of problems.
I'm unable to figure out what's the data type of this object.
So far I've tried :
Reading into an integer. I thought since the volume has an integer value
int i = vo.get_value< int >();
Should work. But instead, it says
error: no member named 'get_value' in
'json_spirit::Pair_impl<json_spirit::Config_vector<std::__1::basic_string<char> > >'
Reading into a string so I could print it out.
string s = vo.value_;
This throws the following error:
error: no viable conversion from 'Value_type' (aka
'Value_impl<json_spirit::Config_vector<std::__1::basic_string<char> > >') to 'string' (aka 'basic_string<char, char_traits<char>,
allocator<char> >')
I can, however, print out vo.name_ by using cout << vo.name_ << endl within the loop.
This outputs :
...
volume
id
value
volume
id
value
...
so on.
I know this is due to data type incompatibility but I've spent two-three hours now unable to figure out how to access this value.
How do I access these values corresponding to volume, id and value?