////////////////////////////////////////////////////////
//  Small tool class to convert text files into TGraph 
//  objects
//  
//
//                    Author: I. Froehlich
//                    Written: 1.08.2007
//                    Revised: 
//
////////////////////////////////////////////////////////


#include "PArray.h"
#include <iostream>

using namespace std;

PArray::PArray(Int_t dimension) {
    dim = dimension;
    fp  = NULL;
    real_size_1d = 0;
    scaling = 1.;

    for (int i=0; i<PARRAY_MAX_COLUMNS; i++)
	vals_1d[i]=NULL;
};

PArray::~PArray() {
};

Bool_t PArray::OpenFile(const char *filename, Int_t, Int_t num_columns, Double_t y) {

    if ((dim == 1) && vals_1d[0]) {
	Warning("OpenFile", "Cannot read more then one file for 1dim arrays");
	return kFALSE;
    }

    fp = fopen(filename,"r");   // open file

    Double_t val[PARRAY_MAX_COLUMNS];
    Int_t    row = 0;

    if (!vals_1d[0]) {
	for (int i=0; i<num_columns; i++) {
	    vals_1d[i] = new TArrayD(PARRAY_GRANULARITY);
	}
	if (dim == 2)
	    vals_2d = new TArrayD(PARRAY_GRANULARITY);
    }

    if (fp == NULL) {
	Error("OpenFile", "Cannot open file %s", filename);
	return kFALSE;
    }
    
    Int_t ret = 0;
    while (ret != EOF) {
	if (num_columns == 5) {
	    ret = fscanf(fp,"%le %le %le %le %le", &val[0], &val[1], &val[2], &val[3], &val[4]);
	}

	for (int i=1; i<num_columns; i++)
	    val[i] *= scaling;

	if (ret != EOF) {

	    if (dim ==1) {
		if (vals_1d[0]->GetSize() > row) {
		    for (int i=0; i<num_columns; i++)
			vals_1d[i]->Set(vals_1d[0]->GetSize() + PARRAY_GRANULARITY);
		}
		for (int i=0; i<num_columns; i++)
		    vals_1d[i]->AddAt(val[i], row);
	    }

	    if (dim == 2) {
		if (vals_1d[0]->GetSize() > (real_size_1d+row)) {
		    for (int i=0; i<num_columns; i++) {
			vals_1d[i]->Set(vals_1d[0]->GetSize() + PARRAY_GRANULARITY);
		    }
		    vals_2d->Set(vals_2d->GetSize() + PARRAY_GRANULARITY);
		}
		for (int i=0; i<num_columns; i++) {
		    vals_1d[i]->AddAt(val[i], real_size_1d+row);
		}
		vals_2d->AddAt(y, real_size_1d+row);
	    }
	    row++;
	}
    }
    real_size_1d += row;

    Info("OpenFile", "%d lines read from file %s", row, filename);
    
    fclose(fp);
    return kTRUE;
}


TGraph *PArray::GetTGraph(Int_t xcol, Int_t ycol) {
    //NB: TGraph is not killed, it must be deleted in the app.
    
    if (dim == 1) {
	if (!vals_1d[0]) {
	    Error ("GetTGraph", "No (1dim) file opened");
	    return NULL;
	}
	TGraph *delme = new TGraph(real_size_1d, vals_1d[xcol]->GetArray(), vals_1d[ycol]->GetArray());
	return delme;

    }
    return NULL;
}

TGraph2D *PArray::GetTGraph2D(Int_t xcol, Int_t ycol) {
    //NB: TGraph is not killed, it must be deleted in the app.
    
    if (dim == 2) {
	if (!vals_1d[0]) {
	    Error ("GetTGraph", "No (2dim) files opened");
	    return NULL;
	}

	TGraph2D *delme = new TGraph2D(real_size_1d, vals_1d[xcol]->GetArray(),
				       vals_2d->GetArray(), vals_1d[ycol]->GetArray() );
// 	for (int i=0;i<real_size_1d;i++)
// 	    cout << (vals_1d[xcol]->GetArray())[i] << ":"
// 		 << (vals_2d->GetArray())[i] << ":"
// 		 << (vals_1d[ycol]->GetArray())[i] << ":"
// 		 << endl;
	return delme;

    }
    return NULL;
}

