//Finds the preshower tube from the soft ID
//rfc: working version 1.1 jul 25 2007
#ifndef RTUBE_
#define RTUBE_



#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <TTree.h>
#include <TClass.h>
#include "./treeread/towermap.C"
#include "./treeread/tube_list.C"
#include "./treeread/tube.C"
#include "./treeread/mask.C"
#include <TObject.h>
using namespace std;

////////////////////////////////////////////////////////////////////////////
//                                                                         //
// Rtube                                                                   //
// Class used to read BPRS masks, tube arrays and PMT numbers from softId  //
// User needs to supply the maps directory for the BPRS as text files      //
// to the constructor: ie, on prompt                                       //
// Rtube b                                                                //
// b("$PATH/mapdir/")                                                     //
////////////////////////////////////////////////////////////////////////////



class Rtube : public TObject {

public:
Rtube(char * map_file_location="./maps/"); //User may need to enter the location of the text "map" files
virtual ~Rtube();
//Vectors with return values for the user

Int_t PMTtower[4][20]; //Note PMTtower[0][0] is the top row
Int_t BPRStube[2]; //Info about BRPS tube array, index 0: tube map type (long short user..) index1: tube array(1-5): index2: softId
Int_t PMTid[4801]; //For soft ID, PMT location number 1-80, starting at 0,0 (from the 4*20 array) in that PMT box (use getPMT for ID)
//Methods

Int_t getPMT(Int_t towerid); //Get Pmt for a tower ID
Int_t getTowersPMT(Int_t pmtId); //Return soft ID for a PMT box to PMTtower vector (see above)
Int_t getTube(Int_t towerid); //Return tube array to BPRStube vector (see above) 
Int_t getMask(Int_t tower); //Returns tower id if not mask or zero if masked
Int_t getMapPlot(); //Returns a complete map of PMT and Tower numbers for inspection of changes with mask
Int_t getTubePlot(); //Returns a complete map of PMT and Tube array numbers for inspection of changes
Int_t getPMTidPlot(); //Returns a complete map of PMTid vs Softid numbers for inspection of changes

//Other usefull stuff
Int_t tubearrtower[4801]; //Tube array to soft ID map
Int_t tubearrtowtp[4801];//Tube type (long short user def) to soft ID map
Int_t towervspmt[4801]; //tower-pmt map
ClassDef(Rtube, 1) 

private:
//Read text file trees via the pointer and Read text to tree method
Int_t fillTube();
towermap *ptowermap;
tube_list *ptubelist;
tube *ptubemap;
Int_t flagFilltube;
Int_t flagFillmask;
//Fill data tree pointers
TTree *ttowermap;
TTree *ttube_list;
TTree *ttube;
char* textfilepath;
Int_t maskPMT();
Int_t amask[4801]; //Map of the masked tubes; returns 1 or 0 if masked


};



//#if !defined(__CINT__) 
ClassImp(Rtube);
//#endif


Rtube::Rtube(char * map_file_location){
//Set flags
flagFilltube=0;flagFillmask=0;
textfilepath=(char*)map_file_location;
//Read in data files

//Open files with tower and tube map
ostringstream name;
ttowermap   = new TTree("towermap","towermap");
ttube_list  = new TTree("tube_list","tube_list");
ttube = new TTree("tube","tube");


//Read this into memory
name.str("");name << textfilepath << "towers.txt";
ttowermap->ReadFile(TString(name.str()) ,"WPMT/I:WMOD/I:WROW/I:TOWERID[40]/I:EROW/I:EMOD/I:EPMT/I");
name.str("");name << textfilepath<< "tube_list.txt";
ttube_list->ReadFile(TString(name.str()),"TWPMT/I:TWTYPE/I:TEPMT/I:TETYPE/I");
name.str("");name << textfilepath << "tube.txt";
ttube->ReadFile(TString(name.str()),"LTYP/I:LTU[20]");

//Make pointers to these classes for further use
ptowermap = new towermap(ttowermap);
ptubelist = new tube_list(ttube_list);
ptubemap  = new tube(ttube);
//cout << ptowermap << endl;//debug
//Make maps of slow stuff:
//1) tower vs pmt/pmt vs tower, we do not want to keep repeating this scan

for (Int_t i=0;i<120;i++){
 ptowermap->GetEntry(i);
 //Fill West Side
 for( Int_t j=0 ;j<20;j++) {towervspmt[ptowermap->TOWERID[j]]= ptowermap->WPMT;}
 //Fill East Side
 for( Int_t j=20;j<40;j++) {towervspmt[ptowermap->TOWERID[j]]= ptowermap->EPMT;}
}

//set up mask here to avoid pointer location problems from mask call
getMask(100);
//set up PMTid map here
for (Int_t i=1;i<61;i++){getTowersPMT(i);}
}

