I am very new to C++ and I don't know the syntax or many of the functions. I am working on an assignment and I am very lost. I am trying to access a variable from another page to use in an equation and I'm not sure how to do so. Here is the code I have written (I know it's very wrong):
#include <fstream>
#include <iostream>
#include <string>
#include "Event.h"
int main()
{
const std::string filename = R"(..\data.txt)";
// Open the data file
std::ifstream inputFile(filename);
// Throw an exception if the file is not where we expected
if (!inputFile)
{
throw std::runtime_error("File " + filename + " not found!");
}
// Find the number of events in the file
int nEvents;
inputFile >> nEvents;
// Loop over each event
int count = 0;
for (int i = 0; i < nEvents; ++i)
{
Event event;
inputFile >> event;
if (Event(3) = 1 or -1)
{
int mass = sqrt(2 * Particle::getpT[1] * Particle::getpT[2] * (cosh(Particle::getPhi[1] - Particle::getPhi[2]) - cos(Particle::getEta[1] - Particle::getEta[2])));
if (180 < mass < 220)
{
count = count + 1;
};
}
return count;
// Here is where you would put the code to look at the particles in each event
// You want to see if the event has both same-sign pairs of invariant masses
// falling into the range 180-220
// Count how many events do this
}
int nEvents = count;
// When you are done, you should print out the answer:
std::cout << "Total events in range: " << nEvents << '\n';
std::cout << "Finished running!\n";
}
And the variables I am trying to get are here:
#pragma once
#include <iostream>
// A class that contains basic information about a particle
class Particle
{
public:
// Default constructor needed for streaming
Particle() : pt(0), phi(0), eta(0), charge(0) {}
Particle(double ipT, double iphi, double ieta, int icharge) : pt(ipT), phi(iphi), eta(ieta), charge(icharge) {}
// Getters
double getpT() const { return pt; }
double getPhi() const { return phi; }
double getEta() const { return eta; }
int getCharge() const { return charge; }
// Setters (needed for streaming)
void setpT(double newPt) { pt = newPt; }
void setPhi(double newPhi) { phi = newPhi; }
void setEta(double newEta) { eta = newEta; }
void setCharge(int newCharge) { charge = newCharge; }
private:
double pt, phi, eta;
int charge;
};
// Streaming operators for I/O
std::ostream& operator<<(std::ostream& stream, const Particle& part);
std::istream& operator>>(std::istream& stream, Particle& part);
There's also this file attached:
#pragma once
#include <iostream>
#include <vector>
#include "Particle.h"
// An event contains a number of Particles
// In our case, there will always be four particles
class Event
{
public:
// Gets a particle by a given index - zero indexed
// So 0, 1, 2, and 3 are the possible values
const Particle& getParticle(int index) const { return particles.at(index); }
// The total number of particles - always 4 in our case
int getNParticles() const { return static_cast<int>(particles.size()); }
// Adds a new particle to the collection
void addParticle(Particle part) { particles.push_back(part); }
private:
std::vector<Particle> particles;
};
// Streaming operators for I/O
std::ostream& operator<<(std::ostream& stream, const Event& event);
std::istream& operator>>(std::istream& stream, Event& event);
If you can make any sense of this and what I need to know, I would really appreciate it!