Error while XOR ing C++ - c++

I have problem, when i compile this code
#include <iostream>
using namespace std;
int main(){
const unsigned long box[256]= { 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7,
0xB8E1AFED, 0x6A267E96, 0xBA7C9045, 0xF12C7F99,
0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16,
0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E,
0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE,
0x7B54A41D, 0xC25A59B5, 0x9C30D539, 0x2AF26013,
0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF,
0x8E79DCB0, 0x603A180E, 0x6C9E0E8B, 0xB01E8A3E,
0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60,
0xE65525F3, 0xAA55AB94, 0x57489862, 0x63E81440,
0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE,
0xA15486AF, 0x7C72E993, 0xB3EE1411, 0x636FBC2A,
0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E,
0xAFD6BA33, 0x6C24CF5C, 0x7A325381, 0x28958677,
0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193,
0x61D809CC, 0xFB21A991, 0x487CAC60, 0x5DEC8032,
0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88,
0x23893E81, 0xD396ACC5, 0x0F6D6FF3, 0x83F44239,
0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E,
0x21C66842, 0xF6E96C9A, 0x670C9C61, 0xABD388F0,
0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3,
0x6EEF0B6C, 0x137A3BE4, 0xBA3BF050, 0x7EFB2A98,
0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88,
0x8CEE8619, 0x456F9FB4, 0x7D84A5C3, 0x3B8B5EBE,
0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6,
0x4ED3AA62, 0x363F7706, 0x1BFEDF72, 0x429B023D,
0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B,
0x075372C9, 0x80991B7B, 0x25D479D8, 0xF6E8DEF7,
0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA,
0xC1A94FB6, 0x409F60C4, 0x5E5C9EC2, 0x196A2463,
0x68FB6FAF, 0x3E6C53B5, 0x1339B2EB, 0x3B52EC6F,
0x6DFC511F, 0x9B30952C, 0xCC814544, 0xAF5EBD09,
0xBEE3D004, 0xDE334AFD, 0x660F2807, 0x192E4BB3,
0xC0CBA857, 0x45C8740F, 0xD20B5F39, 0xB9D3FBDB,
0x5579C0BD, 0x1A60320A, 0xD6A100C6, 0x402C7279,
0x679F25FE, 0xFB1FA3CC, 0x8EA5E9F8, 0xDB3222F8,
0x3C7516DF, 0xFD616B15, 0x2F501EC8, 0xAD0552AB,
0x323DB5FA, 0xFD238760, 0x53317B48, 0x3E00DF82,
0x9E5C57BB, 0xCA6F8CA0, 0x1A87562E, 0xDF1769DB,
0xD542A8F6, 0x287EFFC3, 0xAC6732C6, 0x8C4F5573,
0x695B27B0, 0xBBCA58C8, 0xE1FFA35D, 0xB8F011A0,
0x10FA3D98, 0xFD2183B8, 0x4AFCB56C, 0x2DD1D35B,
0x9A53E479, 0xB6F84565, 0xD28E49BC, 0x4BFB9790,
0xE1DDF2DA, 0xA4CB7E33, 0x62FB1341, 0xCEE4C6E8,
0xEF20CADA, 0x36774C01, 0xD07E9EFE, 0x2BF11FB4,
0x95DBDA4D, 0xAE909198, 0xEAAD8E71, 0x6B93D5A0,
0xD08ED1D0, 0xAFC725E0, 0x8E3C5B2F, 0x8E7594B7,
0x8FF6E2FB, 0xF2122B64, 0x8888B812, 0x900DF01C,
0x4FAD5EA0, 0x688FC31C, 0xD1CFF191, 0xB3A8C1AD,
0x2F2F2218, 0xBE0E1777, 0xEA752DFE, 0x8B021FA1,
0xE5A0CC0F, 0xB56F74E8, 0x18ACF3D6, 0xCE89E299,
0xB4A84FE0, 0xFD13E0B7, 0x7CC43B81, 0xD2ADA8D9,
0x165FA266, 0x80957705, 0x93CC7314, 0x211A1477,
0xE6AD2065, 0x77B5FA86, 0xC75442F5, 0xFB9D35CF,
0xEBCDAF0C, 0x7B3E89A0, 0xD6411BD3, 0xAE1E7E49,
0x00250E2D, 0x2071B35E, 0x226800BB, 0x57B8E0AF,
0x2464369B, 0xF009B91E, 0x5563911D, 0x59DFA6AA,
0x78C14389, 0xD95A537F, 0x207D5BA2, 0x02E5B9C5,
0x83260376, 0x6295CFA9, 0x11C81968, 0x4E734A41,
0xB3472DCA, 0x7B14A94A, 0x1B510052, 0x9A532915,
0xD60F573F, 0xBC9BC6E4, 0x2B60A476, 0x81E67400,
0x08BA6FB5, 0x571BE91F, 0xF296EC6B, 0x2A0DD915,
0xB6636521, 0xE7B9F9B6, 0xFF34052E, 0xC5855664,
0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A };
for (int x = 0;x<=255;x++){
cout<< sbox[x]^ sbox[x]<<endl;
}
return 0;
}
I get an error which says:
[Error] invalid operands of types ‘const long unsigned int’ and
‘<unresolved overloaded function type>’ to binary ‘operator<<’".
What is wrong with my code?

Order of operations: << is higher than ^.
Put parentheses around sbox[x]^ sbox[x]:
cout << (sbox[x]^sbox[x]) << endl;

Related

How do you transpose a tensor in LibTorch?

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.

Error in Using Root/C++: No matching constructor for initialization of 'TTree'

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;

How to rotate a bitvector in cvc4 using c++ API

