/// Class to calculate livetimes /// for a particular component /// /// Blair Jamieson (C) 2010 #include "ILiveTime.hxx" ///---------------------------------------------------------------- /// Initialization /// Requires name of component whose livetime is being checked, and /// time interval in seconds that live fraction is calculated over /// (default time interval is 8 hours = 28800 seconds) ILiveTime::ILiveTime(char * aname, int atinterval ) { fStatVsTime = NULL; fGrLiveFraction = NULL; fTimeInterval = atinterval; int alen = strlen(aname); fName = new char[ alen+1 ]; sprintf( fName, "%s", aname ); // setup relative time for TDatime use in ROOT TAxis nonsense fAxisT0 = new TDatime(2010,01,01,00,00,00); fAxisX0 = fAxisT0->Convert( kTRUE ); gStyle->SetTimeOffset( fAxisX0 ); }; ///---------------------------------------------------------------- /// Get TGraph time offset due to day light savings time /// messing things up int ILiveTime::GetDSTOffset( long atime ){ // convert atime to the kind needed by TGraph // set conversion time. Values of 1 hour // and 2 hour offset seemed to work. tm mt; localtime_r( (const time_t*) &atime, &mt ); int aTOffset=0; if ( mt.tm_isdst == 0 ) aTOffset=3600; else if ( mt.tm_isdst == 1 ) aTOffset=7200; return aTOffset; } ///---------------------------------------------------------------- /// Add another status value /// Status = 0 for good, and anything else for bad /// valid from time atimestart to atimeend /// default reading time is at atime void ILiveTime::AddStatus( long atimestart, long atimeend, long astatus ){ fStartTimes.push_back( atimestart ); fEndTimes.push_back( atimeend ); fStatus.push_back( astatus ); fTimes.push_back( int( atimestart - fAxisX0 + GetDSTOffset( atimestart ) ) ); return; } ///---------------------------------------------------------------- /// Build vector of time pairs when the status is good or bad void ILiveTime::BuildGoodBad(){ if ( fStartTimes.size() <= 0 ) { std::cout<<" No list of times for "< 0 ){ //fStatVsTime = new TGraph( fTimes.size(), // &(fTimes[0]), // &(fStatus[0]) ); if ( fTimesRed.size() > 0 ){ fStatVsTime = new TGraph( fTimesRed.size()); for (int i=0; iSetPoint (i, (Double_t)fTimesRed[i], (Double_t)fStatusRed[i] ); } char aname[256]; sprintf(aname,"%s_statvstime",fName); fStatVsTime->SetName(aname); sprintf(aname,"Status vs Time for %s",fName); fStatVsTime->SetTitle(aname); fStatVsTime->SetMarkerColor(kRed); fStatVsTime->SetLineColor(kRed); fStatVsTime->SetLineWidth(1); fStatVsTime->SetMarkerStyle(8); fStatVsTime->SetMarkerSize(0.2); fStatVsTime->GetXaxis()->SetTimeDisplay(1); fStatVsTime->GetXaxis()->SetLabelOffset(0.03); fStatVsTime->GetXaxis()->SetTimeFormat("#splitline{%b/%d}{%H:%M}"); fStatVsTime->GetXaxis()->SetTimeOffset( fAxisX0, "gmt" ); fStatVsTime->GetXaxis()->SetNdivisions(507); fStatVsTime->GetXaxis()->SetLabelSize(0.04); fStatVsTime->GetYaxis()->SetTitle("Status"); } else { std::cout<<"No list of good and bad times... no graph made!"< No list of times for "<= fTimeInterval ){ // calculate live fraction for interval float afrac = float( atgood ) / float( atsum ); // put point at start and end of interval fLiveTimes.push_back( float(int(atstart - fAxisX0 + GetDSTOffset( atstart )+ 1)) ); fLiveFraction.push_back( afrac ); fLiveTimes.push_back( float(int( fEndTimes[i] - fAxisX0 + GetDSTOffset( fEndTimes[i] ) - 1 )) ); fLiveFraction.push_back( afrac ); // reset counters atgood = 0; atsum = 0; atstart = fEndTimes[i]; } } // add last points for remaining time if ( atsum > 0 ){ // calculate live fraction for interval float afrac = float( atgood ) / float( atsum ); // put point at start and end of interval fLiveTimes.push_back( float(int(atstart -fAxisX0 + GetDSTOffset( atstart) + 1)) ); fLiveFraction.push_back( afrac ); fLiveTimes.push_back( float(int( fEndTimes[ fEndTimes.size()-1 ] - fAxisX0 + GetDSTOffset( fEndTimes[ fEndTimes.size() -1 ] ) - 1 )) ); fLiveFraction.push_back( afrac ); } // build graph of live fraction if ( fLiveTimes.size() > 0 ){ fGrLiveFraction = new TGraph( fLiveTimes.size(), &(fLiveTimes[0]), &(fLiveFraction[0]) ); char aname[256]; sprintf(aname,"%s_livefraction",fName); fGrLiveFraction->SetName(aname); sprintf(aname,"Live Fraction for %s",fName); fGrLiveFraction->SetTitle(aname); fGrLiveFraction->SetMarkerColor(kBlue); fGrLiveFraction->SetLineColor(kBlue); fGrLiveFraction->SetLineWidth(1); fGrLiveFraction->SetMarkerStyle(8); fGrLiveFraction->SetMarkerSize(0.2); fGrLiveFraction->GetXaxis()->SetTimeDisplay(1); fGrLiveFraction->GetXaxis()->SetLabelOffset(0.03); fGrLiveFraction->GetXaxis()->SetTimeFormat("#splitline{%b/%d}{%H:%M}"); fGrLiveFraction->GetXaxis()->SetTimeOffset( fAxisX0 ,"gmt"); fGrLiveFraction->GetXaxis()->SetNdivisions(507); fGrLiveFraction->GetXaxis()->SetLabelSize(0.04); fGrLiveFraction->GetYaxis()->SetTitle("Live Fraction"); } else { std::cout<<"No live fractions calculated"<0) std::cout<<"\tStatus is GOOD:"< >::iterator ptrGood = fGoodTimes.begin(); ptrGood != fGoodTimes.end() ; ++ptrGood ){ acstr = ctime( (const time_t*)& (*ptrGood).first ); sprintf( astart, "%s", acstr ); int alen = strlen( astart ); astart[alen-1] = 0x00; acstr = ctime( (const time_t*)& (*ptrGood).second ); sprintf( aend, "%s", acstr ); alen = strlen( aend ); aend[alen-1] = 0x00; std::cout<<"\t\t"<< (*ptrGood).first << " " << (*ptrGood).second <<" 0 " << astart << " " << aend << std::endl; } if (fBadTimes.size()>0) std::cout<<"\tStatus is BAD:"< >::iterator ptrBad = fBadTimes.begin(); ptrBad != fBadTimes.end() ; ++ptrBad ){ acstr = ctime( (const time_t*)& (*ptrBad).first ); sprintf( astart, "%s", acstr ); int alen = strlen( astart ); astart[alen-1] = 0x00; acstr = ctime( (const time_t*)& (*ptrBad).second ); sprintf( aend, "%s", acstr ); alen = strlen( aend ); aend[alen-1] = 0x00; std::cout<<"\t\t"<< (*ptrBad).first << " " << (*ptrBad).second << " " << fBadStatus[ibadidx] << " " << astart << " " << aend << std::endl; ibadidx++; } } ///---------------------------------------------------------------- /// Process list of times and status to build graphs, and output them /// to a png file, and print status to screen void ILiveTime::Process(bool verbose, bool makeplots){ if ( fStartTimes.size() <= 0 ){ std::cout<<" No list of times for "<-1) { Maximum = 70000.; //16129.1; minimum = 0.1; } if(tpc2>-1) { Maximum = 20000000.;//1032193.1; minimum = 50000.; } if(tpc3>-1) { Maximum = 10000000000.; minimum = 10000000.; } char aname[256]; sprintf(aname,"%s_status_%ld_to_%ld.png",fName,fStartTimes[0],fEndTimes[ fEndTimes.size()-1 ] ); // TCanvas cc(aname,aname,1200,600); TCanvas c1(aname,aname,1200,600); c1.SetLogy(); TGraph * gr = GraphStatVsTime(); if (makeplots==true){ gr->SetMinimum(minimum); gr->SetMaximum(Maximum); gr->Draw("ALP"); c1.Print(aname); } TCanvas c2(aname,aname,1200,600); sprintf(aname,"%s_livetime_%ld_to_%ld.png",fName,fStartTimes[0],fEndTimes[ fEndTimes.size()-1 ] ); gr = GraphLiveFraction(); if (makeplots==true){ gr->SetMinimum(-0.1); gr->SetMaximum(1.1); gr->Draw("ALP"); c2.Print(aname); } // Now print list of times if (verbose==true) Print(); } /// Write log file in format needed for detector status database entries void ILiveTime::WriteLog( char * aauthor, char * acomment, char * aname){ // build file name char outfilename[256]; sprintf(outfilename,"%s_status_%ld_to_%ld.txt",fName,fStartTimes[0],fEndTimes[ fEndTimes.size()-1 ] ); std::ofstream outfile; outfile.open (outfilename, std::ios::out | std::ios::trunc ); // open for output, erase existing file // get timestamp for now: long fCurTime = time(NULL); bool timesorted=true; if (timesorted==true){ // write statuses int iallidx = 0; for ( std::vector< std::pair >::iterator ptrGood = fAllTimes.begin(); ptrGood != fAllTimes.end() ; ++ptrGood ){ // outfile<<"\'"< >::iterator ptrGood = fGoodTimes.begin(); ptrGood != fGoodTimes.end() ; ++ptrGood ){ outfile<< (*ptrGood).first << "|" << (*ptrGood).second <<"|0|" << fCurTime << "|\'" <<"oaSlowControlDatabase"<<"\'|\'" << aauthor << "\'|\'" <<"flagschema"<< "\'|" << aname << "\'|\'" << acomment<<"\'|" << std::endl; } // write bad statuses int ibadidx = 0; for ( std::vector< std::pair >::iterator ptrBad = fBadTimes.begin(); ptrBad != fBadTimes.end() ; ++ptrBad ){ outfile<< (*ptrBad).first << "|" << (*ptrBad).second <<"|" << fBadStatus[ibadidx] << "|" << fCurTime << "|\'" <<"oaSlowControlDatabase"<<"\'|\'" << aauthor << "\'|\'" <<"flagschema"<< "\'|" << aname << "\'|\'" << acomment<<"\'|" << std::endl; ibadidx++; } } outfile.close(); return; }