(I know this looks long, but I have bolded and annotated everything relevant. I've tried to be detailed as possible without making you read too much.)
Beginner here. I'm at my wits end trying to figure out why I'm getting junk numbers from a certain part of my code. I'm sure it has something to do with a subtlety of pointers that I do not understand.
I have a system of three classes: hit, track, and crt_event. "hits" are just an integer and a double and a few member functions. crt_event has a vector of track pointers and four vectors of hit pointers (TXhits, TYhits, BXhits, BYhits). Each track consists of four hit pointers (TX TY BX and BY - obviously these correspond to a single hit from each of the hit pointer vectors).
long story short, the point of the program is to loop through events in a large data file, sort them into different vectors, then construct tracks combinatorially from each hit vector and keep the good ones. Here are the relevant parts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
struct hit
{
public:
hit(){}
hit(Int_t fiber_i, Double_t time_i) : fiber(fiber_i), time(time_i) {}
Int_t fiber;
Double_t time;
Double_t Get_Position() { if (!pos) { /* Computes pos based on isTop and isX */ } return pos; }
Bool_t isTop() { /* Some criterea */ }
Bool_t isX() { /* Some criterea */ }
Bool_t isGood() { /* Some criterea */ }
void Show(ostream &os) { /* Outputs the hits data to some ostream */ }
private:
Double_t pos;
};
struct track {
track(){}
track(hit *TX_i, hit *TY_i, hit *BX_i, hit *BY_i) : TX(TX_i), TY(TY_i), BX(BX_i), BY(BY_i) {}
hit *TX; hit *TY; hit *BX; hit *BY;
/* THIS IS WHERE THE PROBLEM IS. TX->Get_Position() is giving me junk numbers. (Also BX) */
Double_t TX_rot() { if (!txrot) {txrot = (TX->Get_Position())*cosrot - (TY->Get_Position())*sinrot - horizontal_offset;} return txrot;}
Double_t BX_rot() { if (!bxrot) {bxrot = BX->Get_Position()*cosrot - BY->Get_Position()*sinrot - horizontal_offset;} return bxrot; }
/* NOTE: cosrot and sinrot are global variables defined outside of these structs, but I've taken them out and it still breaks */
Bool_t isGood() { /* Some criterea */ }
void Show(ostream &os) { /*Outputs the hits data to some ostream */ }
private:
Double_t txrot; Double_t bxrot;
};
struct crt_event {
crt_event(){}
crt_event(Int_t number_i) : number(number_i) {}
~crt_event(){ /* Loops through and deletes contents of hit vectors and track vector */ }
Int_t number;
Int_t total_number_of_hits;
vector<hit*> TXhits;
vector<hit*> TYhits;
vector<hit*> BXhits;
vector<hit*> BYhits;
vector<track*> tracks;
void AddHit(hit* a) { /* Sorts and adds a hit to the proper vector */ }
virtual Bool_t isGood() { /* Some criterea */ }
void Show(ostream &os) { /*Outputs the hits data to some ostream */ }
};
void manatee()
{
Int_t fiber;
Double_t time;
ifstream EventData("CRTevents.dat");
Int_t current_event_number = 1;
Int_t event_number = 0;
cout << "Finding tracks.\n";
crt_event current_event(current_event_number);
while(!EventData.eof())
{
/* Loops through and collects hits from data file */
while (!EventData.eof() && (event_number == current_event_number || event_number == 0)) {
if (event_number != 0) current_event.AddHit(new hit(fiber,time));
EventData >> event_number >> fiber >> time;
}
/* Combinatorially makes tracks from hit vectors, adds only the good ones */
if (current_event.isGood() ){
for (Int_t i_tx = 0; i_tx < current_event.TXhits.size(); i_tx++) { current_TX = current_event.TXhits[i_tx];
for (Int_t i_ty = 0; i_ty < current_event.TYhits.size(); i_ty++) { current_TY = current_event.TYhits[i_ty];
for (Int_t i_bx = 0; i_bx < current_event.BXhits.size(); i_bx++) { current_BX = current_event.BXhits[i_bx];
for (Int_t i_by = 0; i_by < current_event.BYhits.size(); i_by++) { current_BY = current_event.BYhits[i_by];
current_track = new track(current_TX,current_TY,current_BX,current_BY);
current_track->Show(cout);
if (current_track->isGood() ) current_event.tracks.push_back(current_track); else delete current_track;
}}}}
}
current_event.Show(cout);
current_event_number++;
crt_event current_event(current_event_number);
}
cout << "\n";
}
|
The problem is I'm getting junk numbers from TX_rot() and BX_rot() in the position marked above. I can see this when I call track::Show() from inside crt_event::Show(). However, track::Show() also accesses public members of each hit (the fibers and times) and does so just fine - it only messes up when calling TX_rot(), which seems to be having a problem with Get_Position().
Can anybody help me out here? Thanks a lot in advance...
P.S. As a side note... is what I'm doing with the Get_Whatever() methods generally a good practice? Not computing the value until it's needed?