//**********getPmt*******
Int_t Rtube::getPMT(Int_t towerid){
//Return the PMT box
if (towerid >4800 || towerid < 1) {cout << "ERROR TowerID out of range" << endl; return -1;}
//cout << "PMT " << towervspmt[towerid] << endl;
return(towervspmt[towerid]);
}

//***********getTowersPMT**********
Int_t Rtube::getTowersPMT(Int_t pmtId){
//Return vector of towers to user in 
Int_t VPMT[5][21];
Int_t row[5];
Int_t drow[5];
Int_t n=0;

//Return towers for a PMT
if ( pmtId>60 || pmtId < 1) {cout << "ERROR PMTID out of range" << endl; return -1;}
//Find towers for this PMT tube

for (Int_t i=0;i<120;i++){
 ptowermap->GetEntry(i);

 //Check PMT, arrange the vectors in the right order

 if (ptowermap->WPMT==pmtId || ptowermap->EPMT==pmtId){
  row[n]=i;
  if(pmtId<31){
   for( Int_t j= 0;j<20;j++) {
    VPMT[n][j]=ptowermap->TOWERID[j]; /*cout << j << " " << n << " " << ptowermap->TOWERID[j] << endl;*/}
   }
  if(pmtId>30){
   for( Int_t j=20;j<40;j++) {
    VPMT[n][j-20]=ptowermap->TOWERID[j]; /*cout << j << " " << n << " " << ptowermap->TOWERID[j] << endl;*/} 
   }
  n=n+1;
 }
}

 if(n!=4){cout << "Towermap layout error, 4 PMT rows were not found!" << endl; return -1;}
//Reorder
//cout << row [0] << " "  << row[1] << " "<< row[2]<< " "  << row[3] << endl;
 drow[1]=0;drow[2]=1;drow[3]=2;drow[4]=3;
if ( ((row[1]-row[0])!=1) || ((row[2]-row[1])!=1) || ((row[3]-row[2])!=1) ){
 //Use bottom of spreadsheet as top of row

 drow[1]=1;drow[2]=2;drow[3]=3;drow[4]=0;
}


//Save rows to output vector
for(Int_t i=0 ; i<4; i++){
 for(Int_t j=0;j<20;j++){
  PMTtower[i][j]= VPMT[drow[i+1]][j];
  PMTid[(PMTtower[i][j])]=(i*20)+(j+1);
 }
}

return 0;
}

//***********getTube**********
Int_t Rtube::getTube(Int_t towerId){
Int_t error=0;
//Return tube for a given tower 
//Format: PMT box type, tube array number (see .txt files for these layouts)
BPRStube[2]=getPMT(towerId);

//Only do this once for speed
if (flagFilltube==0) {error=fillTube();flagFilltube=1;}
//Catch problems
if (error==-1) {return -1;}
//Fill result;
BPRStube[0]=tubearrtowtp[towerId];
BPRStube[1]=tubearrtower[towerId];

return 0;
}

//***********fillTube**********
Int_t Rtube::fillTube(){
//Fill tube array to tower map, this is an internal function, not for user.
Int_t type=0,i=0,j=0;
for (Int_t tu= 0; tu<4800; tu++){
 //Find type 
 type=0;
 for (Int_t l = 0; l< ttube_list->GetEntries() ; l++){
  ptubelist->GetEntry(l);
  if(ptubelist->TWPMT==getPMT(tu+1)){type=ptubelist->TWTYPE ;break;}
  if(ptubelist->TEPMT==getPMT(tu+1)){type=ptubelist->TETYPE ;break;}
  //cout << tu+1<< " " << getPMT(tu+1) << " " << ptubelist->TEPMT << " "<< " " << ptubelist->TWPMT << endl;
 }
 //catch errors
 if(type==0){cout << "ERROR, tube type not found, tube map is empty!!!!"; return -1;}
 //Find location of Id in array
// cout << getPMT(tu+1) << endl;//debug
 getTowersPMT(getPMT(tu+1));
 Int_t kill=0;
 for(i=0;i<4;i++){
  for(j=0;j<20;j++){  
  if (PMTtower[i][j]==tu+1) {kill=-1; break;}
  }
 if (kill==-1) break;
 }

 //from the i,j values we can locate the tube
 //line up the tree to the correct type for the tube map
 for(Int_t map=0; map < ttube->GetEntries() ; map++){
  ptubemap->GetEntry(map);
  if(ptubemap->LTYP==type){/*move to correct row*/ptubemap->GetEntry(map+i); /*and exit*/break;}
 }
tubearrtower[tu+1]=ptubemap->LTU[j]; 
tubearrtowtp[tu+1]=type;
//cout << i << " " << j << " "<< tu+1 <<" " << " " <<tubearrtower[tu+1] << " " <<  tubearrtowtp[tu+1] << endl; //debug
}

return 0;
}

