#include <TTree.h>

void settoflev1(TTree *tree, Toflev1 & toflev1){
   tree->SetBranchAddress("good2",&toflev1.good2);
   tree->SetBranchAddress("nev2",&toflev1.nev2);
   tree->SetBranchAddress("pkt_type",&toflev1.pkt_type);
   tree->SetBranchAddress("pkt_num",&toflev1.pkt_num);
   tree->SetBranchAddress("obt",&toflev1.obt);
   tree->SetBranchAddress("which_calib",&toflev1.which_calib);
   tree->SetBranchAddress("tdcid",toflev1.tdcid);
   tree->SetBranchAddress("evcount",toflev1.evcount);
   tree->SetBranchAddress("tdcmask",toflev1.tdcmask);
   tree->SetBranchAddress("adc",toflev1.adc);
   tree->SetBranchAddress("tdc",toflev1.tdc);
   tree->SetBranchAddress("temp1",toflev1.temp1);
   tree->SetBranchAddress("temp2",toflev1.temp2);
   tree->SetBranchAddress("beta",toflev1.beta);
   tree->SetBranchAddress("xtof",toflev1.xtof);
   tree->SetBranchAddress("ytof",toflev1.ytof);
   tree->SetBranchAddress("adc_c",toflev1.adc_c);
   tree->SetBranchAddress("iflag",toflev1.iflag);
   tree->SetBranchAddress("jflag",toflev1.jflag);
   tree->SetBranchAddress("xout",toflev1.xout);
   tree->SetBranchAddress("yout",toflev1.yout);
   tree->SetBranchAddress("trig_evcount",&toflev1.trig_evcount);
   tree->SetBranchAddress("pmtpl",toflev1.pmtpl);
   tree->SetBranchAddress("trigrate",toflev1.trigrate);
   tree->SetBranchAddress("dltime",toflev1.dltime);
   tree->SetBranchAddress("s4calcount",toflev1.s4calcount);
   tree->SetBranchAddress("pmtcount1",toflev1.pmtcount1);
   tree->SetBranchAddress("pmtcount2",toflev1.pmtcount2);
   tree->SetBranchAddress("patternbusy",toflev1.patternbusy);
   tree->SetBranchAddress("patterntrig",toflev1.patterntrig);
   tree->SetBranchAddress("trigconf",&toflev1.trigconf);
   tree->SetBranchAddress("ntrk",&toflev1.ntrk);
   tree->SetBranchAddress("image",&toflev1.image);
   tree->SetBranchAddress("xm",toflev1.xm);
   tree->SetBranchAddress("ym",toflev1.ym);
   tree->SetBranchAddress("zm",toflev1.zm);
   tree->SetBranchAddress("resx",toflev1.resx);
   tree->SetBranchAddress("resy",toflev1.resy);
   tree->SetBranchAddress("al",toflev1.al);
   tree->SetBranchAddress("coval",toflev1.coval);
   tree->SetBranchAddress("chi2",toflev1.chi2);
   tree->SetBranchAddress("xgood",toflev1.xgood);
   tree->SetBranchAddress("ygood",toflev1.ygood);
   tree->SetBranchAddress("xv",toflev1.xv);
   tree->SetBranchAddress("yv",toflev1.yv);
   tree->SetBranchAddress("zv",toflev1.zv);
   tree->SetBranchAddress("axv",toflev1.axv);
   tree->SetBranchAddress("ayv",toflev1.ayv);
   tree->SetBranchAddress("dedxp",toflev1.dedxp);
   tree->SetBranchAddress("nclsx",toflev1.nclsx);
   tree->SetBranchAddress("nclsy",toflev1.nclsy);
}