ClassImp(PArray)
 PArray.cc:1
 PArray.cc:2
 PArray.cc:3
 PArray.cc:4
 PArray.cc:5
 PArray.cc:6
 PArray.cc:7
 PArray.cc:8
 PArray.cc:9
 PArray.cc:10
 PArray.cc:11
 PArray.cc:12
 PArray.cc:13
 PArray.cc:14
 PArray.cc:15
 PArray.cc:16
 PArray.cc:17
 PArray.cc:18
 PArray.cc:19
 PArray.cc:20
 PArray.cc:21
 PArray.cc:22
 PArray.cc:23
 PArray.cc:24
 PArray.cc:25
 PArray.cc:26
 PArray.cc:27
 PArray.cc:28
 PArray.cc:29
 PArray.cc:30
 PArray.cc:31
 PArray.cc:32
 PArray.cc:33
 PArray.cc:34
 PArray.cc:35
 PArray.cc:36
 PArray.cc:37
 PArray.cc:38
 PArray.cc:39
 PArray.cc:40
 PArray.cc:41
 PArray.cc:42
 PArray.cc:43
 PArray.cc:44
 PArray.cc:45
 PArray.cc:46
 PArray.cc:47
 PArray.cc:48
 PArray.cc:49
 PArray.cc:50
 PArray.cc:51
 PArray.cc:52
 PArray.cc:53
 PArray.cc:54
 PArray.cc:55
 PArray.cc:56
 PArray.cc:57
 PArray.cc:58
 PArray.cc:59
 PArray.cc:60
 PArray.cc:61
 PArray.cc:62
 PArray.cc:63
 PArray.cc:64
 PArray.cc:65
 PArray.cc:66
 PArray.cc:67
 PArray.cc:68
 PArray.cc:69
 PArray.cc:70
 PArray.cc:71
 PArray.cc:72
 PArray.cc:73
 PArray.cc:74
 PArray.cc:75
 PArray.cc:76
 PArray.cc:77
 PArray.cc:78
 PArray.cc:79
 PArray.cc:80
 PArray.cc:81
 PArray.cc:82
 PArray.cc:83
 PArray.cc:84
 PArray.cc:85
 PArray.cc:86
 PArray.cc:87
 PArray.cc:88
 PArray.cc:89
 PArray.cc:90
 PArray.cc:91
 PArray.cc:92
 PArray.cc:93
 PArray.cc:94
 PArray.cc:95
 PArray.cc:96
 PArray.cc:97
 PArray.cc:98
 PArray.cc:99
 PArray.cc:100
 PArray.cc:101
 PArray.cc:102
 PArray.cc:103
 PArray.cc:104
 PArray.cc:105
 PArray.cc:106
 PArray.cc:107
 PArray.cc:108
 PArray.cc:109
 PArray.cc:110
 PArray.cc:111
 PArray.cc:112
 PArray.cc:113
 PArray.cc:114
 PArray.cc:115
 PArray.cc:116
 PArray.cc:117
 PArray.cc:118
 PArray.cc:119
 PArray.cc:120
 PArray.cc:121
 PArray.cc:122
 PArray.cc:123
 PArray.cc:124
 PArray.cc:125
 PArray.cc:126
 PArray.cc:127
 PArray.cc:128
 PArray.cc:129
 PArray.cc:130
 PArray.cc:131
 PArray.cc:132
 PArray.cc:133
 PArray.cc:134
 PArray.cc:135
 PArray.cc:136
 PArray.cc:137