00001 #include "MapsTrackManager.hh"
00002 #include "MapsTrack.hh"
00003 #include "MapsSensor.hh"
00004 #include "MapsException.hh"
00005 #include <cmath>
00006 #include <iostream>
00007 #include <vector>
00008 #include <algorithm>
00009 #include <ostream>
00010 #include <iterator>
00011 #include <functional>
00012 #include "TRandom2.h"
00013 #include "TFile.h"
00014 #include "TH1F.h"
00015 #include "TH2F.h"
00016
00017 struct testArea :
00018 public std::binary_function<MapsSensor*, std::pair<double, double>, void> {
00019 void operator()(MapsSensor* s, std::pair<double, double> p) const {
00020 std::cout << *s << "\n";
00021 std::cout << "\tInput:\t" << p << "\n";
00022 MapsTrack::coord testCoord;
00023 if (s->isDeadArea(p))
00024 std::cout << "\tDead area? Yes\n";
00025 else {
00026 s->convertPhysicalToHit(p, testCoord);
00027 std::cout << "\tMaps to:\t" << testCoord << "\n";
00028 }
00029 }
00030
00031 };
00032
00033 int main() {
00034 std::cout << "** Testing Tracking...\n";
00035 double max(pow(2, 8*sizeof(double)) +1);
00036 double pi = 3.14159265358979323846;
00037
00038 MapsTrack::coord c1(132, 137);
00039 MapsTrack::coord c2(30, 30);
00040 MapsTrack::coord c3(132, 137);
00041 MapsTrack::coord c4(30, 30);
00042
00043 MapsSensor* s1 = new MapsSensor(1, 0, pi);
00044 MapsSensor* s2 = new MapsSensor(5, 10);
00045 MapsSensor* s3 = new MapsSensor(7, 20, pi);
00046 MapsSensor* s4 = new MapsSensor(9, 30);
00047
00048 std::vector<MapsSensor*> sensors;
00049 sensors.push_back(s1);
00050 sensors.push_back(s2);
00051 sensors.push_back(s3);
00052 sensors.push_back(s4);
00053
00054 std::cout << "** Sensors initialised. e.g.\n\t" << *s1 << "\n";
00055 std::map<MapsSensor*, MapsTrack::coord> theHits;
00056 theHits[s1] = c1;
00057 theHits[s2] = c2;
00058 theHits[s3] = c3;
00059 theHits[s4] = c4;
00060
00061
00062
00063
00064
00065 std::cout << "** Testing conversions...\n";
00066
00067 MapsTrack::physXY p0(-2.0, 2.0);
00068 MapsTrack::physXY p1(-3.5, -2.8);
00069 MapsTrack::physXY p2(2.4, 1.0);
00070 std::for_each(sensors.begin(), sensors.end(), std::bind2nd(testArea(), p2));
00071
00072 MapsTrack theTrack(35, theHits, s3, 100);
00073
00074 std::cout << "** Track initialised:\n\t" << theTrack << "\n";
00075 std::cout << "Track's coordinates:\n";
00076 theTrack.printCoords(std::cout);
00077
00078 if (theTrack.missingSensor(sensors) != 0)
00079 std::cout << "Missing sensor in track is "
00080 << *(theTrack.missingSensor(sensors)) << "\n";
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 std::cout << "** Testing residuals...\n";
00098 std::pair<double, double> resid3(0, 0);
00099
00100 resid3 = theTrack.simpleResidual(s3);
00101
00102 MapsSensor* s9 = new MapsSensor(9, 100);
00103 try {
00104 std::pair<double, double> exception = theTrack.simpleResidual(s9);
00105 } catch (MapsException& me) {
00106 std::cout << me.what();
00107 }
00108 std::cout << "Residual for " << *s3 << ":\t" << resid3 << "\n";
00109
00110 std::cout << "Getting p0, p1: X = \t" << theTrack.fitParameters(0) << "\n";
00111 std::cout << "Getting p0, p1: Y = \t" << theTrack.fitParameters(1) << "\n";
00112 std::cout << "Track's chiSq: X = \t" << theTrack.chiSq(0) << ", \tY = \t"
00113 << theTrack.chiSq(1) << "\n";
00114
00115 MapsTrackManager mtm(sensors);
00116 TFile* file = new TFile("trackTest.root", "recreate");
00117
00118 std::vector<MapsTrack*> myTracks;
00119 MapsSensor::physXY alignS4(0.3, -0.5);
00120
00121 TRandom2 rand;
00122 TH2F sensorCorr("hSensorCorr", "hSensorCorr", 168, 0, 168, 1000, -5.0, 5.0);
00123 for (unsigned l(0); l < 10000; l++) {
00124 std::map<MapsSensor*, MapsTrack::coord> theseHits;
00125
00126 double x, y, z;
00127 rand.Sphere(x, y, z, 5);
00128 MapsTrack::physXY location(x, y);
00129
00130
00131
00132
00133
00134
00135
00136
00137 MapsTrack::coord c1;
00138 MapsTrack::coord c2;
00139 MapsTrack::coord c3;
00140 MapsTrack::coord c4;
00141
00142
00143 if (!s1->isDeadArea(location)) {
00144 s1->convertPhysicalToHit(location, c1);
00145
00146 c1.first += rand.Uniform() * 2;
00147 c1.second += rand.Uniform() * 2;
00148 theseHits[s1] = c1;
00149
00150 }
00151 if (!s2->isDeadArea(location)) {
00152 s2->convertPhysicalToHit(location, c2);
00153 c2.first += rand.Uniform() * 2;
00154 c2.second += rand.Uniform() * 2;
00155 theseHits[s2] = c2;
00156 sensorCorr.Fill(c2.first, location.first);
00157 }
00158 if (!s3->isDeadArea(location)) {
00159 s3->convertPhysicalToHit(location, c3);
00160 c3.first += rand.Uniform() * 2;
00161 c3.second += rand.Uniform() * 2;
00162 theseHits[s3] = c3;
00163 }
00164 if (!s4->isDeadArea(location)) {
00165 s4->convertPhysicalToHit(location, c4);
00166 }
00167
00168 unsigned bx = rand.Uniform() * 8000;
00169
00170 if (rand.Uniform() > 0.2 && !s4->isDeadArea(location))
00171 theseHits[s4] = c4;
00172
00173 if (theseHits.size() > 2) {
00174 MapsTrack* thisTrack = new MapsTrack(bx, theseHits, s4, 100);
00175 myTracks.push_back(thisTrack);
00176 mtm.addTrack(thisTrack);
00177 if(c2.first < 1)
00178 diagnose(std::cout, *thisTrack);
00179
00180 std::pair<double, double> fourthHitResid;
00181 MapsTrack threeHitVariant;
00182 thisTrack->make3HitTrack(threeHitVariant);
00183
00184
00185
00186
00187 unsigned status = thisTrack->getFourthHitResidual(threeHitVariant,
00188 fourthHitResid);
00189 if (status == 0) {
00190 s4->registerTrackConfirm(100, c4, bx, fourthHitResid);
00191
00192 } else {
00193 std::cout << __PRETTY_FUNCTION__ << __PRETTY_LINE__ << ": disabled!\n
00194 //s4->registerTrackMiss(100, bx);
00195 }
00196 }
00197
00198 }
00199
00200 sensorCorr.Write();
00201 TH1F* hChiSqX = new TH1F("hChiSqX", "Chi squared in X", 100, 0, 100);
00202 TH1F* hChiSqY = new TH1F("hChiSqY", "Chi squared in Y", 100, 0, 100);
00203 TH2F residS2, ineff2;
00204 for (std::vector<MapsTrack*>::iterator it = myTracks.begin(); it
00205 != myTracks.end(); it++) {
00206 MapsTrack* t = *it;
00207 hChiSqX->Fill(t->chiSq(0));
00208 hChiSqY->Fill(t->chiSq(1));
00209 t->tellSensorsOfResiduals();
00210 }
00211 s2->getResidualXYPlot(residS2);
00212 s2->getInefficiencyXYPlot(ineff2, true);
00213 hChiSqX->Write();
00214 hChiSqY->Write();
00215 residS2.Write();
00216 ineff2.Write();
00217 TH1F efficiencyForSensor4;
00218 s4->getEfficiencyCurve(efficiencyForSensor4, 10, 50, 150);
00219 efficiencyForSensor4.Write();
00220
00221 TH2F fourthHitResid;
00222 s4->getFourthHitResidualPlot(fourthHitResid);
00223 fourthHitResid.Write();
00224
00225 file->Write();
00226 file->Close();
00227
00228 std::cout << "Exporting to root file...\n";
00229 mtm.exportToRootFile("MapsTrackManager.root");
00230
00231 std::cout << "Recreating from root file.\n";
00232 MapsTrackManager mtm2;
00233 mtm2.recreateFromRootFile("MapsTrackManager.root");
00234 //std::cout << "Rewriting to new root file...\n";
00235 //mtm2.exportToRootFile("MapsTrackManager2.root");
00236
00237
00238 std::cout << "Done.\n";
00239 return 0;
00240 }
00241