void accesstoflev1(Toflev1 & toflev1, CToflev1 & ctoflev1){
    ctoflev1.good2 = toflev1.good2;
    ctoflev1.nev2 = toflev1.nev2;
    ctoflev1.pkt_type = toflev1.pkt_type;
    ctoflev1.pkt_num = toflev1.pkt_num;
    ctoflev1.obt = toflev1.obt;
    ctoflev1.which_calib = toflev1.which_calib;
    ctoflev1.ntrk = toflev1.ntrk;
    for (Int_t i = 0; i<4; i++){
	for (Int_t j = 0; j<24; j++){
	    if ( i == 0 ){
		ctoflev1.pmtcount1[j] = toflev1.pmtcount1[j]; 		
		ctoflev1.pmtcount2[j] = toflev1.pmtcount2[j]; 		
	    };	
	    if ( j < 12 ){
		ctoflev1.adc[i][j] = toflev1.adc[j][i];
		ctoflev1.tdc[i][j] = toflev1.tdc[j][i];
		ctoflev1.adc_c[i][j] = toflev1.adc_c[j][i];
		if ( i == 0 ){
		    ctoflev1.tdcid[j] = toflev1.tdcid[j];
		    ctoflev1.evcount[j] = toflev1.evcount[j];
		    ctoflev1.tdcmask[j] = toflev1.tdcmask[j];
		    ctoflev1.temp1[j] = toflev1.temp1[j];
		    ctoflev1.temp2[j] = toflev1.temp2[j];
		    if ( j < 6 ){
			ctoflev1.trigrate[j] = toflev1.trigrate[j];
			ctoflev1.patterntrig[j] = toflev1.patterntrig[j];
			ctoflev1.jflag[j] = toflev1.jflag[j];
			ctoflev1.iflag[j] = toflev1.iflag[j];
			if ( j < 5 ){
			    ctoflev1.beta[j] = toflev1.beta[j];
			};
		    };
		};
	    };
	};
	if ( i < 3 ){
	    ctoflev1.xtof[i] = toflev1.xtof[i];
	    ctoflev1.ytof[i] = toflev1.ytof[i];
	    ctoflev1.xout[i] = toflev1.xout[i];
	    ctoflev1.yout[i] = toflev1.yout[i];
	    ctoflev1.patternbusy[i] = toflev1.patternbusy[i];
	    ctoflev1.pmtpl[i] = toflev1.pmtpl[i];
	};
	if ( i < 2 ){
	    ctoflev1.dltime[i] = toflev1.dltime[i];
	    ctoflev1.s4calcount[i] = toflev1.s4calcount[i];
	};
    };
    ctoflev1.trigconf = toflev1.trigconf;
    ctoflev1.trig_evcount = toflev1.trig_evcount;
    //
    for (Int_t i = 0; i<toflev1.ntrk; i++){
	ctoflev1.chi2[i] = toflev1.chi2[i];
	ctoflev1.image[i] = toflev1.image[i];
	for (Int_t j = 0; j<6; j++){
	    ctoflev1.xm[j][i] = toflev1.xm[i][j];
	    ctoflev1.ym[j][i] = toflev1.ym[i][j];
	    ctoflev1.zm[j][i] = toflev1.zm[i][j];
	    ctoflev1.resx[j][i] = toflev1.resx[i][j];
	    ctoflev1.resy[j][i] = toflev1.resy[i][j];
	    ctoflev1.dedxp[j][i] = toflev1.dedxp[i][j];
	    if ( j<5 ) {
		ctoflev1.al[j][i] = toflev1.al[i][j];
		for (Int_t jp = 0; jp<5; jp++){
		    ctoflev1.coval[j][jp][i] = toflev1.coval[i][jp][j];
		};
	    };
	    ctoflev1.xgood[j][i] = toflev1.xgood[i][j];
	    ctoflev1.ygood[j][i] = toflev1.ygood[i][j];
	    ctoflev1.xv[j][i] = toflev1.xv[i][j];
	    ctoflev1.yv[j][i] = toflev1.yv[i][j];
	    ctoflev1.zv[j][i] = toflev1.zv[i][j];
	    ctoflev1.axv[j][i] = toflev1.axv[i][j];
	    ctoflev1.ayv[j][i] = toflev1.ayv[i][j];
	    if ( j == 0 ){
		ctoflev1.nclsx[j] = toflev1.nclsx[j];
		ctoflev1.nclsy[j] = toflev1.nclsy[j];
	    };
	};
    };
}
