Example #1
0
        /// <summary>
        /// Write out the user info file. This file contains things like group names, etc., that the user
        /// might want to change. Do not destroy the old one. Rather, move it out of the way!
        /// </summary>
        /// <param name="groupInfo"></param>
        /// <returns></returns>
        private FileInfo WriteUserInfo(List <ArrayGroup> groupInfo, string treeName)
        {
            ///
            /// First job is to figure out where we will put the file. If there is one there already, then
            /// we chose a new filename. :-)
            ///

            FileInfo userInfoFile = null;
            int      index        = 0;

            do
            {
                userInfoFile = new FileInfo(ProxyGenerationLocation.FullName + "\\" + treeName + "Config-" + index.ToString("000") + ".ntup");
                index        = index + 1;
            } while (userInfoFile.Exists);

            ///
            /// Write out the info.
            ///

            using (var writer = userInfoFile.CreateText())
            {
                XmlSerializer output   = new XmlSerializer(typeof(TTreeUserInfo));
                var           userInfo = new TTreeUserInfo()
                {
                    Groups = groupInfo.ToArray()
                };
                output.Serialize(writer, userInfo);
            }

            return(userInfoFile);
        }
Example #2
0
        public void TestConstCStyleArray()
        {
            // Simple set of types for an index array
            var vArray = new ItemCStyleArray("int[]", new ItemSimpleType("arr", "int"));

            vArray.Add(0, "10", true);
            var vIndex = new ItemSimpleType("n", "int");

            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleRename")
            {
            };

            mainClass.Add(vIndex);
            mainClass.Add(vArray);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name      = "ungrouped",
                                                Variables = new VariableInfo[] {
                                                    new VariableInfo()
                                                    {
                                                        NETName = "n", TTreeName = "n"
                                                    },
                                                    new VariableInfo()
                                                    {
                                                        NETName = "arr", TTreeName = "arr"
                                                    }
                                                }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestConstCStyleArray.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleRename", userinfo }
            });

            CopyToOutput(outputFile);

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsTrue(FindInFile(outputFile, "int[] arr"), "Array Decl missing");
            Assert.IsTrue(FindInFile(outputFile, "int n"), "Index decl missing");
            Assert.IsTrue(FindInFile(outputFile, "[ArraySizeIndex(\"10\", IsConstantExpression = true, Index = 0)]"), "Missing array size index attribute");
        }
Example #3
0
        public void TestNonIntIndex()
        {
            /// Create simple user info - but don't do anything with it!
            ItemSimpleType simpleIndex = new ItemSimpleType("index", "float[]");
            ItemSimpleType simpleVal   = new ItemSimpleType("var1", "float[]");
            ROOTClassShell mainClass   = new ROOTClassShell("TestNonIntIndex")
            {
            };

            mainClass.Add(simpleIndex);
            mainClass.Add(simpleVal);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] {
                    new ArrayGroup()
                    {
                        Name = "jets", Variables = new VariableInfo[]
                        {
                            new VariableInfo()
                            {
                                NETName = "index", TTreeName = "index", IndexToGroup = "muons"
                            }
                        }
                    },
                    new ArrayGroup()
                    {
                        Name = "muons", Variables = new VariableInfo[]
                        {
                            new VariableInfo()
                            {
                                NETName = "var1", TTreeName = "var1"
                            }
                        }
                    }
                }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestNonIntIndex.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestNonIntIndex", userinfo }
            });
        }
