#include "TMcp.h"
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <TChain.h>
#include <TFile.h>
#include <TMath.h>
#include <TObject.h>
#include <TRandom3.h>
#include <TStopwatch.h>
#include <TTree.h>
ClassImp(TMcp);
using namespace TMath;
TMcp::TMcp(const TMcp &mcp) : TObject(mcp)
{
((TMcp&)mcp).Copy(*this);
}
void TMcp::Copy(TObject &mcp)const
{
((TMcp&)mcp).fRandom.SetSeed(0);
((TMcp&)mcp).fName = fName;
((TMcp&)mcp).fTitle = fTitle;
((TMcp&)mcp).mcpMapFile = mcpMapFile ;
((TMcp&)mcp).fAddRndm = fAddRndm;
for(Int_t i=0; i<TMCP_NQDCS; i++) ((TMcp&)mcp).fQdcSlot[i] = fQdcSlot[i];
for(Int_t i=0; i<4; i++) ((TMcp&)mcp).tShift[i] = tShift[i];
for(Int_t i=0; i<2; i++) ((TMcp&)mcp).thetaMCP[i] = thetaMCP[i];
((TMcp&)mcp).tarMcpPlasticGap = tarMcpPlasticGap;
((TMcp&)mcp).mcpMcpGap = mcpMcpGap;
((TMcp&)mcp).upMcpPlasticGap = upMcpPlasticGap;
((TMcp&)mcp).maskTarGap = maskTarGap;
mcp0.Copy(((TMcp&)mcp).mcp0);
mcp1.Copy(((TMcp&)mcp).mcp1);
((TMcp&)mcp).mcp0.parent = &((TMcp&)mcp);
((TMcp&)mcp).mcp1.parent = &((TMcp&)mcp);
}
void TMcp::Clear(Option_t *)
{
memset(ene,'\0',sizeof(ene));
memset(time,'\0',sizeof(time));
memset(charge,'\0',sizeof(charge));
for(Int_t i=0; i<32; i++){
rTime[i] = sqrt(-1.0);
timeCorr[i] = sqrt(-1.0);
}
maskX = sqrt(-1.0);
maskY = sqrt(-1.0);
xAngle = sqrt(-1.0);
yAngle = sqrt(-1.0);
iAngle = sqrt(-1.0);
mcp0.Clear();
mcp1.Clear();
}
Int_t TMcp::Calibrate(Option_t* prefix,Option_t* path,Option_t* treeName,Option_t* option)
{
if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return -1;}
TStopwatch stopWatch;
stopWatch.Start();
Int_t nTrees = 1;
TTree *inifChain = fChain;
if(strcmp(fChain->ClassName(),"TChain")==0) nTrees = ((TChain*)fChain)->GetNtrees();
TMcp *newMcp = new TMcp(*this);
newMcp->mcp0.fCon = this;
newMcp->mcp1.fCon = this;
Long64_t curEntry = 0;
for(Int_t tt=0; tt<nTrees; tt++){
fChain->LoadTree(curEntry);
TTree *cTree = fChain->GetTree();
if(strcmp(cTree->GetUserInfo()->FindObject("Analysis State")->GetTitle(),"0")!=0) {
printf("* ERROR: Analysis State!=0\n"); return -2;
}
curEntry = curEntry+cTree->GetEntries()+1;
InitTree(cTree);
TString fileName;
TNamed *runNumber = (TNamed*)cTree->GetUserInfo()->FindObject("Run Number");
TString fPath(path);
TString fPrefix(prefix);
fileName = runNumber->GetTitle();
if(prefix == "") fileName = fPath+"run"+fileName+"Cal.root";
else fileName = fPath+fPrefix+fileName+"Cal.root";
if(option=="RECREATE"){ cout << "Creating file: " << fileName << endl;
}else {option="UPDATE"; cout << "Updating file: " << fileName << endl;}
TString newTName((Char_t*)treeName);
TString oldTName(cTree->GetName());
TFile newFile(fileName,option);
TTree *newTree = new TTree("Cal","Calibrated "+oldTName+" Tree",99);
TNamed aState("Analysis State","1");
if(newFile.FindObject(oldTName+"Cal")!=0){printf("Tree already exists!\n"); return -2;}
else if(strcmp(newTName,"")==0){
newTName = oldTName+"Cal";
newTree->SetName(newTName);
newTree->GetUserInfo()->Add(&aState);
}else{
newTree->SetName(newTName+"Cal");
newTree->GetUserInfo()->Add(&aState);
}
for(Int_t j=0; j<cTree->GetUserInfo()->GetEntries(); j++){
TNamed *ntmp = (TNamed*)cTree->GetUserInfo()->At(j);
TNamed *ctmp = (TNamed*)ntmp->Clone();
if(strcmp(ntmp->GetName(),"Analysis State")!=0) newTree->GetUserInfo()->Add(ctmp);
}
newTree->Branch("mcp",&newMcp);
newMcp->InitTree(newTree);
Long64_t nEntries = cTree->GetEntries();
for(Long64_t ientry=0; ientry<nEntries; ientry++){
Double_t prog = ((Double_t)ientry/nEntries)*100;
if(ientry%100==0){printf("Entry: %15lli \t%4.2lf %%\r",ientry,prog); cout.flush();}
newMcp->Clear();
newMcp->mcp0.Calibrate(ientry);
newMcp->mcp1.Calibrate(ientry);
newMcp->mcp0.Calculate(ientry);
newMcp->mcp1.Calculate(ientry);
b_time->GetEntry(ientry);
newMcp->rTime[0] = time[2]-time[1]+tShift[0];
newMcp->rTime[1] = time[2]-time[0]+tShift[1];
newMcp->rTime[2] = time[1]-time[3]+tShift[2];
newMcp->rTime[3] = time[3]-time[4]+tShift[3];
newTree->Fill();
}
newTree->AutoSave();
newFile.Close();
InitTree(inifChain);
}
stopWatch.Stop();
stopWatch.Print();
return nTrees;
}
Int_t TMcp::Calculate(Long64_t entry)
{
return 1;
}
void TMcp::CreateFolders()
{
f_mcp0 = gROOT->GetRootFolder()->AddFolder("MCP0","MCP0 Folder");
f_mcp1 = gROOT->GetRootFolder()->AddFolder("MCP0","MCP1 Folder");
f_mcp0->Write();
f_mcp1->Write();
printf("* MCP Folder Creation \t\t\t\t\t\t\t\t [OK]\n");
}
Int_t TMcp::GenPrime(Option_t* prefix,Option_t* path,Option_t* treeName,Option_t* option)
{
if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return -1;}
TStopwatch stopWatch;
stopWatch.Start();
Int_t nTrees = 1;
TTree *inifChain = fChain;
if(strcmp(fChain->ClassName(),"TChain")==0) nTrees = ((TChain*)fChain)->GetNtrees();
TMcp *newMcp = new TMcp(*this);
newMcp->mcp0.fCon = this;
newMcp->mcp1.fCon = this;
newMcp->mcp0.fConD = &mcp0;
newMcp->mcp1.fConD = &mcp1;
Long64_t curEntry = 0;
for(Int_t tt=0; tt<nTrees; tt++){
fChain->LoadTree(curEntry);
TTree *cTree = fChain->GetTree();
if(strcmp(cTree->GetUserInfo()->FindObject("Analysis State")->GetTitle(),"1")!=0) {
printf("* ERROR: Analysis State!=1\n"); return -2;
}
curEntry = curEntry+cTree->GetEntries()+1;
InitTree(cTree);
TString fileName;
TNamed *runNumber = (TNamed*)cTree->GetUserInfo()->FindObject("Run Number");
TString fPath(path);
TString fPrefix(prefix);
fileName = runNumber->GetTitle();
if(prefix == "") fileName = fPath+"run"+fileName+"Pri.root";
else fileName = fPath+fPrefix+fileName+"Pri.root";
if(option=="RECREATE"){ cout << "Creating file: " << fileName << endl;
}else {option="UPDATE"; cout << "Updating file: " << fileName << endl;}
TString newTName((Char_t*)treeName);
TString oldTName(cTree->GetName());
TFile newFile(fileName,option);
TTree *newTree = new TTree("Pri","Primary "+oldTName+" Tree",99);
oldTName.Chop();
oldTName.Chop();
oldTName.Chop();
if(newFile.FindObject(oldTName+"Pri")!=0){printf("Tree already exists!\n"); return -2;}
else if(strcmp(newTName,"")==0){
newTName = oldTName+"Pri";
newTree->SetName(newTName);
TNamed aState("Analysis State","2");
newTree->GetUserInfo()->Add(&aState);
}else{
newTree->SetName(newTName+"Pri");
TNamed aState("Analysis State","2");
newTree->GetUserInfo()->Add(&aState);
}
for(Int_t j=0; j<cTree->GetUserInfo()->GetEntries(); j++){
TNamed *ntmp = (TNamed*)cTree->GetUserInfo()->At(j);
TNamed *ctmp = (TNamed*)ntmp->Clone();
if(strcmp(ntmp->GetName(),"Analysis State")!=0) newTree->GetUserInfo()->Add(ctmp);
}
newTree->Branch("mcp",&newMcp);
newMcp->InitTree(newTree);
Long64_t nEntries = cTree->GetEntries();
for(Long64_t ientry=0; ientry<nEntries; ientry++){
Double_t prog = ((Double_t)ientry/nEntries)*100;
if(ientry%100==0){printf("Entry: %15lli \t%4.2lf %%\r",ientry,prog); cout.flush();}
Clear();
newMcp->mcp0.GenPrime(ientry);
newMcp->mcp1.GenPrime(ientry);
newMcp->GenPrime(ientry);
newTree->Fill();
}
newTree->AutoSave();
newFile.Close();
InitTree(inifChain);
}
stopWatch.Stop();
stopWatch.Print();
delete newMcp;
return nTrees;
}
Int_t TMcp::GenPrime(Long64_t entry)
{
if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return -1;}
Double_t zprime;
Double_t gap;
TVector3 posV0,posV1,aVec;
posV0.SetXYZ(mcp0.x,mcp0.y,mcp0.z-mcpMcpGap);
posV1.SetXYZ(mcp1.x,mcp1.y,mcp1.z);
aVec = posV1-posV0;
iAngle = aVec.Theta()*1000;
xAngle = ATan(aVec.X()/aVec.Z())*1000;
yAngle = ATan(aVec.Y()/aVec.Z())*1000;
return 1;
}
void TMcp::InitBranch(TTree *tree, Option_t *stage)
{
Char_t *stageOp = (Char_t*)stage;
if(strcmp(stageOp,"Raw")==0){
printf("* Disabling calibrated branches.\n");
tree->SetBranchStatus("mcp0*",0);
tree->SetBranchStatus("mcp1*",0);
tree->SetBranchStatus("xAngle",0);
tree->SetBranchStatus("yAngle",0);
tree->SetBranchStatus("iAngle",0);
tree->SetBranchStatus("mcp0.corner[12]",1);
tree->SetBranchStatus("mcp1.corner[12]",1);
tree->SetBranchStatus("mcp0.cMult",1);
tree->SetBranchStatus("mcp1.cMult",1);
}else if(strcmp(stageOp,"Cal")==0){
printf("* Disabling raw branches.\n");
tree->SetBranchStatus("mcp*",0);
tree->SetBranchStatus("mcp0*",1);
tree->SetBranchStatus("mcp1*",1);
tree->SetBranchStatus("mcp0.corner[12]",0);
tree->SetBranchStatus("mcp1.corner[12]",0);
}else if(strcmp(stageOp,"Pri")==0){
printf("* Enabling only primary branches.\n");
tree->SetBranchStatus("mcp*",0);
tree->SetBranchStatus("mcp0*",0);
tree->SetBranchStatus("mcp1*",0);
tree->SetBranchStatus("mcp.xAngle",1);
tree->SetBranchStatus("mcp.yAngle",1);
tree->SetBranchStatus("mcp.iAngle",1);
}
}
void TMcp::InitClass()
{
SetNameTitle("mcp","MCP Tracking System.");
printf("* MCP QDC and TDC Map Loading . . . ");
if(LoadQdcTdcMap()){
printf("\t\t\t\t\t\t [OK]\n");
}else{
printf("\t\t\t\t\t\t [FAILED!!!]\n");
}
mcp0.InitClass("mcp0",0,TMCP0_NSTAGES);
mcp1.InitClass("mcp1",1,TMCP1_NSTAGES);
mcp0.parent = this;
mcp1.parent = this;
Clear();
fAddRndm = kTRUE;
tShift[0] = 0;
tShift[1] = 0;
tShift[2] = 0;
tShift[3] = 0;
thetaMCP[0] = 0;
thetaMCP[1] = 0;
tarMcpPlasticGap = 0;
mcpMcpGap = 0;
upMcpPlasticGap = 0;
maskTarGap = 0;
}
void TMcp::InitTree(TTree *tree)
{
fChain = tree;
fCurrent = -1;
Char_t chargeName[200];
sprintf(chargeName,"charge[%i][32]",TMCP_NQDCS);
b_time = fChain->GetBranch("time[32]");
b_charge = fChain->GetBranch(chargeName);
b_ene = fChain->GetBranch("ene[32]");
b_rTime = fChain->GetBranch("rTime[32]");
b_timeCorr = fChain->GetBranch("timeCorr[32]");
b_maskX = fChain->GetBranch("maskX");
b_maskY = fChain->GetBranch("maskY");
b_xAngle = fChain->GetBranch("xAngle");
b_yAngle = fChain->GetBranch("yAngle");
b_iAngle = fChain->GetBranch("iAngle");
mcp0.InitTree(tree);
mcp1.InitTree(tree);
}
Bool_t TMcp::LoadQdcTdcMap()
{
Int_t slotNum,chanNum,mcpNum,sigType;
ifstream file(mcpMapFile,ios::in);
if(!file.is_open()) return kFALSE;
Char_t line[200];
file.getline(line,200);
while(!isdigit(line[0])){
file.getline(line,200);
}
for(Int_t i=0; i<TMCP_NQDCS; i++) fQdcSlot[i] = -1;
Int_t curSlot = 0;
while(!file.eof() || isdigit(line[0])){
sscanf(line,"%i %i %i %i",&slotNum,&chanNum,&mcpNum,&sigType);
if(fQdcSlot[curSlot]!=slotNum && fQdcSlot[curSlot]!=-1){curSlot++;}
fQdcSlot[curSlot] = slotNum;
if(mcpNum==0 && sigType<9){
mcp0.fSigMap[sigType] = chanNum;
mcp0.fChargeMap[sigType] = curSlot;
}else if(mcpNum==1 && sigType<9){
mcp1.fSigMap[sigType] = chanNum;
mcp1.fChargeMap[sigType] = curSlot;
}else{
printf("Invalid MCP number or signal type!!!\n");
return kFALSE;
}
file.getline(line,200);
}
file.close();
return kTRUE;
}
Bool_t TMcp::SetMcpMapFile(Char_t *filePath)
{
Bool_t fFound = kFALSE;
mcpMapFile = filePath;
fFound = kTRUE;
return fFound;
}
void TMcp::SetName(const Char_t *name)
{
fName = name;
}
void TMcp::SetNameTitle(const Char_t *name, const Char_t *title)
{
fName = name;
fTitle = title;
}
void TMcp::SetTitle(const Char_t *title)
{
fTitle = title;
}
Bool_t TMcp::Unpack(UShort_t *pEvent)
{
UShort_t *p = pEvent;
UShort_t packetSize;
UShort_t subEventLength,
subPacketSize;
*p--;
*p--;
packetSize = *p;
packetSize = packetSize - 2;
*p++;
*p++;
fNwords = packetSize;
while (packetSize > 0) {
subPacketSize = *p++;
UShort_t subPacketTag = *p++;
Bool_t subFlag = kFALSE;
UShort_t ID;
switch(subPacketTag) {
case VMEADC:
ID = *p++;
if (subPacketSize >3) p = UnpackEnergy(p);
break;
case VMETDC:
ID = *p++;
if (subPacketSize >3) p = UnpackTime(p);
break;
case VMEQDC:
ID = *p++;
if (subPacketSize >3) p = UnpackCharge(p);
break;
default:
printf("Unrecognized sub-packet tag: 0x%x\n", subPacketTag);
Int_t count = 0;
for (UShort_t ii = 0; ii < *p; ii++) {
printf("0x%4x ",*p++);
if (count == 9) {
count = 0;
printf("\n");
}
count++;
}
printf("\n");
p += subPacketSize-2;
return kFALSE;
break;
}
packetSize -= subPacketSize;
}
return kTRUE;
}
UShort_t* TMcp::UnpackEnergy(UShort_t *p)
{
Int_t m_nCrate,m_nSlot,num_evts=0;
Int_t channel;
Short_t value;
m_nCrate = (Int_t)((*p&0x0700)>>8);
m_nSlot = (Int_t)(((*p++)&0xf800)>>11);
num_evts = (Int_t)(((*p++)&0x3f00)>>8);
for (Int_t i=0; i<num_evts; i++){
channel = (Int_t)((*p++)&(0x003f));
value = (Short_t)((*p++)&0x0fff);
ene[channel] = value;
}
p++;
p++;
return p;
}
UShort_t* TMcp::UnpackCharge(UShort_t *p)
{
Int_t m_nCrate,m_nSlot,num_evts=0;
Int_t channel;
Short_t value;
m_nCrate = (Int_t)((*p&0x0700)>>8);
m_nSlot = (Int_t)(((*p++)&0xf800)>>11);
num_evts = (Int_t)(((*p++)&0x3f00)>>8);
for (Int_t i=0;i<num_evts;i++){
channel = (Int_t)((*p++)&(0x003f));
value = (Short_t)((*p++)&0x0fff);
for(Int_t j=0; j<TMCP_NQDCS; j++){
if(m_nSlot==fQdcSlot[j]) charge[j][channel] = value;
}
}
p++;
p++;
return p;
}
UShort_t* TMcp::UnpackTime(UShort_t *p)
{
Int_t m_nCrate,m_nSlot,num_evts=0;
Int_t channel;
Short_t value;
m_nCrate = (Int_t)((*p&0x0700)>>8);
m_nSlot = (Int_t)(((*p++)&0xf800)>>11);
num_evts = (Int_t)(((*p++)&0x3f00)>>8);
for (Int_t i=0; i<num_evts; i++){
channel = (Int_t)((*p++)&(0x003f));
value = (Short_t)((*p++)&0x0fff);
time[channel] = value;
}
p++;
p++;
return p;
}
Last change: Sun Dec 21 12:38:49 2008
Last generated: 2008-12-21 12:38
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.