0

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!

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    `Event(3) = 1 or -1` is wrong mainly for two reasons. First it's wrong because you use *assignment* instead of comparison for equality (which is `==`). Secondly (and once you fix the assignment issue) because the expression is really equal to `(Event(3) == 1) or -1` it will *always* be `true` (since all non-zero values are true, and `-1` is non-zero). You need to compare explicitly to both values (i.e. `Event(3) == 1 or Event(3) == -1`). – Some programmer dude May 17 '22 at 05:47
  • 1
    Also to check if a value is in a range `180 < mass < 220` won't work as expected, as it's really the same as `(180 < mass) < 220` which means you check if the boolean result of `180 < mass` is less than `220` (which it will always be). Use e.g. `180 < mass and mass < 220` instead. – Some programmer dude May 17 '22 at 05:50
  • 1
    All in all, you should probably invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to actually learn these things. – Some programmer dude May 17 '22 at 05:51
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community May 17 '22 at 06:47
  • 3
    Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/)). By SE policy, any vandalism will be reverted. – tripleee May 19 '22 at 05:10
  • It is also unfair to the people who took time formulating answers – Sebastian May 19 '22 at 05:47

1 Answers1

1

Particle and Event are not variables, they are types.
You are supposed to access the Particles in an Event using its getNParticles and getParticle functions.

Example of a loop over all of them:

Event event;
inputFile >> event;
for (int i = 0; i < event.getNParticles(); i++)
{
    const Particle& p = event.getParticle(i);
    // Use p.getpT() et al in your formula (it's not an equation).
}

It is also a good idea to put the mass calculation in a function so you can write for instance,

if (mass(particle1) == mass(particle2)) 
    ...

Also, 180 < mass < 220 does not mean what you think it means – you need to write 180 < mass && mass < 220.
You should not return count; inside the loop; that will exit main immediately.
And it's unclear what you're trying to accomplish with Event(3) = 1 or -1, but I would guess it's an attempt at "the event has both same-sign pairs of invariant masses".
You're going to need to think harder about that.
And do get yourself a good book. You can't learn C++ by guessing.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Yes, it is an attempt at the "event has both same-sign pairs of invariant masses". Can you give me a better idea of what I might need to do for that? Thank you so much for your help! –  May 17 '22 at 07:26
  • I'm afraid I don't have any idea about what that phrase means. – molbdnilo May 17 '22 at 08:00