read1.cpp 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <fstream>

#include "TString.h"
#include "TFile.h"
#include "TError.h"
#include "TTree.h"

#include "../dataClasses/RawEvent.h"


int main(int argc, char* argv[])
{

	if (argc != 3) {
		// Tell the user how to run the program
		std::cerr << "Usage: " << argv[0] << " [list with input files] [outputfile]" << std::endl;
		/* "Usage messages" are a conventional way of telling the user
		 * how to run a program if they enter the command incorrectly.
		 */
		return 1;
	}
	// Print the user's name:
	TString infiles = argv[1];
	TString ofile = argv[2];

//	std::cout << argv[1] << "says hello, " << argv[2] << "!" << std::endl;
//	std::cout << infiles << "says hello, " << ofile << "!" << std::endl;
//	return 0;
29 30 31 32 33 34 35

	Double_t A1[1000],A2[1000],A3[1000],A4[1000];
	Double_t T[1000];
	Int_t i,j,k,n,p,Nchannel,Nlines;
	TString line1,line2,line3,line4,fileName;
	Double_t amp1,amp2,amp3,amp4,time;	

36
	TFile *f1 = new TFile(ofile.Data(),"RECREATE");
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	TTree *tree = new TTree("rtree","signal");
	tree->Branch("A1",A1,"A1[1000]/D");
	tree->Branch("A2",A2,"A2[1000]/D");
	tree->Branch("A3",A3,"A3[1000]/D");
	tree->Branch("A4",A4,"A4[1000]/D");
	tree->Branch("T",T,"T[1000]/D");	

	RawEvent *event1 = new RawEvent(1000);
	tree->Bronch("ch0.", "RawEvent", &event1);
	RawEvent *event2 = new RawEvent(1000);
	tree->Bronch("ch1.", "RawEvent", &event2);
	RawEvent *event3 = new RawEvent(1000);
	tree->Bronch("ch2.", "RawEvent", &event3);
	RawEvent *event4 = new RawEvent(1000);
	tree->Bronch("ch3.", "RawEvent", &event4);
////////
	RawEvent **Nevent;
	Nevent = new RawEvent *[4];
	Nevent[0] = event1;
	Nevent[1] = event2;
	Nevent[2] = event3;
	Nevent[3] = event4;	
/////////
	ifstream myfile; 	
	ifstream myfile1;
	ifstream myfile2;
	ifstream myfile3;
	ifstream myfile4;

66
	myfile.open(infiles.Data()); // открываю файл с названиями файлов с данными для подсчёта общего количества файлов
67 68 69 70 71 72 73 74
	if (myfile.is_open()) {					/// для создания динамического массива с путями к файлам с данными
		while(1){
			fileName.ReadLine(myfile);
			if ( fileName.IsNull() ) break;
			Nlines++;
		}			
		myfile.close();			
	}
75 76 77 78
	else {
		Error("read.cpp", "Some error when opening file %s", infiles.Data());
		return 1;
	}
79 80 81 82
	
	TString *NameLines = new TString[Nlines]; // создание массива в котором хранятся строки из файла "infiles.dat"
//	TString NameLines[Nlines];

83
	myfile.open(infiles.Data());
84 85 86
	for(i=0;;i++){
		fileName.ReadLine(myfile);
		NameLines[i] = fileName;
Muzalevsky I.A's avatar
Muzalevsky I.A committed
87
		if ( fileName.IsNull() ) {break;}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	}
	myfile.close();

	for(i=0;i<(-1+Nlines/4);i++){
		cout<<NameLines[i+1]<<endl; // открываю i - ый файл с данными для всех каналов
		cout<<NameLines[i+1+Nlines/4]<<endl;
		cout<<NameLines[i+1+Nlines/2]<<endl;
		cout<<NameLines[i+1+3*Nlines/4]<<endl;
	}

//	return;
	for(i=0;i<(-1+Nlines/4);i++){
		myfile1.open( NameLines[i+1] ); // открываю i - ый файл с данными для всех каналов
		myfile2.open( NameLines[i+1+Nlines/4] );
		myfile3.open( NameLines[i+1+Nlines/2] );
		myfile4.open( NameLines[i+1+3*Nlines/4] );

		if( ( myfile1.is_open() ) && ( myfile2.is_open() ) && ( myfile3.is_open() ) && ( myfile4.is_open() ) ) {
			amp1=amp2=amp3=amp4=time=0;
			j=0;
			while (1) {
				line1.ReadLine(myfile1);
				line2.ReadLine(myfile2);
				line3.ReadLine(myfile3);
				line4.ReadLine(myfile4);	
				if( line1.IsNull() ) break;	

				sscanf(line1.Data(), "%lf", &amp1);
				sscanf(line2.Data(), "%lf", &amp2);	
				sscanf(line3.Data(), "%lf", &amp3);	
				sscanf(line4.Data(), "%lf", &amp4);	
119
				
120
				n=j/1000;
121 122
				time = 5e-04+ (j-1000*n)*1e-01;
	
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
				A1[j-1000*n] = amp1;
				A2[j-1000*n] = amp2;
				A3[j-1000*n] = amp3;
				A4[j-1000*n] = amp4;
	
				T[j-1000*n] = time;

				event1->SetAmp(amp1, j-1000*n);
				event2->SetAmp(amp2, j-1000*n);
				event3->SetAmp(amp3, j-1000*n);
				event4->SetAmp(amp4, j-1000*n);
				event1->SetTime(time, j-1000*n);
				event2->SetTime(time, j-1000*n);
				event3->SetTime(time, j-1000*n);
				event4->SetTime(time, j-1000*n);

				if(j-1000*n == 999) {
					tree->Fill();
					for(k=0;k<1000;k++){
						T[k]=0;
						A1[k]=0;A2[k]=0;A3[k]=0;A4[k]=0;
					}
					event1->Reset();
					event2->Reset();
					event3->Reset();
					event4->Reset();
				}
				j++;	
			}
		myfile1.close();
		myfile2.close();
		myfile3.close();	
		myfile4.close();
		}	
		else {
			Error("read.cpp", "Some error when opening data file number of %d", i);
159
			return 1;
160 161 162 163
		}
	}//for i
	tree->Write();
	f1->Close();
164 165

	return 0;
166
}