I try to rotate a bitvector in cvc4 using the C++ API, but the API is a little bit confusing when it comes to operator expressions.
Using the following code (extract):
#include <iostream>
#include <cvc4/cvc4.h>
using namespace std;
using namespace CVC4;
int main() {
ExprManager em;
SmtEngine smt(&em);
smt.setLogic("QF_BV");
Type bitvector32 = em.mkBitVectorType(32);
Integer i = Integer(1, 10);
BitVector bv = BitVector(32, i);
Expr expr = em->mkConst(bv);
BitVectorRotateLeft bv_rl = BitVectorRotateLeft(1);
Expr e_bv_rl = em->mkConst(bv_rl);
Expr e_op_rl = em->operatorOf(kind::BITVECTOR_ROTATE_LEFT_OP);
Expr e_op_e = em->mkExpr(e_op_rl, e_bv_rl);
Expr e = em->mkExpr(Kind::BITVECTOR_ROTATE_LEFT, e_op_e, expr);
return 0;
}
Executing this yields:
terminate called after throwing an instance of 'CVC4::IllegalArgumentException'
what(): Illegal argument detected
CVC4::Expr CVC4::ExprManager::mkExpr(CVC4::Expr, CVC4::Expr)
`opExpr' is a bad argument; expected (opExpr.getKind() == kind::BUILTIN || kind::metaKindOf(kind) == kind::metakind::PARAMETERIZED) to hold
This Expr constructor is for parameterized kinds only
Aborted
Does anybody know how to deal with the operator construct of cvc4?
See below for the correct construction of a rotate left expression. In general, whenever you have an expression operator that is itself an expression, you apply it by simply calling mkExpr and passing the operator expression as the first argument.
int main() {
ExprManager em;
SmtEngine smt(&em);
smt.setLogic("QF_BV");
Type bitvector32 = em.mkBitVectorType(32);
BitVector bv = BitVector(32, 1U);
Expr expr = em.mkConst(bv);
BitVectorRotateLeft bv_rl = BitVectorRotateLeft(1);
Expr e_bv_rl = em.mkConst(bv_rl);
Expr e = em.mkExpr(e_bv_rl, expr);
cout << e;
return 0;
}

Libtorrent set_settings error

I'm trying to set the session settings but I'm getting an error I don't quite understand, I am able to get the session settings OK and according to the Libtorrent docs I simply pass the session_settings structure to set_settings once I have changed any values.
using namespace libtorrent;
session* Session;
session_status* Session_Status;
session_settings* Session_Settings;
bool Start_Client_Sess()
{
Session = new session;
Session_Status = new session_status;
Session_Settings = new session_settings;
Session->settings( );
std::cout << "upload_rate_limit " << Session_Settings->upload_rate_limit << " \n";
std::cout << "dht_announce_interval " << Session_Settings->dht_announce_interval << " \n";
Session_Settings->upload_rate_limit = 200;
Session_Settings->dht_announce_interval = 1800;
Session->set_settings( Session_Settings ); // error
}
Error:
1>Source\Client_F.cpp(66): error C2664: 'void libtorrent::session::set_settings(const libtorrent::session_settings &)' : cannot convert argument 1 from 'libtorrent::session_settings *' to 'const libtorrent::session_settings &'
1> Reason: cannot convert from 'libtorrent::session_settings *' to 'const libtorrent::session_settings'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
As the error message says, you don't have to pass a pointer to this function.
Session->set_settings( *Session_Settings );

c++ pushing dereferenced objects to vector

I'm making a function to take in a reference to a path on the filesystem and recursively add file names to a vector. But first I have to be able to add paths to a vector.
what is wrong with this method?
namespace filesys = boost::filesystem;
Method:
void recursive_file_list(filesys::path & root_path, vector<filesys::path> & paths) {
paths->push_back(*root_path); // line 14
// TODO: add more logic to actually recurse through file system
// for now just add root_path (even though root_path is path of directory, not file)
}
And I call it like so:
int main(int argc, char* argv[]) {
// Use the command line arguments
filesys::path abs_path; // line 23
if ( argc > 1 )
// Make the system complete this path to absolute path
abs_path = filesys::system_complete( filesys::path( argv[1] ) );
else {
// If no arguments given
cout << "usage: list_files [path]" << endl;
exit(1);
}
// Is this a valid path?
if (!filesys::exists( abs_path )) {
cout << "The path you have specified does not exist." << endl;
exit(2);
}
// If this is a directory
vector<filesys::path> filepaths();
if (filesys::is_directory( abs_path )) {
cout << "You have specified a directory." << endl;
recursive_file_list(&abs_path, &filepaths);
}
else {
cout << "You have specified a file." << endl;
}
}
Error:
list_files.cpp: In function 'void recursive_file_list(boost::filesystem3::path&, std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >&)':
list_files.cpp:14: error: base operand of '->' has non-pointer type 'std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >'
list_files.cpp:14: error: no match for 'operator*' in '*root_path'
list_files.cpp: In function 'int main(int, char**)':
list_files.cpp:43: error: invalid initialization of non-const reference of type 'boost::filesystem3::path&' from a temporary of type 'boost::filesystem3::path*'
list_files.cpp:13: error: in passing argument 1 of 'void recursive_file_list(boost::filesystem3::path&, std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >&)'
I don't understand - I'm passing it by reference and then dereferencing it to add it...not sure what I'm doing wrong.
You can't dereference references! The * and -> operators do not apply to references; you would push the path itself to the vector itself:
paths.push_back(root_path); // line 14
and furthermore, you don't pass addresses to reference arguments, just the arguments themselves, so you would call the function like this:
recursive_file_list(abs_path, filepaths);
References have pointer-like non-copy semantics all by themselves; you do not need to help them along by taking addresses or trying to dereference them.