00001 #include "MapsTrackManager.hh"
00002 #include "MapsTrack.hh"
00003 #include "MapsSensor.hh"
00004 #include "TFile.h"
00005 #include "TTree.h"
00006 #include "Rtypes.h"
00007 #include "TBranch.h"
00008 #include <map>
00009 #include <vector>
00010 #include <string>
00011 #include "ToString.h"
00012 #include <cassert>
00013
00014 MapsTrackManager::MapsTrackManager(std::vector<MapsSensor*> sensors) {
00015 mySensors = sensors;
00016 for (std::vector<MapsSensor*>::const_iterator it = sensors.begin(); it
00017 != sensors.end(); it++)
00018 mySensorIds.push_back((*it)->id());
00019 myRequiredLeft = 0;
00020 myRequiredRight = 0;
00021 }
00022 MapsTrackManager::MapsTrackManager() {
00023 myRequiredLeft = 0;
00024 myRequiredRight = 0;
00025
00026 }
00027 MapsTrackManager::~MapsTrackManager() {
00028
00029 }
00030 void MapsTrackManager::addTrack(MapsTrack* mt) {
00031 myTracks.push_back(mt);
00032 }
00033 std::vector<MapsTrack*>& MapsTrackManager::getTracks() {
00034 return myTracks;
00035 }
00036 std::vector<MapsSensor*>& MapsTrackManager::getSensors() {
00037 return mySensors;
00038 }
00039
00040 void MapsTrackManager::addSensor(MapsSensor* s) {
00041 mySensors.push_back(s);
00042 mySensorIds.push_back(s->id());
00043 }
00044
00045 void MapsTrackManager::setSensors(std::vector<MapsSensor*> sensors) {
00046 mySensors = sensors;
00047 }
00048
00049 void MapsTrackManager::setRequiredLeftSensor(MapsSensor* left) {
00050 myRequiredLeft = left;
00051 }
00052
00053 void MapsTrackManager::setRequiredRightSensor(MapsSensor* right) {
00054 myRequiredRight = right;
00055 }
00056
00057 void MapsTrackManager::recreateFromRootFile(char* rootFileName)
00058 throw (MapsException) {
00059 TFile* file = new TFile(rootFileName);
00060 if (file == 0) {
00061 std::string desc(__PRETTY_FUNCTION__);
00062 desc.append(": Couldn't open root file!\n");
00063 MapsException me(desc.c_str());
00064 throw me;
00065 }
00066
00067 TTree* tree = (TTree*) file->Get("mapsTracks");
00068
00069
00070 UInt_t sensorIds[4];
00071 Double_t sensorZs[4];
00072 Double_t sensorPhis[4];
00073 Double_t xAlign[4];
00074 Double_t yAlign[4];
00075
00076 TBranch* sensorBr = tree->GetBranch("sensorIds");
00077 sensorBr->SetAddress(sensorIds);
00078 TBranch* sensorZBr = tree->GetBranch("sensorZs");
00079 sensorZBr->SetAddress(sensorZs);
00080 TBranch* sensorPhiBr = tree->GetBranch("sensorPhis");
00081 sensorPhiBr->SetAddress(sensorPhis);
00082 TBranch* sensorXAl = tree->GetBranch("xAlign");
00083 sensorXAl->SetAddress(xAlign);
00084 TBranch* sensorYAl = tree->GetBranch("yAlign");
00085 sensorYAl->SetAddress(yAlign);
00086
00087 sensorBr->GetEntry(0);
00088 sensorZBr->GetEntry(0);
00089 sensorPhiBr->GetEntry(0);
00090 sensorXAl->GetEntry(0);
00091 sensorYAl->GetEntry(0);
00092
00093
00094 std::vector<MapsSensor*> sensors;
00095 std::map<unsigned, MapsSensor*> sensorsAndKeys;
00096 std::vector<unsigned> ids;
00097
00098 for (unsigned k(0); k < 4; k++) {
00099 MapsSensor::physXY align(xAlign[k], yAlign[k]);
00100 MapsSensor* s = new MapsSensor(sensorIds[k], sensorZs[k], align, sensorPhis[k]);
00101
00102 sensors.push_back(s);
00103 ids.push_back(sensorIds[k]);
00104 sensorsAndKeys[sensorIds[k]] = s;
00105 std::cout << "Recreated sensor:\t" << *s << "\n";
00106 }
00107 mySensors = sensors;
00108 mySensorIds = ids;
00109
00110 std::cout << "Recreated sensors successfully!\n";
00111
00112
00113 UInt_t bx;
00114 UInt_t fourthSensor;
00115 UInt_t fourthSensorThresh;
00116 UInt_t nHits;
00117 UInt_t sensorHit[4];
00118 UInt_t xHit[4];
00119 UInt_t yHit[4];
00120
00121 TBranch* bxBr = tree->GetBranch("bx");
00122 bxBr->SetAddress(&bx);
00123
00124 TBranch* fourthSensorBr = tree->GetBranch("fourthSensor");
00125 fourthSensorBr->SetAddress(&fourthSensor);
00126
00127 TBranch* fourthSensorThreshBr = tree->GetBranch("fourthSensorThresh");
00128 fourthSensorThreshBr->SetAddress(&fourthSensorThresh);
00129
00130 TBranch* nHitsBr = tree->GetBranch("nHits");
00131 nHitsBr->SetAddress(&nHits);
00132
00133 TBranch* sensorHitBr = tree->GetBranch("sensorHit");
00134 sensorHitBr->SetAddress(sensorHit);
00135
00136 TBranch* xHitBr = tree->GetBranch("xHit");
00137 xHitBr->SetAddress(xHit);
00138
00139 TBranch* yHitBr = tree->GetBranch("yHit");
00140 yHitBr->SetAddress(yHit);
00141 std::cout << "Created branches.\n";
00142
00143 double chiXSum(0);
00144 std::vector<MapsTrack*> tracks;
00145 std::cout << "Tree has " << tree->GetEntries() << " entries\n";
00146 for (unsigned entries(0); entries < tree->GetEntries(); entries++) {
00147 tree->GetEntry(entries);
00148 std::map<MapsSensor*, MapsTrack::coord> hits;
00149
00150 for (unsigned k(0); k < nHits; k++) {
00151 MapsSensor* relevantS = sensorsAndKeys[sensorHit[k]];
00152
00153 MapsTrack::coord* relevantHit = new MapsTrack::coord(xHit[k], yHit[k]);
00154 hits[relevantS] = *relevantHit;
00155
00156 }
00157 MapsTrack* t = new MapsTrack(bx, hits, sensorsAndKeys[fourthSensor], fourthSensorThresh);
00158
00159
00160 tracks.push_back(t);
00161 chiXSum += t->chiSq(0);
00162 }
00163 std::cout << "ChiXSum coming in: " << chiXSum << "\n";
00164 myTracks = tracks;
00165 std::cout << "Initialised myTracks with " << myTracks.size()
00166 << " entries.\n";
00167 }
00168
00169 void MapsTrackManager::exportToRootFile(char* rootFileName) {
00170 TFile* file = new TFile(rootFileName, "recreate");
00171 TTree* tree = new TTree("mapsTracks", "Maps Tracks");
00172 std::cout << "Created file and tree...\n";
00173
00174 UInt_t sensorIds[4];
00175 Double_t sensorZs[4];
00176 Double_t sensorPhis[4];
00177 Double_t xAlign[4];
00178 Double_t yAlign[4];
00179 UInt_t bx;
00180
00181 UInt_t fourthSensor;
00182 UInt_t fourthSensorThresh;
00183
00184 UInt_t nHits;
00185 UInt_t sensorHit[4];
00186 UInt_t xHit[4];
00187 UInt_t yHit[4];
00188 Double_t physX[4];
00189 Double_t physY[4];
00190
00191
00192
00193 Double_t residFourthX[1];
00194 Double_t residFourthY[1];
00195 Double_t chiSqProbX3Hit[1];
00196 Double_t chiSqProbY3Hit[1];
00197
00198 UInt_t fourthHitPresent(0);
00199
00200 Double_t chiXProb, chiYProb;
00201 Double_t p0, p1, q0, q1;
00202
00203 Double_t chiX, chiY, theta;
00204 Double_t meanX, meanY;
00205
00206 std::cout << "Defining branches...\n";
00207 tree->Branch("sensorIds", sensorIds, "sensorIds[4]/I");
00208 tree->Branch("sensorZs", sensorZs, "sensorZs[4]/D");
00209 tree->Branch("sensorPhis", sensorPhis, "sensorPhis[4]/D");
00210 tree->Branch("xAlign", xAlign, "xAlign[4]/D");
00211 tree->Branch("yAlign", yAlign, "yAlign[4]/D");
00212
00213 tree->Branch("bx", &bx, "bx/I");
00214 tree->Branch("fourthSensor", &fourthSensor, "fourthSensor/I");
00215 tree->Branch("fourthSensorThresh", &fourthSensorThresh,
00216 "fourthSensorThresh/I");
00217
00218 tree->Branch("nHits", &nHits, "nHits/I");
00219 tree->Branch("fourthHitPresent", &fourthHitPresent, "fourthHitPresent/I");
00220
00221 tree->Branch("chiX", &chiX, "chiX/D");
00222 tree->Branch("chiY", &chiY, "chiY/D");
00223 tree->Branch("chiXProb", &chiXProb, "chiXProb/D");
00224 tree->Branch("chiYProb", &chiYProb, "chiYProb/D");
00225 tree->Branch("theta", &theta, "theta/D");
00226
00227 tree->Branch("meanX", &meanX, "meanX/D");
00228 tree->Branch("meanY", &meanY, "meanY/D");
00229 tree->Branch("p0", &p0, "p0/D");
00230 tree->Branch("p1", &p1, "p1/D");
00231 tree->Branch("q0", &q0, "q0/D");
00232 tree->Branch("q1", &q1, "q1/D");
00233
00234
00235 tree->Branch("sensorHit", sensorHit, "sensorHit[nHits]/I");
00236 tree->Branch("xHit", xHit, "xHit[nHits]/I");
00237 tree->Branch("yHit", yHit, "yHit[nHits]/I");
00238 tree->Branch("physX", physX, "physX[nHits]/D");
00239 tree->Branch("physY", physY, "physY[nHits]/D");
00240 tree->Branch("residFourthX", residFourthX, "fourthHitX[fourthHitPresent]/D");
00241 tree->Branch("residFourthY", residFourthY, "fourthHitY[fourthHitPresent]/D");
00242 tree->Branch("chiSqProbX3Hit", chiSqProbX3Hit,
00243 "chiSqProbX3Hit[fourthHitPresent]/D");
00244 tree->Branch("chiSqProbY3Hit", chiSqProbY3Hit,
00245 "chiSqProbY3Hit[fourthHitPresent]/D");
00246
00247 std::cout << "Populating sensors...\n";
00248
00249 unsigned count(0);
00250 for (std::vector<MapsSensor*>::const_iterator it = mySensors.begin(); it
00251 != mySensors.end(); it++) {
00252 MapsSensor* s = *it;
00253 sensorIds[count] = s->id();
00254 sensorZs[count] = s->zPosition();
00255 sensorPhis[count] = s->phi();
00256 MapsSensor::physXY align = s->getAlignment();
00257 xAlign[count] = align.first;
00258 yAlign[count] = align.second;
00259 count++;
00260 std::cout << "\tExporting: " << *s <<"\n";
00261 }
00262
00263
00264
00265 std::cout << "Writing tracks: " << myTracks.size() << "\n";
00266
00267 double chiXSum(0);
00268 unsigned fourHitTracks(0);
00269 unsigned ok(0);
00270 unsigned anomaly(0);
00271 for (std::vector<MapsTrack*>::const_iterator it = myTracks.begin(); it
00272 != myTracks.end(); it++) {
00273 MapsTrack* t = *it;
00274 if (myRequiredLeft == 0 || myRequiredRight == 0)
00275 t->tellSensorsOfResiduals();
00276 else
00277 t->tellSensorsOfResiduals(myRequiredLeft, myRequiredRight);
00278 std::map<MapsSensor*, MapsTrack::coord> hits = t->getHits();
00279 std::map<MapsSensor*, MapsTrack::physXY> physicals = t->getGlobalHits();
00280
00281 bx = t->timeStamp();
00282
00283 if (t->fourthSensor() == 0) {
00284
00285 fourthSensor = 0;
00286 fourthSensorThresh = 0;
00287 } else {
00288
00289 fourthSensor = t->fourthSensor()->id();
00290 fourthSensorThresh = t->fourthSensorThresh();
00291 }
00292
00293 nHits = hits.size();
00294
00295 if (nHits == 4) {
00296 fourthHitPresent = 1;
00297 std::pair<double, double> fourthHitResid;
00298 MapsTrack threeHitVariant;
00299 t->make3HitTrack(threeHitVariant);
00300
00301 unsigned status = t->getFourthHitResidual(threeHitVariant,
00302 fourthHitResid);
00303 assert(status == 0);
00304 residFourthX[0] = fourthHitResid.first;
00305 residFourthY[0] = fourthHitResid.second;
00306 chiSqProbX3Hit[0] = threeHitVariant.chiSqProb(0);
00307 chiSqProbY3Hit[0] = threeHitVariant.chiSqProb(1);
00308 fourHitTracks++;
00309 } else {
00310 fourthHitPresent = 0;
00311 }
00312
00313 count = 0;
00314 for (std::map<MapsSensor*, MapsTrack::coord>::iterator hitIt =
00315 hits.begin(); hitIt != hits.end(); hitIt++) {
00316 sensorHit[count] = (*hitIt).first->id();
00317 xHit[count] = (*hitIt).second.first;
00318 yHit[count] = (*hitIt).second.second;
00319 physX[count] = physicals.at((*hitIt).first).first;
00320 physY[count] = physicals.at((*hitIt).first).second;
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 count++;
00337 }
00338
00339
00340 chiXSum += t->chiSq(0);
00341 chiX = t->chiSq(0);
00342 chiY = t->chiSq(1);
00343 chiXProb = t->chiSqProb(0);
00344 chiYProb = t->chiSqProb(1);
00345 meanX = t->meanX();
00346 meanY = t->meanY();
00347 theta = t->theta();
00348 std::pair<double, double> p = t->fitParameters(0);
00349 p0 = p.first;
00350 p1 = p.second;
00351
00352 std::pair<double, double> q = t->fitParameters(1);
00353 q0 = q.first;
00354 q1 = q.second;
00355
00356
00357 tree->Fill();
00358 }
00359 std::cout << "\tFound " << fourHitTracks << " 4 hit tracks.\n";
00360 std::cout << "ok: \t" << ok << ",\t anomolies: \t" << anomaly << "\n";
00361 for (std::vector<MapsSensor*>::const_iterator it = mySensors.begin(); it
00362 != mySensors.end(); it++) {
00363 TH2F resid;
00364 (*it)->getResidualXYPlot(resid);
00365 resid.Write();
00366 }
00367
00368 file->Write();
00369 file->Close();
00370
00371 }
00372