/* University College London Dept of P&A course in C++ 3C59 | all rights reserved 2000 | | Module: Consolidation excercise: | | Title: ComptonAnalysis | | Class: Datum | | Description: | This class is used to create objects which can hold the information pertaining to a | single datum as measured in the 3rd year lab compton scattering experiment. | A datum consists of: | - The angle at which a photopeak energy was measured | - The measured photopeak energy | - The uncertainty on the energy measurement | | Author: P.Clarke */ #ifndef _datum_h #define _datum_h 1 #include #include class Datum { private: // These are the member variables which each object of this class will have float m_angle ; float m_energy ; float m_errEnergy ; public: // -------------------------- // This method is a used to initialise an object after it has been created. // (this is provided because at this stage we have not covered constructors) void initialise( float inputAngle, float inputEnergy, float inputError ) { // Students should be clear that here we set: // member variable = value input as argument to method m_angle = inputAngle ; m_energy = inputEnergy ; m_errEnergy = inputError ; // Note the useful convention: prefix all member variables with m_.... // This means that in all your methods it is crystal clear which variables // are member variables as opposed to temporary local variables return ; } // End of method // --------------------------- // This method asks the Datum object to calculate its contribution to the chi-squared // What this means is that it has to form the quantity // (Epredicted-Emeasured)^2 / error^2 // corresponding to this Datum object // // In this calculation it already knows everything it needs to know except the mass // of the elctron which we are treating as an unknown. // Therefore when we ask a Datum object to do this calculation we have to pass it // the electron mass we want it to use. float chiSquared( float mass ) { // First lets calculate the predicted energy from the formula float eIncident = 662.0 ; float invE = 0.0 ; float ePredicted = 0.0; float angleInRadians = m_angle * 3.14159 / 180.0 ; invE = (1.0/eIncident) + (1.0/mass) - ( cos( angleInRadians ) / mass ) ; ePredicted = 1.0/invE ; // Now calculate the chi-squared contribution float chisq = 0 ; chisq = (m_energy - ePredicted)*(m_energy - ePredicted)/(m_errEnergy*m_errEnergy); // Finally return the chi-squared contribution to the caller of this method return chisq ; } // End of method // ------------------------- // Method to dump contents for verification void dump() { std::cout << " Datum contents : " << " Angle = " << m_angle << " / Energy = " << m_energy << " +- " << m_errEnergy << std::endl; } // -------------------------- // Please note how clear I make the boundary between each method. I use comment line // extensively to do this. // -------------------------- } ; #endif