Commit 7297856a authored by positron cool guys's avatar positron cool guys
Browse files

macro to demultiplex

parent 442d401a
#include <vector>
#ifdef __MAKECINT__
#pragma link C++ class vector<vector<short> >+;
#pragma link C++ class vector<char>+;
#endif
//#pragma link C++ class vector<vector<short> >+;
#include <iostream>
#include <string>
using namespace std;
#include "Riostream.h"
#include "TH1.h"
#include "TH2.h"
#include "TF1.h"
#include "TH1D.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TStyle.h"
#include "TLegend.h"
#include "TFile.h"
#include "TMath.h"
#include "TLatex.h"
#include "TBranch.h"
#include "TTree.h"
#include "TKey.h"
#include "TClass.h"
#include "TDirectory.h"
//#define sleontsi
//TFile *root_file;
#include "apv_raw.C"
#include "APV_RAW_PED.C"
apv_raw *apv_raw_obj;
APV_RAW_PED *apv_raw_ped;
void CopyDir(TDirectory *source) {
//copy all objects and subdirs of directory source as a subdir of the current directory
TDirectory *savdir = gDirectory;
TDirectory *adir = savdir->mkdir("config");
adir->cd();
//loop on all entries of this directory
TKey *key;
TIter nextkey(source->GetListOfKeys());
while ((key = (TKey*)nextkey())) {
const char *classname = key->GetClassName();
TClass *cl = gROOT->GetClass(classname);
if (!cl) continue;
if (cl->InheritsFrom(TDirectory::Class())) {
source->cd(key->GetName());
TDirectory *subdir = gDirectory;
adir->cd();
CopyDir(subdir);
adir->cd();
// }
// else if (cl->InheritsFrom(TTree::Class())) {
// TTree *T = (TTree*)source->Get(key->GetName());
// adir->cd();
// TTree *newT = T->CloneTree(-1,"fast");
// newT->Write();
} else {
source->cd();
TObject *obj = key->ReadObj();
TObjString *takis = new TObjString(obj->GetName());
savdir->cd();
takis->Write(key->GetName());
}
}
adir->SaveSelf(kTRUE);
savdir->cd();
}
void run(string file){
TFile *root_file = new TFile(file.c_str(),"READ");
apv_raw_obj = new apv_raw((TTree*)root_file->Get("apv_raw"));
apv_raw_ped = new APV_RAW_PED((TTree*)root_file->Get("apv_raw_ped"));
int entries = (apv_raw_obj->fChain)->GetEntries();
int entries_ped = (apv_raw_ped->fChain)->GetEntries();
std::size_t found_run = file.find("run");
std::size_t found_dotroot = file.find(".root");
std::string run_name = file.substr(found_run,found_dotroot-found_run);
std::string path = file.substr(0,found_run);
std::string new_file = path+"mapped/"+run_name+"_mapped.root";
TFile *ntuple = new TFile(new_file.c_str(), "RECREATE");
TTree *aux;
aux = new TTree("apv_raw", "apv_raw");
ULong64_t write_evt;
ULong64_t write_evt_temp=0;
UInt_t write_error;
Int_t write_daqTimeSec;
Int_t write_daqTimeMicroSec;
Int_t write_srsTimeStamp;
UInt_t write_srsTrigger;
std::vector<unsigned int> write_srsFec;
std::vector<unsigned int> write_srsChip;
std::vector<unsigned int> write_srsChan;
std::vector<string> write_mmChamber;
std::vector<int> write_mmLayer;
std::vector<char> write_mmReadout;
std::vector<int> write_mmStrip;
std::vector<vector<short> > write_raw_q_parse;
std::vector<short> charge_info_to_be_puched_back_temp;
aux->Branch("evt" , &write_evt );
aux->Branch("error" , &write_error );
aux->Branch("daqTimeSec" , &write_daqTimeSec );
aux->Branch("daqTimeMicroSec" , &write_daqTimeMicroSec);
aux->Branch("srsTimeStamp" , &write_srsTimeStamp );
aux->Branch("srsTrigger" , &write_srsTrigger );
aux->Branch("srsFec" , &write_srsFec );
aux->Branch("srsChip" , &write_srsChip );
aux->Branch("srsChan" , &write_srsChan );
aux->Branch("mmChamber" , &write_mmChamber );
aux->Branch("mmLayer" , &write_mmLayer );
aux->Branch("mmReadout" , &write_mmReadout );
aux->Branch("mmStrip" , &write_mmStrip );
aux->Branch("raw_q" , &write_raw_q_parse );
// loop on multiplexed map file X
int numOfLines_mapx=320;
double first_mapx[numOfLines_mapx], second_mapx[numOfLines_mapx], third_mapx[numOfLines_mapx], fourth_mapx[numOfLines_mapx], fifth_mapx[numOfLines_mapx];
ifstream InputRead_mapx ("multiplexing_maps/Mapping_new_1.txt");
for (int i=0; i<numOfLines_mapx; i++) {
InputRead_mapx >> first_mapx[i] >> second_mapx[i] >> third_mapx[i] >> fourth_mapx[i] >> fifth_mapx[i];
//std::cout<< first_mapx[i] <<" "<< second_mapx[i] <<" "<< third_mapx[i] <<" "<< fourth_mapx[i] <<" "<< fifth_mapx[i] <<" "<<std::endl;
} // loop on map file
// loop on multiplexed map file X
// loop on multiplexed map file Y
int numOfLines_mapy=320;
double first_mapy[numOfLines_mapy], second_mapy[numOfLines_mapy], third_mapy[numOfLines_mapy], fourth_mapy[numOfLines_mapy], fifth_mapy[numOfLines_mapy];
ifstream InputRead_mapy ("multiplexing_maps/Mapping_new_2.txt");
for (int i=0; i<numOfLines_mapy; i++) {
InputRead_mapy >> first_mapy[i] >> second_mapy[i] >> third_mapy[i] >> fourth_mapy[i] >> fifth_mapy[i];
} // loop on map file
// loop on multiplexed map file Y
for(int iEntry=0; iEntry<entries; iEntry++) {
if(iEntry%200==0)
cout << "\r" << (double)iEntry/(double)entries*100 << "\% processed" << flush;
(apv_raw_obj->fChain)->GetEntry(iEntry);
write_srsFec.clear();
write_srsChip.clear();
write_srsChan.clear();
write_mmChamber.clear();
write_mmLayer.clear();
write_mmReadout.clear();
write_mmStrip.clear();
write_raw_q_parse.clear();
write_evt = apv_raw_obj->evt;
write_error = apv_raw_obj->error;
write_daqTimeSec = apv_raw_obj->daqTimeSec;
write_daqTimeMicroSec = apv_raw_obj->daqTimeMicroSec;
write_srsTimeStamp = apv_raw_obj->srsTimeStamp;
write_srsTrigger = apv_raw_obj->srsTrigger;
for (int j=0; j<(int)apv_raw_obj->srsFec->size(); j++) {
//X remapping
if(apv_raw_obj->mmReadout->at(j)==49){
for (int p=0; p<numOfLines_mapx; p++) {
if(apv_raw_obj->srsChan->at(j)==(int)first_mapx[p]-2)
{
charge_info_to_be_puched_back_temp.clear();
for(int i_sample=0; i_sample<apv_raw_obj->raw_q->at(j).size(); i_sample++)
{
charge_info_to_be_puched_back_temp.push_back(apv_raw_obj->raw_q->at(j).at(i_sample));
}
write_raw_q_parse.push_back(charge_info_to_be_puched_back_temp);
write_mmStrip.push_back(fifth_mapx[p]);
write_srsChan.push_back(apv_raw_obj->srsChan->at(j));
write_srsFec.push_back(apv_raw_obj->srsFec->at(j));
write_srsChip.push_back(apv_raw_obj->srsChip->at(j));
write_mmChamber.push_back(apv_raw_obj->mmChamber->at(j));
write_mmLayer.push_back(apv_raw_obj->mmLayer->at(j));
write_mmReadout.push_back(apv_raw_obj->mmReadout->at(j));
}
}
}
//Y remapping
else if(apv_raw_obj->mmReadout->at(j)==48){
for (int p=0; p<numOfLines_mapy; p++) {
if(apv_raw_obj->srsChan->at(j)==(int)first_mapy[p]-2)
{
charge_info_to_be_puched_back_temp.clear();
for(int i_sample=0; i_sample<apv_raw_obj->raw_q->at(j).size(); i_sample++)
{
charge_info_to_be_puched_back_temp.push_back(apv_raw_obj->raw_q->at(j).at(i_sample));
}
write_raw_q_parse.push_back(charge_info_to_be_puched_back_temp);
write_mmStrip.push_back(fifth_mapy[p]);
write_srsChan.push_back(apv_raw_obj->srsChan->at(j));
write_srsFec.push_back(apv_raw_obj->srsFec->at(j));
write_srsChip.push_back(apv_raw_obj->srsChip->at(j));
write_mmChamber.push_back(apv_raw_obj->mmChamber->at(j));
write_mmLayer.push_back(apv_raw_obj->mmLayer->at(j));
write_mmReadout.push_back(apv_raw_obj->mmReadout->at(j));
}
}
}
}
aux->Fill();
}
// make second tree
TTree *aux_ped;
aux_ped = new TTree("apv_raw_ped", "apv_raw_ped");
ULong64_t write_pedevt;
UInt_t write_pederror;
Int_t write_peddaqTimeSec;
Int_t write_peddaqTimeMicroSec;
Int_t write_pedsrsTimeStamp;
UInt_t write_pedsrsTrigger;
std::vector<unsigned int> write_pedsrsFec;
std::vector<unsigned int> write_pedsrsChip;
std::vector<unsigned int> write_pedsrsChan;
std::vector<string> write_pedmmChamber;
std::vector<int> write_pedmmLayer;
std::vector<char> write_pedmmReadout;
std::vector<int> write_pedmmStrip;
std::vector<double> write_pedped_mean;
std::vector<double> write_pedped_stdev;
std::vector<double> write_pedped_sigma;
aux_ped->Branch("evt" , &write_pedevt );
aux_ped->Branch("error" , &write_pederror );
aux_ped->Branch("daqTimeSec" , &write_peddaqTimeSec );
aux_ped->Branch("daqTimeMicroSec" , &write_peddaqTimeMicroSec);
aux_ped->Branch("srsTimeStamp" , &write_pedsrsTimeStamp );
aux_ped->Branch("srsTrigger" , &write_pedsrsTrigger );
aux_ped->Branch("srsFec" , &write_pedsrsFec );
aux_ped->Branch("srsChip" , &write_pedsrsChip );
aux_ped->Branch("srsChan" , &write_pedsrsChan );
aux_ped->Branch("mmChamber" , &write_pedmmChamber );
aux_ped->Branch("mmLayer" , &write_pedmmLayer );
aux_ped->Branch("mmReadout" , &write_pedmmReadout );
aux_ped->Branch("mmStrip" , &write_pedmmStrip );
aux_ped->Branch("ped_mean" , &write_pedped_mean );
aux_ped->Branch("ped_stdev" , &write_pedped_stdev );
aux_ped->Branch("ped_sigma" , &write_pedped_sigma );
cout << endl;
for(int iEntry=0; iEntry<entries_ped; iEntry++) {
if(iEntry%200==0)
cout << std::setprecision(2) << "\r" << (double)iEntry/(double)entries_ped*100 << "\% processed" << flush;
(apv_raw_ped->fChain)->GetEntry(iEntry);
write_pedsrsFec.clear();
write_pedsrsChip.clear();
write_pedsrsChan.clear();
write_pedmmChamber.clear();
write_pedmmLayer.clear();
write_pedmmReadout.clear();
write_pedmmStrip.clear();
write_pedevt = apv_raw_ped->evt;
write_pederror = apv_raw_ped->error;
write_peddaqTimeSec = apv_raw_ped->daqTimeSec;
write_peddaqTimeMicroSec = apv_raw_ped->daqTimeMicroSec;
write_pedsrsTimeStamp = apv_raw_ped->srsTimeStamp;
write_pedsrsTrigger = apv_raw_ped->srsTrigger;
for (int j=0; j<(int)apv_raw_ped->srsFec->size(); j++) {
write_pedsrsFec.push_back(apv_raw_ped->srsFec->at(j));
write_pedsrsChip.push_back(apv_raw_ped->srsChip->at(j));
write_pedsrsChan.push_back(apv_raw_ped->srsChan->at(j));
write_pedmmChamber.push_back(apv_raw_ped->mmChamber->at(j));
write_pedmmLayer.push_back(apv_raw_ped->mmLayer->at(j));
write_pedmmReadout.push_back(apv_raw_ped->mmReadout->at(j));
write_pedmmStrip.push_back(apv_raw_ped->mmStrip->at(j));
write_pedped_mean.push_back(apv_raw_ped->ped_mean->at(j));
write_pedped_stdev.push_back(apv_raw_ped->ped_stdev->at(j));
write_pedped_sigma.push_back(apv_raw_ped->ped_sigma->at(j));
}
aux_ped->Fill();
}
ntuple->cd();
TDirectory *target = gDirectory;
target->cd();
CopyDir(root_file);
target->cd();
ntuple->Write();
ntuple->Close();
cout << endl;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment