#include "WCPSst/FrameDataSource.h"

#include "TClonesArray.h"
#include "TH1F.h"

#include <iostream>		// debug
#include <string>


WCPSst::FrameDataSource::FrameDataSource(TTree& ttree, const char* br)
    : WCP::FrameDataSource()
    , event_tree(&ttree)
    , sim_tree(0)
    , event()
    , simtruth()

{
    // sigh, we can't do things this simply because the ttree does not
    // have a single branch.  
    // tree->SetBranchAddress(name, &event);

    event_tree->SetBranchAddress("eventNo" , &event.number);
    event_tree->SetBranchAddress("runNo"   , &event.run);
    event_tree->SetBranchAddress("subRunNo", &event.subrun);

    std::string br_nChannel(br);
    std::string br_channelId(br);
    std::string br_wf(br);
    br_nChannel += "_nChannel";
    br_channelId += "_channelId";
    br_wf += "_wf";
    // event_tree->SetBranchAddress("calib_nChannel", &event.nchannels);
    // event_tree->SetBranchAddress("calib_channelId", &event.channelid);
    // event_tree->SetBranchAddress("calib_wf", &event.signal);
    event_tree->SetBranchAddress(br_nChannel.c_str(), &event.nchannels);
    event_tree->SetBranchAddress(br_channelId.c_str(), &event.channelid);
    event_tree->SetBranchAddress(br_wf.c_str(), &event.signal);

    
}

WCPSst::FrameDataSource::~FrameDataSource()
{
  // event_tree->SetDirectory(0);
  // sim_tree->SetDirectory(0);
  // delete event_tree;
  // delete sim_tree;
  frame.clear();
}

int WCPSst::FrameDataSource::size() const
{
    return event_tree->GetEntries();
}

int WCPSst::FrameDataSource::jump(int frame_number)
{
    if (frame.index == frame_number) {
	return frame_number;
    }

    frame.clear();		// win or lose, we start anew

    if (frame_number < 0) {	// underflow
	return frame_number;
    }

    int siz = event_tree->GetEntry(frame_number);
    if (siz <= 0 ) {
	return -1;
    }

    if (frame_number >= siz) {
	return -1;
    }

    // load into frame
    int nchannels = event.channelid->size();
    for (size_t ind=0; ind < nchannels; ++ind) {
	TH1F* signal = dynamic_cast<TH1F*>(event.signal->At(ind));
	if (!signal) {
	    return -1;
	}

	WCP::Trace trace;
	trace.chid = event.channelid->at(ind);

	trace.tbin = 0;		// full readout, if zero suppress this would be non-zero
	for (int ibin=1; ibin <= signal->GetNbinsX(); ++ibin) {
	    trace.charge.push_back(signal->GetBinContent(ibin));
	    //std::cerr << ibin << " " << signal->GetBinContent(ibin) << std::endl;
	}
        trace.hCharge = signal;
	frame.traces.push_back(trace);
    }
    frame.index = frame_number;
    return frame.index;
}

void WCPSst::FrameDataSource::set_sim_tree(TTree& tree)
{
    sim_tree = &tree;

    tree.SetBranchAddress("simide_size", &rootsimtruth.size);
    tree.SetBranchAddress("simide_channelIdY", &rootsimtruth.channelIdY);
    tree.SetBranchAddress("simide_trackId", &rootsimtruth.trackId);
    tree.SetBranchAddress("simide_tdc", &rootsimtruth.tdc);
    tree.SetBranchAddress("simide_x", &rootsimtruth.x);
    tree.SetBranchAddress("simide_y", &rootsimtruth.y);
    tree.SetBranchAddress("simide_z", &rootsimtruth.z);
    tree.SetBranchAddress("simide_numElectrons", &rootsimtruth.q);
}

WCP::SimTruthSelection WCPSst::FrameDataSource::truth() const
{
    WCP::SimTruthSelection ret;
    if (! sim_tree) {
	return ret;
    }

    if (frame.index < 0) {	// underflow
	return ret;
    }

    simtruth.clear();
    rootsimtruth.clear();
    int siz = sim_tree->GetEntry(frame.index);
    if (siz <= 0 || frame.index >= siz) {
	return ret;
    }


    for (int ind=0; ind < rootsimtruth.size; ++ind) {
	WCP::SimTruth st((*rootsimtruth.x)[ind],
			      (*rootsimtruth.y)[ind],
			      (*rootsimtruth.z)[ind],
			      (*rootsimtruth.q)[ind],
			      (*rootsimtruth.tdc)[ind],
			      (*rootsimtruth.trackId)[ind]);
	ret.push_back(&(*simtruth.insert(st).first));
    }
    return ret;
}


