/*
Copyright (c) 2001                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
-------------------------------------------------------------------------------
Module Header
Filename          : TTMSummaryChain.h
Author            : Rene Wilhelm
Date              : 21-SEP-2001
Description       : TTMSummaryChain class definition (create chain of
                    ROOT files with TTM summary data for specific period)
Language Version  : C++
OSs Tested        : Solaris 2.6, Debian Linux 2.2
$Id: TTMSummaryChain.h,v 1.2 2003/01/16 17:30:18 wilhelm Exp $
-------------------------------------------------------------------------------
*/

#ifndef TTMSummaryChain_included
#define TTMSummaryChain_included

#include "DynamicArray.h"

#include "TestBoxConfig.h"	// TTM  TestBoxConfig class
#include "TTMOptions.h"		// TTM  Program Options class
#include "Percentiles.h"
#include "Delay.h"
#include "utils.h"

#include <time.h>
#include <TChain.h> 		// ROOT TChain class
#include <TPad.h> 		// ROOT TPad class
#include <TH1.h> 		// ROOT 1D histo class
#include <TH2.h> 		// ROOT 2D histo class


class TTMSummaryChain {
private:
	TestBoxConfig *tbConfig;    // Testbox Production Chain Configuration
				    // to see which boxes were ON
	TTMOptions    *progOptions; // Program options

	int *id2index;   	    // map box id to array index
	int numBoxes;		    // number of distinct (src/tgt) boxes
	int numIntervals;	    // number of intervals per day
				    // (hard to deduce from data set)
	int numChainFiles;          // number of files in the chain
	int ipVersion;		    // version of IP protocol to report on


	// 3D arrays to store short term (i.e. one day) delay parameters
	// indexes: source box, target box, interval no.

	DynamicArray<double> shortTermMedian;  
	DynamicArray<double> shortTermLevel025;
	DynamicArray<double> shortTermLevel975;
	DynamicArray<float> shortTermLossRate;

	// 3D arrays to determine long term Percentiles of the
	// short term (numIntervals/day) delay parameters

	DynamicArray<Percentiles> longTermMedian;  
	DynamicArray<Percentiles> longTermLevel025;
	DynamicArray<Percentiles> longTermLevel975;
	DynamicArray<Percentiles> longTermLossRate;
					   	 
	// arrays for histogram objects

	DynamicArray<TH1F*> level025Hist1D;
	DynamicArray<TH1F*> medianHist1D;
	DynamicArray<TH1F*> level975Hist1D;

	DynamicArray<TH2F*> level025Hist2D;
	DynamicArray<TH2F*> medianHist2D;
	DynamicArray<TH2F*> level975Hist2D;


	int fileProcessed;	    // flag if last day file has been processed
	int chainProcessed;	    // flag if whole chain has been processed
	int fillHistograms;	    // flag if histograms should be filled
	int plotHistoFilled;	    // flag if histograms for plots have been filled


	char *dataPath;		// path to (top of) ROOT data storage
	char  storageType;	// storage type: 'H' = year/month/day hierarchy
				//               'F' = flat

	char *outputDir;	// output filename for Summary Info.
	plotformat plotFormat;  // format of output plots (gif, ps, ..)

	TChain *sumChain;	// ROOT Chain object 
	TTree  *sumTree;	// TTree with summary info
	TFile  *sumFile;	// ROOT file with last day summary data

	time_t endTime;		// endtime for TTM Data chain
	time_t timePeriod;	// time period of Data chain
				// (not necessarily integer number of days)

	int InitHistograms();
		// Create all the necessary histogram objects
		
	// void FillHistograms(DelaySummary *summary);
		// Fill relevant histograms for later plotting

	int ProcessChain();
		// process all data in chain, fill Percentiles etc.
		// returns number of (valid) items processed

	int ProcessFile();
		// process all data in last file of chain
		// returns number of (valid) items processed

	void FillCell(char *src, char *dst, double shortterm, double longterm,
		float level, ofstream& html, int multiplier=1);
		// output HTML code to fill one Table cell with data

	void ResetHistograms();
		// Reset all existing histograms 

	int TTMSummaryChain::FillHistos(unsigned int); 
		// Process chain, fill histograms for specified sourceId

	void PlotHistos(TestBox* source, TestBox* target);
		// Plot the filled Histograms

	void OutputCSV(char* pathname, TestBox* source, TestBox* target);
		// Output chained data for source/target combination
		// in comma separated field format to specified file

	void write_stats(TPad* pad, const char* ptr_text, const Float_t x,
	       const Float_t y, const Short_t align, Font_t font, const Short_t color);


public:
	~TTMSummaryChain();				  // destructor
	TTMSummaryChain();				  // constructor

	void SetConfig(TTMOptions &opt, TestBoxConfig& conf, int numint);
		//  opt    - object holding program's options 
		//  conf   - configuration of TTM chain (at endtime)
		//  numint - (max) number of intervals per day 

	void Chain();  
		// Create the ROOT data chain


	void OutputHTML(float triggerLevel);
		// Create HTML output from summary chain
		// For each box create a page with parameters 
		// of the incoming and outgoing TTM data
		// triggerLevel - fractional increase/decrease that triggers
		//		  color coding table entries

	void OutputXML();
		// Create XML output from summary chain
		// The resulting file can be used by CGI scripts on
		// the webserver, to allow more user interaction with
		// ordering and color coding the summary data

	void PlotAll();           
		// Create the plots 

	void MakePlots(char* plotdir, TestBox* source);
		// Create plots for source
		// plotdir is usually a subdir of outputDir data member


	// void Scores();         // compute performance scores
};

#endif 

