/**
 * \file TrkStruct.h
 * \author Elena Vannuccini
 * \date
 */
#include <TrkStruct.h>
//#include <TrkCalib.h>

using namespace pamela;
using namespace std;

float aint(float f){
    int   ret  = (int)f;
    float ret1 = 0;
    ret1=(float)ret;
    return ret1;	
}

float anint(float f){
    int   ret  = (int)(f+0.5);
    float ret1 = 0;
    ret1=(float)ret;
    return ret1;	
}
// /**
//  * Method to fill the tracker calibration-parameter struct from on-line calibrations
//  */
// void cTrkCalib::FillACalibFrom(TFile* f0, Int_t ev_reg1, Int_t ev_reg2){

//     TTree *tr1 = (TTree*)f0->Get("CalibTrk1");  
//     TTree *tr2 = (TTree*)f0->Get("CalibTrk2"); 

//     CalibTrk1Event *calibdata1 = 0;
//     CalibTrk2Event *calibdata2 = 0;
//     tr1->SetBranchAddress("CalibTrk1",&calibdata1);
//     tr2->SetBranchAddress("CalibTrk2",&calibdata2);
//     tr1->GetEntry(ev_reg1);
//     tr2->GetEntry(ev_reg2); 
//     Int_t dspn1(0),dspn2(0);
	
//     for(Int_t i=0;i<6;i++){
// 	dspn1=calibdata1->DSPnumber[i]-1;
// 	dspn2=calibdata2->DSPnumber[i]-1;
// 	/*     cout<<"dspn1= "<<dspn1<<"   dspn2= "<<dspn2<<endl; */
// 	for(Int_t ii=0;ii<3072;ii++){
// 	    Int_t j=(ii/128);
// 	    Int_t jj=ii-j*128; 
// 	    pedestal[jj][j][dspn1]=calibdata1->DSPped_par[i][ii];
// 	    pedestal[jj][j][dspn2]=calibdata2->DSPped_par[i][ii];
// 	    pedestal_t[jj][j][dspn1]=aint(calibdata1->DSPped_par[i][ii]);
// 	    pedestal_t[jj][j][dspn2]=aint(calibdata2->DSPped_par[i][ii]);
// 	    sigma[jj][j][dspn1]=calibdata1->DSPsig_par[i][ii];
// 	    sigma[jj][j][dspn2]=calibdata2->DSPsig_par[i][ii];
// 	    sigma_t[jj][j][dspn1]=anint(calibdata1->DSPsig_par[i][ii]);
// 	    sigma_t[jj][j][dspn2]=anint(calibdata2->DSPsig_par[i][ii]);
// 	    //	      cout<<"sigma trk1= "<<sigma[jj][j][dspn1]<<endl;
// 	    //        cout<<"sigma_t trk1= "<<sigma_t[jj][j][dspn1]<<endl;
// 	    //        cout<<"sigma trk2= "<<sigma[jj][j][dspn2]<<endl;
// 	    //        cout<<"sigma_t trk2= "<<sigma_t[jj][j][dspn2]<<endl;
	
// 	    //
// 	    // invert the bad
// 	    //
// 	    if(calibdata1->DSPbad_par[i][ii]==0) 
// 		bad[jj][j][dspn1]=1;
// 	    else if(calibdata1->DSPbad_par[i][ii]==1)
// 		bad[jj][j][dspn1]=0;
// 	    if(calibdata2->DSPbad_par[i][ii]==0)
// 		bad[jj][j][dspn2]=1;
// 	    else if(calibdata2->DSPbad_par[i][ii]==1)
// 		bad[jj][j][dspn2]=0;
// 	};
//     };
	
//     tr1->Delete();
//     tr2->Delete();
// };

// /**
//  * Method to fill the tracker calibration-parameter struct from on-line calibrations
//  */
// void cTrkCalib::FillFCalibFrom(TFile* f0, Int_t ev_reg1, Int_t ev_reg2){

//     TTree *tr1 = (TTree*)f0->Get("CalibTrk1");  
//     TTree *tr2 = (TTree*)f0->Get("CalibTrk2"); 

//     CalibTrk1Event *calibdata1 = 0;
//     CalibTrk2Event *calibdata2 = 0;
//     tr1->SetBranchAddress("CalibTrk1",&calibdata1);
//     tr2->SetBranchAddress("CalibTrk2",&calibdata2);
//     tr1->GetEntry(ev_reg1);
//     tr2->GetEntry(ev_reg2); 
//     Int_t dspn1(0),dspn2(0);
	
//     for(Int_t i=0;i<6;i++){
// 	dspn1=calibdata1->DSPnumber[i]-1;
// 	dspn2=calibdata2->DSPnumber[i]-1;
// 	/*     cout<<"dspn1= "<<dspn1<<"   dspn2= "<<dspn2<<endl; */
// 	for(Int_t ii=0;ii<3072;ii++){
// 	    Int_t j=(ii/128);
// 	    Int_t jj=ii-j*128; 
// 	    pedestal[jj][j][dspn1]=calibdata1->DSPped_par[i][ii];
// 	    pedestal[jj][j][dspn2]=calibdata2->DSPped_par[i][ii];
// 	    sigma[jj][j][dspn1]=calibdata1->DSPsig_par[i][ii];
// 	    sigma[jj][j][dspn2]=calibdata2->DSPsig_par[i][ii];
// 	    //	      cout<<"sigma trk1= "<<sigma[jj][j][dspn1]<<endl;
// 	    //        cout<<"sigma_t trk1= "<<sigma_t[jj][j][dspn1]<<endl;
// 	    //        cout<<"sigma trk2= "<<sigma[jj][j][dspn2]<<endl;
// 	    //        cout<<"sigma_t trk2= "<<sigma_t[jj][j][dspn2]<<endl;
	