Example #4
0
        public void GroupWithArrayLengthSpecification()
        {
            ItemSimpleType simple1 = new ItemSimpleType("avar", "int[]");
            ItemSimpleType simple2 = new ItemSimpleType("bvar", "int[]");

            ROOTClassShell mainClass = new ROOTClassShell("GroupWithArrayLengthSpecification")
            {
            };

            mainClass.Add(simple1);
            mainClass.Add(simple2);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] {
                    new ArrayGroup()
                    {
                        Name = "jets",
                        NETNameOfVariableToUseAsArrayLength = "b",
                        Variables = new VariableInfo[] {
                            new VariableInfo()
                            {
                                NETName = "a", TTreeName = "avar"
                            },
                            new VariableInfo()
                            {
                                NETName = "b", TTreeName = "bvar"
                            },
                        }
                    }
                }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("GroupWithArrayLengthSpecification.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "GroupWithArrayLengthSpecification", userinfo }
            });

            DumpOutputFile(outputFile);

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsTrue(FindInFile(outputFile, "UseAsArrayLength"), "Missing the UseAsArrayLength attribute!!");
        }
Example #5
0
        /// <summary>
        /// Similar to the filling in above - we fill in any defaults to make the code cleaner
        /// than might have been done above.
        /// </summary>
        /// <param name="cls"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        private TTreeUserInfo FillInDefaults(ROOTClassShell cls, TTreeUserInfo userInfo)
        {
            //
            // Group class names are often left up to use to determine. Fill in the defaults here.
            //

            foreach (var g in userInfo.Groups)
            {
                if (string.IsNullOrWhiteSpace(g.ClassName))
                {
                    g.ClassName = string.Format("{0}{1}", cls.Name, g.Name);
                }
            }

            return(userInfo);
        }
Example #6
0
        public void TestCStyleArrayBadIndexName()
        {
            // Simple set of types for an index array
            var vArray = new ItemCStyleArray("int[]", new ItemSimpleType("arr", "int"));

            vArray.Add(0, "i", false);
            var vIndex = new ItemSimpleType("n", "int");

            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleRename")
            {
            };

            mainClass.Add(vIndex);
            mainClass.Add(vArray);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name      = "ungrouped",
                                                Variables = new VariableInfo[] {
                                                    new VariableInfo()
                                                    {
                                                        NETName = "n", TTreeName = "n"
                                                    },
                                                    new VariableInfo()
                                                    {
                                                        NETName = "arr", TTreeName = "arr"
                                                    }
                                                }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestCStyleArray.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleRename", userinfo }
            });
        }
Example #7
0
        public void TestSimpleGroupWithCustomClassName()
        {
            /// Create simple user info - but don't do anything with it!
            ItemSimpleType simple    = new ItemSimpleType("var1", "int[]");
            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleGroupAndRename")
            {
            };

            mainClass.Add(simple);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name = "jets", ClassName = "Jet", Variables = new VariableInfo[] { new VariableInfo()
                                                                                                                   {
                                                                                                                       NETName = "myvar", TTreeName = "var1"
                                                                                                                   } }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestSimpleGroupAndRename.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleGroupAndRename", userinfo }
            });

            DumpOutputFile(outputFile);

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsTrue(FindInFile(outputFile, "RenameVariable(\"var1\")"), "Rename missing!");
            Assert.IsTrue(FindInFile(outputFile, "TTreeVariableGrouping"), "Missing TTreeVariableGrouping");
            Assert.IsTrue(FindInFile(outputFile, "jets"), "missing a reference to jets");
            Assert.IsTrue(FindInFile(outputFile, "int myvar"), "myvar missing");
            Assert.IsTrue(FindInFile(outputFile, "int[] var1"), "val1 missing");
            Assert.IsFalse(FindInFile(outputFile, "ungrouped"), "group found");
            Assert.IsFalse(FindInFile(outputFile, "TestSimpleGroupAndRenamejets"), "Found the non-class name default class name");
            Assert.IsTrue(FindInFile(outputFile, "Jet"), "Did not find the Jet custom class name");
        }
Example #8
0
        public void TestDuplicateClassNames()
        {
            var vIndex = new ItemSimpleType("n", "int");

            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleRename")
            {
            };

            mainClass.Add(vIndex);

            ROOTClassShell mainClass1 = new ROOTClassShell("TestSimpleRename")
            {
            };

            mainClass1.Add(vIndex);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass, mainClass1 }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name      = "ungrouped",
                                                Variables = new VariableInfo[] {
                                                    new VariableInfo()
                                                    {
                                                        NETName = "n", TTreeName = "n"
                                                    },
                                                }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestDuplicateClassNames.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestDuplicateClassNames", userinfo }
            });
        }
