// Tools for reading and analysing field maps #include #include int readFieldMap(TTree* t1, TString inputFile="output1.dat", TString fileType="G4",int maxCount=-1, bool debug=false){ // This function needs to be passed a pointer to a TTree that already has been defined // fileType={"TOSHIBA","G4BL","G4"} // maxCount is the number of lines to read // Example usage: // TTree *t1 = new TTree("t1","t1"); // readFieldMap(t1, "myInputFile.txt"); // t1->Draw("z:Bmod"); char tmp[1000]; TString tempStr=""; TObjArray *tempArray; int count=0; bool found=false; int numVars; TString *varNames; double *theVars; ifstream theFile(inputFile.Data()); if(!theFile){ std::cout << "Error reading " << inputFile << std::endl; return 1; } if (fileType=="TOSHIBA"){ // Skip first 9 lines for(int j=0;j<9;j++){ theFile.getline(tmp,1000); } numVars=7; theVars = new double[numVars]; varNames=new TString[numVars]; varNames[0]="x";varNames[1]="y";varNames[2]="z"; varNames[3]="Bx";varNames[4]="By";varNames[5]="Bz"; varNames[6]="Bmod"; }else if (fileType=="G4BL"){ //skip lines until the line containing "data" is found. while (!found){ theFile.getline(tmp,1000); tempStr=tmp; if (tempStr.Index("data")>-1) found=true; if (theFile.eof()){ std::cout << "Error finding start of field map" << std::endl; return 1; } } numVars=6; theVars = new double[numVars]; varNames=new TString[numVars]; varNames[0]="x";varNames[1]="y";varNames[2]="z"; varNames[3]="Bx";varNames[4]="By";varNames[5]="Bz"; }else if (fileType=="G4"){ //skip lines until the line containing "Start field listing" is found. while (!found){ theFile.getline(tmp,1000); tempStr=tmp; if (tempStr.Index("Start field listing")>-1){ found=true; //std::cout << tempStr.Data() << std::endl; } if (theFile.eof()){ std::cout << "Error finding start of field map" << std::endl; return 1; } } numVars=9; theVars = new double[numVars]; varNames=new TString[numVars]; varNames[0]="x";varNames[1]="y";varNames[2]="z"; varNames[3]="Bx";varNames[4]="By";varNames[5]="Bz"; varNames[6]="Ex";varNames[7]="Ey";varNames[8]="Ez"; }else{ std::cout << "Error unknown file type " << fileType << std::endl; return 1; } for (int i=0;iBranch(varNames[i],&theVars[i],varNames[i]+"/D"); } if(debug) std::cout << "Press Enter to advance or q to quit" << std::endl; found=false; if (fileType=="G4"){ while(!theFile.eof() && !found){ theFile.getline(tmp,1000); tempStr=tmp; if (tempStr.Index("End field listing")>-1){ found=true; //std::cout << "Found end" << std::endl << tempStr.Data() << std::endl; }else{ //std::cout << tempStr.Data() << std::endl; tempArray=tempStr.Tokenize(" "); if (tempArray->GetEntries() != numVars){ std::cout << "Error expected to read " << numVars << " variables but found " << tempArray->GetEntries() << std::endl; return 1; } for (int i=0;iString().Atof(); } if(theFile.good()){ //If the last read was successful fill theTree if(debug){ for (int i=0;iFill(); count++; //Check count if reading fixed number of lines if(maxCount!= -1 && count>=maxCount) break; } } } }else{ while(!theFile.eof()){ for (int i=0;i> theVars[i]; } if(theFile.good()){ //If the last read was successful fill theTree if(debug){ for (int i=0;iFill(); count++; //Check count if reading fixed number of lines if(maxCount!= -1 && count>=maxCount) break; } } } theFile.close(); std::cout << "Read " << count << " lines from " << inputFile << std::endl; return 0; }