00001 #include "ArchTrimRangeTestResult.h"
00002 #include "ArchivingManager.h"
00003 #include <is/isinfo.h>
00004 #include <iostream>
00005 #include <vector>
00006 #include "SctData/Stat.h"
00007 #include "Sct/SctParameters.h"
00008 #include <TFile.h>
00009 #include <TDirectory.h>
00010 #include <TObject.h>
00011 #include <TVector.h>
00012 #include <TVectorD.h>
00013 #include "SummaryWriter/SummaryWriter.h"
00014 #include "SummaryWriter/SummaryManager.h"
00015 
00016 
00017 using namespace SctData;
00018 
00019 bool ArchTrimRangeTestResult::inMap = ArchivingManager::instance().addTestArchiver("SctData::TrimRangeTestResult", shared_ptr<ArchTestResult>(new ArchTrimRangeTestResult()));  
00020 
00021 
00022 
00023 ArchTrimRangeTestResult::ArchTrimRangeTestResult(): ArchTestResult()  {
00024 
00025   
00026   
00027   
00028   
00029 }
00030   
00031   
00032 ArchTrimRangeTestResult::ArchTrimRangeTestResult(shared_ptr<TrimRangeTestResult> trimtest): ArchTestResult(trimtest)  {
00033 
00034   
00035   
00036   
00037   TGraph* g = new TGraph();
00038   TF1* f = new TF1("tf1","pol1");
00039   for (unsigned int ichip = 0; ichip < nChipModule; ++ichip) {
00040     for (unsigned ichannel=0; ichannel<nChannelChip; ++ichannel){
00041       Stat<TrimRangeTestResult::TrimData> trim = trimtest->chipTrimData[ichip]->channelData.getAt(ichannel);
00042       
00043       
00044       g->SetMarkerColor(ichannel);
00045       f->SetLineColor(ichannel);
00046       g->Set(trim.value.graph.size());
00047       for (unsigned ipt=0; ipt<trim.value.graph.size(); ++ipt){
00048     g->SetPoint(ipt,trim.value.graph[ipt].first, trim.value.graph[ipt].second);
00049       }
00050       f->SetParameters(trim.value.p0,trim.value.p1);
00051       
00052       chipTrim.push_back(g);
00053       chipTrimFit.push_back(f);
00054       
00055       
00056     }
00057     
00058     
00059   }
00060 
00061 
00062   
00063   try{
00064     TestSummary(trimtest);
00065   } catch(...) {
00066     cout<<" ERROR in retrieving Test Summary from IS" <<endl;
00067   }
00068 
00069 }
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 ArchTrimRangeTestResult::~ArchTrimRangeTestResult()  {
00080   
00081   
00082   
00083 
00084   
00085 }
00086 
00087 
00088 
00089 void ArchTrimRangeTestResult::SaveTrimRangeTestDataOnly() {
00090 
00091 
00092   cout<<"Saving TrimRangeTestData "<<endl;
00093      
00094   
00095   
00096   string filename = names->getfilename();
00097   string moduleID = names->getModuleID(); 
00098   string testType  = names->getTestType(); 
00099   string runID = names->getRunID();
00100   
00101   
00102 
00103 
00104   
00105   
00106  
00107 
00108   TFile *storage_file = new TFile(filename.c_str(),"update");
00109   
00110   
00111 
00112   
00113 
00114 
00115   
00116   
00117     
00118     
00119     
00120     
00121     TDirectory *this_run = (TDirectory*) gDirectory->Get(runID.c_str());
00122     
00123     
00124     if (!this_run) {
00125       
00126       this_run = storage_file->mkdir(runID.c_str());
00127       
00128     }
00129     storage_file->GetListOfKeys()->Print();
00130     this_run->cd();  
00131     
00132    
00133     
00134     TObject* pers_runnumber = gDirectory->FindObjectAny(runID.c_str()); 
00135     if (!pers_runnumber) {
00136       double frunNumber = runNumber;
00137       TVector run(1,1,frunNumber, "END");
00138       run.Write(runID.c_str());
00139     }
00140     
00141     
00142     
00143     
00144     TDirectory* this_module = (TDirectory*) gDirectory->Get(moduleID.c_str()); 
00145     
00146     if (!this_module) {
00147       
00148       this_module =gDirectory ->mkdir(moduleID.c_str());
00149     }
00150     
00151     
00152     
00153     
00154     this_module->cd();  
00155     TDirectory* this_testType = (TDirectory*) gDirectory->Get(testType.c_str());
00156     
00157     
00158     if (!this_testType) {
00159       
00160       this_testType = gDirectory->mkdir(testType.c_str());
00161     }
00162     this_testType->cd();
00163 
00164 
00165     
00166     int chipN = 0;
00167    
00168     for (vector<TGraph*>::iterator i = chipTrim.begin(); i!=chipTrim.end(); i++) {
00169       (*i)->Write(names->getChipTrimID(chipN).c_str());
00170       chipN++;
00171     }   
00172    
00173 
00174 
00175     
00176     chipN = 0;
00177     for (vector<TF1*>::iterator i = chipTrimFit.begin(); i!=chipTrimFit.end(); i++) {
00178       (*i)->Write(names->getChipTrimFitID(chipN).c_str());
00179       chipN++;
00180     }   
00181     
00182     
00183     
00184     if(testSummary != NULL) {
00185       
00186       TObject* pers_testSummary = gDirectory->FindObjectAny(names->getTestSummaryID().c_str());
00187       if (!pers_testSummary) {
00188     testSummary->Write(names->getTestSummaryID().c_str());
00189     
00190       }
00191     }
00192  
00193     
00194     
00195     
00196     storage_file->Close();
00197     delete storage_file;
00198     
00199 
00200 
00201 
00202 
00203 }
00204 
00205 
00206 
00207 void ArchTrimRangeTestResult::Save() {
00208 
00209   
00210   ArchTestResult::Save();
00211   SaveTrimRangeTestDataOnly();
00212   
00213   
00214   AddTestToIndex();
00215   
00216 
00217 
00218 }
00219 
00220 
00221 void ArchTrimRangeTestResult::TestSummary(shared_ptr<TrimRangeTestResult> trimtest) {
00222   
00223   ostringstream datastream;
00224   shared_ptr<const SctData::TestSummary::SummaryWriter> writer = 
00225     SctData::TestSummary::SummaryManager::instance().getWriter(trimtest->getClassName());
00226   writer->writeHeader(*(trimtest.get()), datastream);
00227   writer->write(*(trimtest.get()), datastream);
00228   string myString = datastream.str();
00229   
00230 
00231   testSummary = new TObjString(myString.c_str());
00232 
00233   
00234 
00235 
00236   return;
00237 
00238 
00239 }
00240 
00241 
00242