00001 #include "ArchTestResult.h"
00002 #include <TFile.h>
00003 #include <TKey.h>
00004 #include <TDirectory.h>
00005 #include <TSystem.h>
00006 #include <TNamed.h>
00007 #include <TObject.h>
00008 #include <TObjArray.h>
00009 #include <TString.h>
00010 #include <TObjString.h>
00011 #include <TVector.h>
00012 #include <TVectorD.h>
00013 #include <TH1.h>
00014 #include <TH2.h>
00015 #include <TCanvas.h>
00016 #include <TROOT.h>
00017 #include <is/isinfo.h>
00018 #include <iostream>
00019 #include <vector>
00020 #include <boost/shared_ptr.hpp>
00021 #include <iomanip>
00022 #include <fstream>
00023 #include <iostream>
00024
00025 #include "ArchivingManager.h"
00026 #include "ArchModuleDefects.h"
00027
00028 using namespace SctData;
00029 using namespace Sct;
00030 using namespace boost;
00031 using boost::shared_ptr;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 ArchTestResult::ArchTestResult(): Archiving() {
00046
00047 cout<< "I am in the ArchTestResult Default Constructor"<< endl;
00048 runNumber = 0;
00049 moduleName = "0";
00050 NScans = 0;
00051 names = new ArchTestNames();
00052
00053 testSummary = NULL;
00054 }
00055
00056
00057 ArchTestResult::ArchTestResult(shared_ptr<TestResult> test): Archiving() {
00058
00059 cout<< "I am in the ArchTestResult Constructor"<< endl;
00060
00061
00062
00063
00064
00065 runNumber = test->getRunNumber();
00066 moduleName = test->getModuleName();
00067 NScans = test->getNScans();
00068 names = new ArchTestNames(runNumber, moduleName,test->getClassName());
00069 testType = names->getTestType();
00070
00071 for (unsigned int i=0; i<NScans; i++) {
00072 scanNumbers.push_back(test->getScanNumberAt(i));
00073 }
00074
00075
00076 defects = test->getDefects().getAllDefects();
00077
00078
00079
00080
00081
00082 testSummary = NULL;
00083
00084
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 ArchTestResult::~ArchTestResult() {
00097
00098
00099
00100
00101
00102 }
00103
00104
00105
00106 void ArchTestResult::SaveRawScans() {
00107
00108 cout << "Saving RawScans " << endl;
00109
00110
00111
00112
00113 for (vector<shared_ptr<RawScanResult> >::iterator i = rawScans.begin(); i!=rawScans.end(); i++) {
00114
00115 shared_ptr<RawScanResult> raw = *i;
00116
00117 shared_ptr<ArchScanResult> myScanArchiving = ArchivingManager::instance().getScanFromMap(raw);
00118 shared_ptr<ArchScanResult> cloned_scan = myScanArchiving->clone(raw);
00119
00120 cloned_scan->SetTestType(testType);
00121 cloned_scan->Save();
00122
00123 }
00124
00125
00126
00127
00128 }
00129
00130
00131
00132 void ArchTestResult::SaveFitScans() {
00133
00134 cout << "Saving FitScans " << endl;
00135
00136
00137 for (vector<shared_ptr<FitScanResult> >::iterator i = fitScans.begin(); i!=fitScans.end(); i++) {
00138 shared_ptr<FitScanResult> fit = *i;
00139 shared_ptr<ArchScanResult> myScanArchiving = ArchivingManager::instance().getScanFromMap(fit);
00140 shared_ptr<ArchScanResult> cloned_scan = myScanArchiving->clone(fit);
00141
00142 cloned_scan->SetTestType(testType);
00143 cloned_scan->Save();
00144
00145
00146 }
00147
00148
00149 }
00150
00151
00152 vector<shared_ptr<const ModuleDefect> > ArchTestResult::DeleteRepetedDefects( vector<shared_ptr<const ModuleDefect> > tmp_defects ) {
00153
00154 vector<shared_ptr<const ModuleDefect> > skimmedDefects;
00155 vector<shared_ptr<const ModuleDefect> >::iterator ibegin=tmp_defects.begin();
00156 shared_ptr<const ModuleDefect> tmp_mydefect = *ibegin;
00157 skimmedDefects.push_back(tmp_mydefect);
00158
00159
00160
00161 for (unsigned int i=0; i<tmp_defects.size(); ++i) {
00162
00163
00164 string Name = tmp_defects[i]->getName();
00165 double first = tmp_defects[i]->getModuleElement().getFirst();
00166 double Nchannels = tmp_defects[i]->getModuleElement().getNChannels();
00167 double chipDefect = tmp_defects[i]->isChipDefect();
00168 double linkDefect = tmp_defects[i]->isLinkDefect();
00169 double unfittable = tmp_defects[i]->isUnfittable();
00170 double unusable = tmp_defects[i]->isUnuseable();
00171 double dodgy = tmp_defects[i]->isDodgy();
00172
00173
00174 bool defectToBeSaved = true;
00175
00176
00177 for (vector<shared_ptr<const ModuleDefect> >::iterator iSkimmed=skimmedDefects.begin();iSkimmed<skimmedDefects.end(); ++iSkimmed) {
00178 string skimmed_Name = (*iSkimmed)->getName();
00179 double skimmed_first = (*iSkimmed)->getModuleElement().getFirst();
00180 double skimmed_Nchannels = (*iSkimmed)->getModuleElement().getNChannels();
00181 double skimmed_chipDefect = (*iSkimmed)->isChipDefect();
00182 double skimmed_linkDefect = (*iSkimmed)->isLinkDefect();
00183 double skimmed_unfittable = (*iSkimmed)->isUnfittable();
00184 double skimmed_unusable =(*iSkimmed)->isUnuseable();
00185 double skimmed_dodgy = (*iSkimmed)->isDodgy();
00186
00187
00188
00189
00190
00191
00192 if (Name == skimmed_Name &&
00193 first == skimmed_first &&
00194 Nchannels == skimmed_Nchannels &&
00195 chipDefect == skimmed_chipDefect &&
00196 linkDefect == skimmed_linkDefect &&
00197 unfittable == skimmed_unfittable &&
00198 unusable == skimmed_unusable &&
00199 dodgy == skimmed_dodgy )
00200 {
00201
00202 defectToBeSaved = false;
00203 break;
00204 }
00205
00206
00207
00208 }
00209
00210
00211 if ( defectToBeSaved == true ) {
00212 skimmedDefects.push_back(tmp_defects[i]);
00213
00214 }
00215
00216
00217 }
00218
00219
00220
00221 return skimmedDefects;
00222
00223 }
00224
00225
00226 void ArchTestResult::Save() {
00227
00228 cout<<" Saving TestData " << endl;
00229
00230
00231
00232
00233 string filename = names->getfilename();
00234 string moduleID = names->getModuleID();
00235 string moduleDefectsID = names->getModuleDefectsID();
00236 string testType = names->getTestType();
00237 string runID = names->getRunID();
00238 string DefectsID = names->getDefectsID();
00239
00240
00241
00242 cout << "Opening file..."<<endl;
00243
00244
00245 TFile *storage_file = new TFile(filename.c_str(),"update");
00246
00247 cout << "The file .root succesfully open"<<endl;
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 TDirectory *this_run = (TDirectory*) gDirectory->Get(runID.c_str());
00259
00260
00261 if (!this_run) {
00262
00263 this_run = storage_file->mkdir(runID.c_str());
00264
00265 }
00266
00267 this_run->cd();
00268
00269
00270
00271 TObject* pers_runnumber = gDirectory->FindObjectAny(runID.c_str());
00272 if (!pers_runnumber) {
00273 double frunNumber = runNumber;
00274 TVector run(1,1,frunNumber, "END");
00275 run.Write(runID.c_str());
00276 }
00277
00278
00279
00280
00281 TDirectory* this_module = (TDirectory*) gDirectory->Get(moduleID.c_str());
00282
00283 if (!this_module) {
00284
00285 this_module =gDirectory ->mkdir(moduleID.c_str());
00286 }
00287 this_module->cd();
00288
00289
00290
00291 TDirectory* this_moduleDefects = (TDirectory*) gDirectory->Get(moduleDefectsID.c_str());
00292 if (!this_moduleDefects) {
00293
00294 this_moduleDefects =gDirectory ->mkdir(moduleDefectsID.c_str());
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 this_moduleDefects->cd();
00360
00361 ArchModuleDefects archDefects(defects);
00362
00363 vector< shared_ptr<TObjArray> > sortedDefectList = archDefects.getDefects();
00364
00365 cout<<" sortedDefectList size = " << sortedDefectList.size() <<endl;
00366
00367
00368 for (vector<shared_ptr<TObjArray> >::iterator it = sortedDefectList.begin();it != sortedDefectList.end() ; ++it)
00369 {
00370 this_moduleDefects->cd();
00371
00372
00373 shared_ptr<TObject> objFirst = (*it)->First();
00374 shared_ptr<TObjString> this_name = dynamic_pointer_cast<TObjString>(objFirst);
00375 const char *this_defectName = this_name->GetName();
00376 cout<< "Name = " <<this_defectName<<endl;
00377
00378
00379 cout<<"ArchTestResult: about to write sortedDefectList obj..."<<endl;
00380
00381 (*it)->Write(this_defectName,TObject::kSingleKey );
00382 cout<<"ArchTestResult: sortedDefectList obj written"<<endl;
00383
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 this_module->cd();
00397 TDirectory* this_testType = (TDirectory*) gDirectory->Get(testType.c_str());
00398
00399
00400 if (!this_testType) {
00401
00402 this_testType = gDirectory->mkdir(testType.c_str());
00403 }
00404 this_testType->cd();
00405
00406
00407
00408
00409
00410 storage_file->Close();
00411 delete storage_file;
00412 cout << "File closed " << endl;
00413 }
00414
00415
00416 void ArchTestResult::RetrieveRawFitScans() {
00417
00418
00419 for (vector<unsigned int>::iterator i = scanNumbers.begin(); i!=scanNumbers.end(); i++) {
00420 unsigned int scanNumber = *i;
00421 string RawISName = Archiving::GetRawISUniqueID(runNumber, scanNumber, moduleName);
00422 rawScans.push_back(ISRetrieveRawScanResult(RawISName));
00423
00424
00425 if (testType != "SctData::PipelineTestResult" && testType != "SctData::FullBypassTestResult" && testType != "SctData::NMaskTestResult") {
00426
00427 if (testType != "PipelineTest" && testType != "FullBypassTest" && testType != "NMaskTest") {
00428 string FitISName = Archiving::GetFitISUniqueID(runNumber, scanNumber, moduleName);
00429 fitScans.push_back(ISRetrieveFitScanResult(FitISName));
00430 }
00431
00432
00433 }
00434
00435
00436
00437 }
00438
00439 }
00440
00441
00442
00443 void ArchTestResult::AddTestToIndex() {
00444
00445
00446
00447
00448 string index = names->getIndexFileName();
00449 ofstream index_file;
00450 index_file.open(index.c_str(),ios::app);
00451
00452
00453 string indexTestName = names->getRunID() + ".";
00454 indexTestName = indexTestName + names->getModuleID();
00455 indexTestName = indexTestName + ".";
00456 indexTestName = indexTestName + names->getTestType() + ".";
00457
00458
00459 index_file << indexTestName <<endl;
00460 cout << "Test appended to Index"<<endl;
00461
00462
00463
00464
00465 index_file.close();
00466
00467 }