#include "retriever.hpp" #define SWAP_ENDIAN //triger the swapping endian subroutine (if needed) using std::ifstream; using std::invalid_argument; using std::runtime_error; using std::string; using std::cout; using std::endl; using std::vector; /********************************* /SnsHistogramRetriever constructor /*********************************/ SnsHistogramRetriever::SnsHistogramRetriever(const string &str): source(str) { // open the file BinaryFile=fopen(source.c_str(),"rb"); // check that open was successful if (BinaryFile==NULL) throw invalid_argument("Could not open file: "+source); } /********************************* /SnsHistogramRetriever destructor /*********************************/ SnsHistogramRetriever::~SnsHistogramRetriever() { // close the file if(BinaryFile) fclose(BinaryFile); } /** * \brief This function creates the array according to the * string location * * \param location (INPUT) is the string location coming from the * translation file * \param tr (INPUT) is where to put the final array created */ void SnsHistogramRetriever::getData(const string &location, tree &tr) { string new_location; string definition_version_with_groups = ""; //use to determine priorities vector decla_def; //declaration and definition parts vector LocGlobArray; //local and global array (declaration part) vector Ope; //Operators of the definition part int OperatorNumber; string DefinitionPart; //use to determine the operators vector GrpPriority; //Vector of priority of each group vector OpePriority; //Vector of priority for each operator vector InverseDef; //True=Inverse definition, False=keep it like it is int Everything = 0; //0= we don't want everything, 1=we do int GlobalArraySize = 1; //Size of global array within our program vector Tag, Def; vector LocalArray, GlobalArray; vector GrpPara; Grp_parameters record; // check that the argument is not an empty string if(location.size()<=0) throw invalid_argument("cannot parse empty string"); //format the string location (remove white spaces) without_white_spaces(location, new_location); DeclaDef_separator(new_location, decla_def); //Separate Declaration part from Definition part -> decla_def //check if the definition part has a valid format if (decla_def[1].size() > 0 && decla_def[1].size()<7) throw runtime_error("Definition part is not valid"); //check if we want everything check_want_everything (decla_def, Everything, Tag, Ope); //Separate declaration arrays (local and global) declaration_separator(decla_def[0], LocGlobArray); if (Everything == 0) { //Work on definition part DefinitionPart = decla_def[1]; //Parse definition part, separate Tag from Def TagDef_separator(decla_def[1], Tag, Def, definition_version_with_groups); //check if we have at least one tag if (Tag.size()<1) throw runtime_error("Definition part is not valid"); //Store operators OperatorNumber = Tag.size(); store_operators(DefinitionPart, OperatorNumber, Ope); //Give to each grp its priority assign_grps_priority(definition_version_with_groups, OperatorNumber, GrpPriority, InverseDef, OpePriority); //Store parameters of the definition part into GrpPara[0], GrpPara[1]... store_para_of_definition(Def, OperatorNumber, GrpPara, record); } //parse Local and Global Array from Declaration part parse_declaration_array(LocGlobArray, LocalArray, GlobalArray); //allocate memory for the binary Array for (int i=0; i Def, int HowManyDef, vector & GrpPara, Grp_parameters & record) { //find out first if it's a loop or a list of identifiers for (int i=0; i & GrpPara) { static const string sep=","; int pos1, pos2; string new_def; //Remove "loop(" and ")" def=def.substr(5,def.size()-6); //store the info into GrpPara[i].init, end and increment pos1 = def.find(sep); new_def = def.substr(pos1+1,def.size()-pos1); pos2 = new_def.find(sep); GrpPara[i].init =atoi((def.substr(0,pos1)).c_str()); GrpPara[i].last =atoi((def.substr(pos1+1,pos2).c_str())); GrpPara[i].increment = atoi((new_def.substr(pos2+1, new_def.size()-1).c_str())); return; } /** * \brief This function parses the value of the list of identifiers * * \param def (INPUT) is the definition part to parse * \param GrpPara (INPUT) is the Grp_parameters structure of the list of * identifiers */ void ParseGrp_Value(string& def, int i, vector & GrpPara) { int b=0, a=0; while (b <= def.size()) { if (def[b]==',') { GrpPara[i].value.push_back(atoi((def.substr(a,b-a)).c_str())); a=b+1; } if (b==def.size()) { GrpPara[i].value.push_back(atoi((def.substr(a,b-a)).c_str())); } ++b; } return; } /** * \brief This function parse the local and global array of the * declaration part * * \param LocGlobArray (INPUT) is the full string of the declaration part * \param LocalArray (OUTPUT) is the list of parameters of the local part * \param GlobalArray (OUTPUT) is the list of parameters of the global part */ void parse_declaration_array(vector & LocGlobArray, vector & LocalArray, vector & GlobalArray) { int a=0, b=0; //Parse Local array int i=0; //remove square braces LocGlobArray[i]=LocGlobArray[i].substr(1,LocGlobArray[i].size()-2); while (b <= LocGlobArray[i].size()) { if (LocGlobArray[i][b]==',') { LocalArray.push_back(atoi((LocGlobArray[i].substr(a,b-a)).c_str())); a=b+1;} if (b==LocGlobArray[i].size()) { LocalArray.push_back(atoi((LocGlobArray[i].substr(a,b-a)).c_str())); } ++b; } i=1,a=0,b=0; //Parse Global Array //remove square braces LocGlobArray[i]=LocGlobArray[i].substr(1,LocGlobArray[i].size()-2); while (b <= LocGlobArray[i].size()) { if (LocGlobArray[i][b]==',') { GlobalArray.push_back(atoi((LocGlobArray[i].substr(a,b-a)).c_str())); a=b+1;} if (b==LocGlobArray[i].size()) { GlobalArray.push_back(atoi((LocGlobArray[i].substr(a,b-a)).c_str())); } ++b; } return; } /** * \brief This function calculates the final array according to the * string location * * \param GrpPriority (INPUT) is a list of the groups priorities * \param InverseDef (INPUT) inverses or not the meaning of the definition * part having the same index * \param BinaryArray (INPUT) is the array coming from the binary file * \param Ope (INPUT) is the list of the operators * \param OpePriority (INPUT) is a list of the operator priorities * \param tr (INPUT) is the final location of the array in the NeXus file * \param Tag (INPUT) is the list of the tag_names * \param Def (INPUT) is the list of the tag_definitions * \param LocalArray (INPUT) is the list of parameters of the local declaration * part * \param GlobalArray (INPUT) is the list of parameters of the global * declaration part * \param GrpPara (INPUT) is a list of structures of all the parameters of the * definition part */ void calculate_array (vector & GrpPriority, vector & InverseDef, binary_type * BinaryArray, vector Ope, vector & OpePriority, tree & tr, vector & Tag, vector & Def, vector & LocalArray, vector & GlobalArray, vector & GrpPara) { int HighestPriority; int GrpNumber = GrpPriority.size(); int ArraySize = 1; int ArraySizeGlobal = 1; if (Tag[0]!="*") { HighestPriority = FindMaxPriority(GrpPriority); } //determine array size for (int i=0 ; i dims(GlobalArray.size()); for (int j=0 ; j< GlobalArray.size() ; ++j) { dims[j]=GlobalArray[j]; } // int dims[]={3,10,5}; NXmalloc(&NewArray,rank,&(dims[0]),NX_INT32); //make an array for each group for (int i=0 ; i & GrpPriority) { int MaxValue = 0; for (int i=0 ; iMaxValue) { MaxValue = GrpPriority[i]; } } return MaxValue; } /** * \brief This function calculates the array if the definition tag is PixelID * * \param MyGrpArray (INPUT) * \param BinaryArray (INPUT) is the array that comes from the binary file * \param grp_number (INPUT) is the group index * \param InverseDef (INPUT) allows checking if we want or not the inverse * of the definition * \param def (INPUT) is the list of definition * \param LocalArray (INPUT) ???not used??? * \param GlobalArray (INPUT) * \param GrpPara (INPUT) is the list of parameters of the group * */ void MakeArray_pixelID (binary_type * MyGrpArray, binary_type * BinaryArray, int grp_number, int InverseDef, vector & Def, vector & LocalArray, vector & GlobalArray, vector & GrpPara) { string loop="loop"; if (Def[grp_number][0] == loop[0]) //case with loop { if (InverseDef==1) //case inverse for loop { InversePixelIDLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case for loop { PixelIDLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } else //case with list of identifiers { if (InverseDef==1) //case inverse for list { InversePixelIDList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case for list { PixelIDList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } return; } /** * \brief This function calculates the array if the definition tag is PixelX * * \param MyGrpArray (INPUT) * \param BinaryArray (INPUT) is the array that comes from the binary file * \param grp_number (INPUT) is the group index * \param InverseDef (INPUT) allows checking if we want or not the inverse * of the definition * \param def (INPUT) is the list of definition * \param LocalArray (INPUT) ???not used??? * \param GlobalArray (INPUT) * \param GrpPara (INPUT) is the list of parameters of the group * */ void MakeArray_pixelX (binary_type* MyGrpArray, binary_type* BinaryArray, int grp_number, int InverseDef, vector & Def, vector & LocalArray, vector & GlobalArray, vector & GrpPara) { string loop="loop"; if (Def[grp_number][0] == loop[0]) { if (InverseDef==1) //case inverse with loop { InversePixelXLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case with loop { PixelXLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } else { if(InverseDef==1) //case inverse with list { InversePixelXList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case with list { PixelXList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } return; } /** * \brief This function calculates the array if the definition tag is PixelY * * \param MyGrpArray (INPUT) * \param BinaryArray (INPUT) is the array that comes from the binary file * \param grp_number (INPUT) is the group index * \param InverseDef (INPUT) allows checking if we want or not the inverse * of the definition * \param def (INPUT) is the list of definition * \param LocalArray (INPUT) ???not used??? * \param GlobalArray (INPUT) * \param GrpPara (INPUT) is the list of parameters of the group * */ void MakeArray_pixelY (binary_type* MyGrpArray, binary_type* BinaryArray, int grp_number, int InverseDef, vector & Def, vector & LocalArray, vector & GlobalArray, vector & GrpPara) { string loop="loop"; if (Def[grp_number][0] == loop[0]) { if (InverseDef==1) //inverse case for loop { InversePixelYLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case for loop { PixelYLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } else { if (InverseDef==1) //case inverse with list { InversePixelYList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case { PixelYList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } return; } /** * \brief This function calculates the array if the definition tag is Tbin * * \param MyGrpArray (INPUT) * \param BinaryArray (INPUT) is the array that comes from the binary file * \param grp_number (INPUT) is the group index * \param InverseDef (INPUT) allows checking if we want or not the inverse * of the definition * \param def (INPUT) is the list of definition * \param LocalArray (INPUT) ???not used??? * \param GlobalArray (INPUT) * \param GrpPara (INPUT) is the list of parameters of the group * */ void MakeArray_Tbin (binary_type* MyGrpArray, binary_type* BinaryArray, int grp_number, int InverseDef, vector & Def, vector & LocalArray, vector & GlobalArray, vector & GrpPara) { string loop="loop"; if (Def[grp_number][0] == loop[0]) { if (InverseDef ==1) //case inverse with loop { InverseTbinLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case with loop { TbinLoop (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } else { if (InverseDef==1) //case inverse with list { InverseTbinList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } else //normal case with list { TbinList (MyGrpArray, BinaryArray, GlobalArray, GrpPara, grp_number); } } return; } /** * \brief This function makes a copy of the binary array * * \param MyGrpArray (OUTPUT) is the local copy of the binary array * \param BinaryArray (INPUT) is the array coming from the binary file * \param LocalArray (INPUT) ??? not used ??? * \param GlobalArray (INPUT) is the list of parameters of the global * declaration part */ void MakeArray_Everything (binary_type* MyGrpArray, binary_type* BinaryArray, vector & LocalArray, vector & GlobalArray) { for (int y=0 ; y & LocalArray, vector & GlobalArray, vector & GrpPara) { string OR="OR"; if (Operator[0] == OR[0]) { for (int y=0 ; y>24) & 0x000000FF) | ((x<<8) & 0x00FF0000) | ((x>>8) & 0x0000FF00) | ((x<<24) & 0xFF000000); } /** * \brief This function initialize the array to 0 * * \param MyGrpArray (INPUT/OUTPUT) is the array to initialized * \param GlobalArray (INPUT) is the dimension of the array */ void InitializeArray(binary_type * MyGrpArray, vector & GlobalArray) { for (int a=0 ; a!pixelID case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelIDLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; ipixelID case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelIDLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=GrpPara[grp_number].init; i<=GrpPara[grp_number].last; i=i+GrpPara[grp_number].increment) { for (int tbin=0 ; tbin < GlobalArray[2] ; ++tbin) { MyGrpArray[i*GlobalArray[2]+tbin]= BinaryArray[i*GlobalArray[2]+tbin]; } } return; } /** * \brief This function calculates the array for the !pixelID case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelIDList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; ipixelID case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelIDList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int j=0 ; j!pixelX case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelXLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; i!pixelX case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelXLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=0 ; y!pixelX case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelXList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; ipixelX case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelXList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=0 ; y!pixelY case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelYLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; ipixelY case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelYLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=GrpPara[grp_number].init; y<=GrpPara[grp_number].last; y=y + GrpPara[grp_number].increment) { for (int x=0 ; x!pixelY case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InversePixelYList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; ipixelY case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void PixelYList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=0 ; y!Tbin case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InverseTbinLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; iTbin case * for the loop case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void TbinLoop (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=0 ; y!Tbin case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void InverseTbinList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int i=0 ; iTbin case * for the list of identifiers case * * \param MyGrpArray (INPUT/OUTPUT) is the resulting array * \param BinaryArray (INPUT) is the array coming from the binary file * \param GlobalArray (INPUT) is the the size of the array to return * \param GrpPara (INPUT) is the list of the parameters of the loop operator * \param grp_number (INPUT) is the index of the group */ void TbinList (binary_type * MyGrpArray, binary_type * BinaryArray, vector & GlobalArray, vector & GrpPara, int grp_number) { for (int y=0 ; y & GrpPriority) { int find_one = 0; for (int m=0 ; m= CurrentPriority) { find_one = 1; break; } } if (find_one == 0) { --CurrentPriority; } return; } /** * \brief This function shift to the left all the priority of the groups * behind index i * * \param GrpPriority (INPUT/OUTPUT) is the list of groups priority * \param GrpPriority_size (INPUT) is the size of the groups priority * \param i (INPUT) is the index of the group priority of interest */ void Shift_GrpPriorities_Left (vector & GrpPriority, int & GrpPriority_size, int & i) { for (int k=i+1 ; k & OpePriority, vector & Ope, int & GrpPriority_size, int & i) { for (int j=i;j & GrpPriority, int & HighestPriority, vector & OpePriority, binary_type ** MyGrpArray, vector & GlobalArray, vector & LocalArray, vector & Ope, vector & GrpPara) { int CurrentPriority = HighestPriority; int GrpPriority_size = GrpPriority.size(); //number of grp while (GrpPriority_size > 1) //as long as we have more than just one array { for (int i=0 ; i & decla_def, int & Everything, vector & Tag, vector & Ope) { if (decla_def[1] == "") { Everything = 1; Tag.push_back("*"); Ope.push_back("*"); } return; } /** * \brief This function swap endians of all the numbers of the BinaryArray * * \param GlobalArray (INPUT) is the size of the binary array * \param BinaryArray (INPUT/OUTPUT) is the array coming from the binary file */ void swap_endian (vector & GlobalArray, binary_type * BinaryArray) { for (int j=0 ; j