/*
Copyright (c) 2000                      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          : TTMDataChain.C
Author            : Rene Wilhelm
Date              : 13-OCT-2000
Description       : Implementation of TTMDataChain class (create chain of
		    ROOT TTree files for specific measurement period)
Language Version  : C++
OSs Tested        : Solaris 2.6
$Id: TTMDataChain.C,v 1.6 2001/06/19 13:47:50 wilhelm Exp $
-------------------------------------------------------------------------------
*/

#include "TTMDataChain.h"
#include "utils.h"
#include <string.h>
#include <iostream.h>



static char const rcsid[] = "$Id: TTMDataChain.C,v 1.6 2001/06/19 13:47:50 wilhelm Exp $";


TTMDataChain::TTMDataChain(TestBox src, time_t end,
	time_t per, char *path, char stype) {

// constructor of TTMDataChain object
// 	input:	src - source testbox
//		end - endtime of selected data
//		per - timeperiod of selected data
//		path - pathname to TTM ROOT datastorage
//		stype - type of datastorage ('H' = hierarchical year/month/day)

#define SUBPATHLENGTH 15	// "ttXY.ttYZ.root\0"  ---> 15 chars

	char subpath[SUBPATHLENGTH];
	char *fullpath;
	struct tm *timestruct;
	time_t starttime, timestamp;	
	int    pathlen;		// length of the *path string argument
	int    nfiles;		// number of files to chain

	const int seconds_per_day = 86400;

	// initalize data members
	source = src;
	endtime = end;
	period = per;
	storage = stype;

	starttime = endtime - period;

	// create ROOT chain
	chain = new TChain("tree");

	// copy path
	pathlen = strlen(path);
	datapath = new char[pathlen+1];
	fullpath = new char[pathlen+1+100];  // leaves 80 char for boxname 
	strcpy(datapath, path);
	strcpy(fullpath, path);

	if (starttime % seconds_per_day == 0) {
		// starttime on day boundary, push it forward a little
		// to get the correct data file
		timestamp = starttime + 5;  
	}
	else {
		timestamp = starttime;
	}

	// since endtime is in time_t (seconds since 1/1/1970) format
	// we can determine day numbers (since 1/1/1970) by simple integer
	// division; number of files to chain is difference between
        // end and start day.  Subtract and add 5 seconds repectively
	// to avoid rounding errors

	int endday = (endtime-5) / seconds_per_day;
	int startday = (endtime-period+5) / seconds_per_day;
	nfiles = (endday - startday) + 1;

	for (int i = nfiles; i>0; i--)
	{

		fullpath[pathlen] = '\0';  // truncate previous path

		timestruct = gmtime(&timestamp);
		if (storage == 'H') {
			// append appropriate <year>/<month>/<day>
			strftime(subpath, SUBPATHLENGTH, "/%Y/%m/%d", timestruct);
			strcat(fullpath, subpath);
		}
		strcat(fullpath, "/");

		// construct full filename from source name and timestruct
		strftime(subpath, SUBPATHLENGTH, ".%Y%m%d.root", timestruct);
		strcat(fullpath, source.name);
		strcat(fullpath, subpath);
			
		if (verbose) {
			cout << "fullpath: " << fullpath << endl;
		}
		chain->Add(fullpath);
		timestamp += seconds_per_day; // increase timestamp by a day
	}
}

TTMDataChain::~TTMDataChain() {

	delete chain;
	delete datapath;
}