Example #9
0
        /// <summary>
        /// See if any translation is required.
        /// </summary>
        /// <param name="tTreeUserInfo"></param>
        /// <returns></returns>
        private bool RequiresTranslation(TTreeUserInfo tTreeUserInfo)
        {
            if (tTreeUserInfo.Groups.Length == 0)
            {
                return(false);
            }

            if (tTreeUserInfo.Groups.Length > 1)
            {
                return(true);
            }
            if (tTreeUserInfo.Groups[0].Name != "ungrouped")
            {
                return(true);
            }

            var anyRenames = tTreeUserInfo.Groups[0].Variables.Where(g => g.TTreeName != g.NETName).Any();

            return(anyRenames);
        }
Example #10
0
        public void TestColonsInVarName()
        {
            ItemSimpleType simple = new ItemSimpleType("dude::fork", "int[]");

            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleGroupAndRename")
            {
            };

            mainClass.Add(simple);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name = "jets", Variables = new VariableInfo[] { new VariableInfo()
                                                                                                {
                                                                                                    NETName = "dude::fork", TTreeName = "dude::fork"
                                                                                                } }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestSimpleGroupAndRename.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleGroupAndRename", userinfo }
            });

            DumpOutputFile(outputFile);

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsFalse(FindInFile(outputFile, "dude::fork"), "Saw the double colon!!");
            Assert.IsTrue(FindInFile(outputFile, "dude__fork"), "Missing the variable!!");
        }
Example #11
0
        public void TestNoGroups()
        {
            /// Create simple user info - but don't do anything with it!
            ItemSimpleType simple = new ItemSimpleType("var1", "int[]");

            Assert.IsFalse(simple.NotAPointer, "not a pointer");
            ROOTClassShell mainClass = new ROOTClassShell("TestSimpleRename")
            {
            };

            mainClass.Add(simple);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name = "ungrouped", Variables = new VariableInfo[] { new VariableInfo()
                                                                                                     {
                                                                                                         NETName = "var1", TTreeName = "var1"
                                                                                                     } }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestNoGroups.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleRename", userinfo }
            });

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsFalse(FindInFile(outputFile, "RenameVariable"), "We saw a rename!");
            Assert.IsFalse(FindInFile(outputFile, "ungrouped"), "group found");
        }
Example #12
0
        public void TestCharactersInClassName()
        {
            ItemSimpleType simple = new ItemSimpleType("fork", "int");

            ROOTClassShell mainClass = new ROOTClassShell("##Shapes")
            {
            };

            mainClass.Add(simple);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] { new ArrayGroup()
                                            {
                                                Name = "jets", Variables = new VariableInfo[] { new VariableInfo()
                                                                                                {
                                                                                                    NETName = "fork", TTreeName = "fork"
                                                                                                } }
                                            } }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestCharactersInClassName.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestSimpleGroupAndRename", userinfo }
            });

            DumpOutputFile(outputFile);

            Assert.AreEqual(3, CountInFile(outputFile, "##Shapes"), "Missing reference ot the shapes object");
        }