// 	    //
// 	    // invert the bad
// 	    //
// 	    if(calibdata1->DSPbad_par[i][ii]==0)
// 		bad[jj][j][dspn1]=1;
// 	    else if(calibdata1->DSPbad_par[i][ii]==1)
// 		bad[jj][j][dspn1]=0;
// 	    if(calibdata2->DSPbad_par[i][ii]==0)
// 		bad[jj][j][dspn2]=1;
// 	    else if(calibdata2->DSPbad_par[i][ii]==1)
// 		bad[jj][j][dspn2]=0;
// 	};
//     };
	
//     tr1->Delete();
//     tr2->Delete();
// };
// /**
//  * Method to fill the tracker calibration-parameter struct from on-line calibrations
//  */
// void cTrkCalib::FillTCalibFrom(TFile* f0, Int_t ev_reg1, Int_t ev_reg2){

//     TTree *tr1 = (TTree*)f0->Get("CalibTrk1");  
//     TTree *tr2 = (TTree*)f0->Get("CalibTrk2"); 

//     CalibTrk1Event *calibdata1 = 0;
//     CalibTrk2Event *calibdata2 = 0;
//     tr1->SetBranchAddress("CalibTrk1",&calibdata1);
//     tr2->SetBranchAddress("CalibTrk2",&calibdata2);
//     tr1->GetEntry(ev_reg1);
//     tr2->GetEntry(ev_reg2); 
//     Int_t dspn1(0),dspn2(0);
	
//     for(Int_t i=0;i<6;i++){
// 	dspn1=calibdata1->DSPnumber[i]-1;
// 	dspn2=calibdata2->DSPnumber[i]-1;
// 	/*     cout<<"dspn1= "<<dspn1<<"   dspn2= "<<dspn2<<endl; */
// 	for(Int_t ii=0;ii<3072;ii++){
// 	    Int_t j=(ii/128);
// 	    Int_t jj=ii-j*128; 
// 	    pedestal_t[jj][j][dspn1]=aint(calibdata1->DSPped_par[i][ii]);
// 	    pedestal_t[jj][j][dspn2]=aint(calibdata2->DSPped_par[i][ii]);
// 	    sigma_t[jj][j][dspn1]=anint(calibdata1->DSPsig_par[i][ii]);
// 	    sigma_t[jj][j][dspn2]=anint(calibdata2->DSPsig_par[i][ii]);	
// 	};
//     };
	
//     tr1->Delete();
//     tr2->Delete();
// };
// /**
//  * Method to fill the tracker calibration-parameter struct from default calibration
//  */
// void cTrkCalib::FillTCalibFrom(TString path){
    
//     path_.FillWith(path);
//     fillpedsigfromdefault_();
//     if(path_.error) throw -216;

// };

/**
 * Method to fill the tracker VA1-mask struct from calibration
 */
// void cTrkMask::Set(TFile* f0, Int_t ev_reg1, Int_t ev_reg2){
    
//     TrkCalib *calib = new TrkCalib();

//     for(Int_t i=0; i<2; i++){
// 	TTree *tr=0; 
// 	if(i==0){
// 	    //-------------------------
// 	    //read calibration packet 1
// 	    //-------------------------
// 	    tr = (TTree*)f0->Get("CalibTrk1");  
// 	    tr->SetBranchAddress("CalibTrk1",calib->GetPointerTo());
// 	    tr->GetEntry(ev_reg1);
// 	}
// 	if(i==1){
// 	    //-------------------------
// 	    //read calibration packet 2
// 	    //-------------------------
// 	    tr = (TTree*)f0->Get("CalibTrk2"); 
// 	    tr->SetBranchAddress("CalibTrk2",calib->GetPointerTo());
// 	    tr->GetEntry(ev_reg2); 
// 	}
// 	for(Int_t ipkt=0; ipkt<NPLANE; ipkt++){
// 	    for(Int_t ivk=0; ivk<NVK; ivk++){
// 		Int_t view   = calib->GetView(ipkt);
// 		Float_t sigm = calib->GetMean("SIG-BAD",ipkt,ivk+1);
// 		Float_t sigmin = 0.;
// 		if(view%2)sigmin = 6.5;
// 		else      sigmin = 2.5;
// 		Int_t mask = 1;
// 		if( sigm<sigmin )mask = 0;
	    
// 		mask_vk_run[ivk][view-1] = mask;
	    
// //		if(mask==0)cout<< " VIEW "<<view<<" VK "<<ivk+1<<" -- <SIG> "<<sigm<<" ---> MASKED!"<<endl;
// 	    }	
// 	}
// 	if(tr)tr->Delete();
//     }

//     if(calib)calib->Delete();

// };

