Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8344)
+++ trunk/ippTools/src/dettool.c	(revision 8349)
@@ -1654,4 +1654,86 @@
 static bool tonormalizeMode(pxConfig *config)
 {
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // select detRun.iteration
+    // select detProcessedImfile.det_id
+    // select detProcessedImfile.class_id
+    // by:
+    // find the current iteration bassed on det_id
+    // find all exp_ids in the current det_id/iteration from detInputExp
+    // find all rawImfiles in the current exp_ids
+    // compare to detProcessedImfiles by det_id/exp_id
+    // found how many imfile there are in each class_id
+    // and: 
+    // det_id is not in detStackedImfile;
+    // iteration is not in detStackedImfile;
+    // class_id is not in detStackedImfile;
+
+    psString query = psStringCopy(
+        " SELECT"
+        "    detRun.position,"
+        "    detRun.iteration,"
+        " FROM detRun"
+        " LEFT JOIN detInputExp"
+        "    ON detRun.position = detInputExp.det_id"
+        "    AND detRun.iteration = detInputExp.iteration"
+        " LEFT JOIN detStackedImfile"
+        "    ON detInputExp.det_id = detStackedImfile.det_id"
+        "    AND detInputExp.iteration = detStackedImfile.iteration"
+        " LEFT JOIN rawImfile"
+        "    ON detInputExp.exp_id = rawImfile.exp_id"
+        " WHERE"
+        "    detStackedImfile.det_id IS NOT NULL"
+        "    AND detStackedImfile.iteration IS NOT NULL"
+        "    AND detStackedImfile.class_id IS NOT NULL"
+        " GROUP BY"
+        "    detStackedImfile.class_id,"
+        "    rawImfile.class_id"
+        " HAVING"
+        "    COUNT(detStackedImfile.class_id) = COUNT(rawImfile.class_id)"
+        ); 
+
+    // XXX does it make sens to accept any search params?
+#if 0
+    if (config->where) {
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where);
+        psStringAppend(&query, " AND %s", whereClaus);
+        psFree(whereClaus);
+    }
+#endif
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    } 
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
+        return false;
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            return false;
+        }
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "rawDetrendImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
     return true;
 }