Example #13
0
        /// <summary>
        /// Write out a variable definition.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="v"></param>
        /// <param name="extraAttributes">Extra attributes to be written before the funciton. Important to do them here otherwise any XML comments we write won't actually be recognized as such!</param>
        private void WriteVariableRenameDefinition(TextWriter output,
                                                   VariableInfo v,
                                                   IDictionary <string, string> varTypes,
                                                   string baseTypeName,
                                                   bool removeOneArrayDecl,
                                                   TTreeUserInfo groupInfo,
                                                   params string[] extraAttributes)
        {
            ///
            /// Figure out the comment that we put at the top
            ///

            string comment = v.Comment;

            if (string.IsNullOrWhiteSpace(comment))
            {
                comment = "";
            }

            if (v.NETName != v.TTreeName)
            {
                comment += string.Format("(TTree Leaf name: {0})", v.TTreeName.FixupLeafName());
            }

            if (!string.IsNullOrWhiteSpace(comment))
            {
                output.WriteLine("    /// <summary>");
                output.WriteLine("    /// {0}", comment);
                output.WriteLine("    /// </summary>");
            }

            //
            // Write out extra attributes
            //

            foreach (var att in extraAttributes)
            {
                output.WriteLine("    {0}", att);
            }

            ///
            /// If this is a rename, then do teh rename
            ///

            var cppVarName = v.NETName;

            if (v.NETName != v.TTreeName)
            {
                cppVarName = v.TTreeName;
                output.WriteLine("    [RenameVariable(\"{0}\")]", v.TTreeName.FixupLeafName());
            }

            ///
            /// Figure out what type the variable is
            ///

            if (!varTypes.ContainsKey(cppVarName))
            {
                throw new ArgumentException("The variable '" + cppVarName + "' is not known!");
            }

            var typeName = varTypes[cppVarName];

            ///
            /// If this is an index, then reset the type and also write
            /// out the index spec. We do, also, have to copy over all the array references
            /// that are found on this guy.
            ///

            if (!string.IsNullOrEmpty(v.IndexToGroup))
            {
                var grpReference = (from g in groupInfo.Groups
                                    where g.Name == v.IndexToGroup
                                    select g).FirstOrDefault();
                if (grpReference == null)
                {
                    throw new ArgumentException("Group '" + v.IndexToGroup + "' is not a defined group!");
                }
                if (!typeName.StartsWith("int"))
                {
                    throw new ArgumentException("Variable of type '" + typeName + "' marked as index - only integers and integer arrays can index into other objects");
                }

                typeName = baseTypeName + v.IndexToGroup + ArrayReferences(typeName);

                output.WriteLine("    [IndexToOtherObjectArray(typeof({0}), \"{1}\")]", baseTypeName, v.IndexToGroup);
            }

            if (removeOneArrayDecl)
            {
                if (!typeName.EndsWith("[]"))
                {
                    throw new ArgumentException("Attempting to remove an array indirection from type '" + typeName + "' and can't find a [] at the end!");
                }
                typeName = typeName.Substring(0, typeName.Length - 2);
            }

            output.WriteLine("    public {0} {1};", typeName, v.NETName.FixupLeafName());
        }