//***********Get Mask**********
Int_t Rtube::getMask(Int_t tower){
//Returns SoftId if no mask or returns zero
if (flagFillmask==0){maskPMT();flagFillmask=1;}
Int_t towerid=(tower * amask[tower]);
return towerid;
}

//***********Make mask**********
Int_t Rtube::maskPMT(){
//Fill mask, not for user, this is an internal call

ostringstream namem;
TTree *mmask = new TTree("mask","mask");
//Read this into memory
namem.str("");namem << textfilepath << "mask.txt";

mmask->ReadFile(TString(namem.str()) ,"PMT/I:TOWARR/I:TIDS/I:TIDE/I");
mask * pmask=new mask(mmask);
//Set all the towers to unmasked
for (Int_t i=0 ;i <4801 ;i++){amask[i]=1;}

for (Int_t row =0 ; row < mmask->GetEntries();row++){
 pmask->GetEntry(row);

 //Check if this tower range is masked
 if (pmask->TIDS!=0){
  //Enter this range as masked
  for(Int_t i=pmask->TIDS ; i<((pmask->TIDE)+1); i++){
   amask[i]=0;
  }
 }


 //Check if this tube array range is masked
 if (pmask->TIDS==0 && pmask->TOWARR!=0){
  getTowersPMT(pmask->PMT); //Find towers in PMT
  for(Int_t i =0 ; i<80 ;i++){
   //Scan PMT for tube number
   getTube(*((Int_t*)&PMTtower+i));
   //do we have a match?
   //blank this tube
   if(BPRStube[1]==pmask->TOWARR){ amask[*((Int_t*)&PMTtower+i)]=0;}
  }
 }

 //Check if this PMT tube is masked
 if (pmask->TIDS==0 && pmask->TOWARR==0){
  getTowersPMT(pmask->PMT); //Find towers in PMT
  for(Int_t i =0 ; i<80 ;i++){
   //blank this PMT 
   amask[*((Int_t*)&PMTtower+i)]=0;
  }
 } 
}
delete pmask;
return 0;
}

//***********Plot mask/map**********
Int_t Rtube::getMapPlot(){
//Test mask by plotting softid vs PMT in numerical PMT box layout. Zero == masked tower
for (Int_t i=1;i<61;i++){ 
  getTowersPMT(i);
  cout << "************** PMT: " << i << endl; 
  for (Int_t j=0;j<80;j++){
   cout << getMask(*((Int_t*)PMTtower+j)) << " " ;
   if ((j+1)%20==0){cout << endl;}
 }
}
return 0;
}

//***********Plot tube/map**********
Int_t Rtube::getTubePlot(){
//Test map of tube array Id, numerical plot lay out in PMT box sections
for (Int_t i=1;i<31;i++){
  getTowersPMT(i);
  cout << "************** PMT: " << i << endl; 
  for (Int_t j=0;j<80;j++){
   cout << tubearrtower[*((Int_t*)PMTtower+j)] << " ";
   if ((j+1)%20==0){cout << endl;}   
 }
}
return 0;
}

//***********Plot tube/map**********
Int_t Rtube::getPMTidPlot(){
//Test PMTId by making a numerical plot of PMTid (1-80) for each PMT box
for (Int_t i=1;i<61;i++){
  getTowersPMT(i);
  cout << "************** PMT: " << i << endl; 
  for (Int_t j=0;j<80;j++){
   cout << PMTid[*((Int_t*)PMTtower+j)] << "," << *((Int_t*)PMTtower+j) << " " ;
   if ((j+1)%20==0){cout << endl;}   
 }
}
return 0;
}

//*********** delete RTUBE**********
Rtube::~Rtube(){
//Clear heap
delete ttowermap;
delete ttube_list;
delete ttube;
delete ptowermap;
delete ptubelist;
delete ptubemap;
//cout << "Tube object deleted.." << endl ;
}

#endif


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.