Example #14
0
        /// <summary>
        /// We have to write out the translated class. Sigh!! :-)
        /// </summary>
        /// <param name="tTreeUserInfo"></param>
        /// <param name="p"></param>
        /// <param name="rawClassName"></param>
        private void WriteTranslatedObjectStructure(TextWriter output, TTreeUserInfo tTreeUserInfo, string className, string translateToName, IDictionary <string, string> varTypes, Action kludgeWriter)
        {
            ///
            /// Main class header
            ///

            output.WriteLine("  [TranslateToClass(typeof({0}))]", translateToName);
            output.WriteLine("  public partial class {0}", className);
            output.WriteLine("  {");

            ///
            /// First, do the ungrouped variables
            ///

            var ungrouped = (from g in tTreeUserInfo.Groups
                             where g.Name == "ungrouped"
                             select g).FirstOrDefault();

            if (ungrouped != null)
            {
                output.WriteLine("#pragma warning disable 0649");
                foreach (var v in ungrouped.Variables)
                {
                    WriteVariableRenameDefinition(output, v, varTypes, className, false, tTreeUserInfo);
                }
                output.WriteLine("#pragma warning restore 0649");
            }


            ///
            /// Now do the groups
            ///

            foreach (var grp in tTreeUserInfo.Groups.Where(g => g.Name != "ungrouped"))
            {
                if (!string.IsNullOrWhiteSpace(grp.Comment))
                {
                    output.WriteLine("    /// <summary>");
                    output.WriteLine("    /// {0}", grp.Comment);
                    output.WriteLine("    /// </summary>");
                }
                output.WriteLine("    [TTreeVariableGrouping]");
                output.WriteLine("    public {0}[] {1};", grp.ClassName, grp.Name);
            }

            ///
            /// And the object is finished.
            ///

            kludgeWriter();
            output.WriteLine("  }");
            output.WriteLine();
            output.WriteLine();

            ///
            /// Now do all the group classes
            ///

            foreach (var grp in tTreeUserInfo.Groups.Where(g => g.Name != "ungrouped"))
            {
                output.WriteLine("  public partial class {0}", grp.ClassName);
                output.WriteLine("  {");

                output.WriteLine("#pragma warning disable 0649");
                foreach (var v in grp.Variables)
                {
                    var attrs = v.NETName == grp.NETNameOfVariableToUseAsArrayLength
                        ? new[] { "[TTreeVariableGrouping]", "[UseAsArrayLengthVariable]" }
                        : new[] { "[TTreeVariableGrouping]" };
                    WriteVariableRenameDefinition(output, v, varTypes, className, true, tTreeUserInfo, attrs);
                }
                output.WriteLine("#pragma warning restore 0649");

                output.WriteLine("  }");
                output.WriteLine();
                output.WriteLine();
            }
        }
Example #15
0
        public void TestRenamedIndex()
        {
            /// Create simple user info - but don't do anything with it!
            ItemSimpleType simpleIndex = new ItemSimpleType("index", "int[]");
            ItemSimpleType simpleVal   = new ItemSimpleType("var1", "float[]");
            ROOTClassShell mainClass   = new ROOTClassShell("TestRenamedIndex")
            {
            };

            mainClass.Add(simpleIndex);
            mainClass.Add(simpleVal);
            var ntup = new NtupleTreeInfo()
            {
                Classes = new ROOTClassShell[] { mainClass }, ClassImplimintationFiles = new string[0]
            };

            var userinfo = new TTreeUserInfo()
            {
                Groups = new ArrayGroup[] {
                    new ArrayGroup()
                    {
                        Name = "jets", Variables = new VariableInfo[]
                        {
                            new VariableInfo()
                            {
                                NETName = "muons", TTreeName = "index", IndexToGroup = "muons"
                            }
                        }
                    },
                    new ArrayGroup()
                    {
                        Name = "muons", Variables = new VariableInfo[]
                        {
                            new VariableInfo()
                            {
                                NETName = "var1", TTreeName = "var1"
                            }
                        }
                    }
                }
            };

            var cg         = new ClassGenerator();
            var outputFile = new FileInfo("TestRenamedIndex.cs");

            cg.GenerateClasss(ntup, outputFile, "junk", new Dictionary <string, TTreeUserInfo>()
            {
                { "TestRenamedIndex", userinfo }
            });

            DumpOutputFile(outputFile);

            /// Look through this to see if we can make sure there are no renames!
            Assert.IsTrue(FindInFile(outputFile, "TTreeVariableGrouping"), "Missing TTreeVariableGrouping");
            Assert.IsTrue(FindInFile(outputFile, "jets"), "missing a reference to jets");
            Assert.IsTrue(FindInFile(outputFile, "muons"), "missing a reference to jets");
            Assert.IsTrue(FindInFile(outputFile, "IndexToOtherObjectArray(typeof("), "Missing IndexToOtherObject");
            Assert.IsTrue(FindInFile(outputFile, "TestRenamedIndexmuons muons"), "Muon reference is imporper");
            Assert.IsTrue(FindInFile(outputFile, "float var1"), "var1 missing");
            Assert.IsFalse(FindInFile(outputFile, "ungrouped"), "group found");
        }